The swiss army knife of lossless video/audio editing
Implement split segment function #460
Mikael Finstad53850aac7df4
diff --git a/package.json b/package.jsonindex 236a4f14..98b73940 100644--- a/package.json+++ b/package.json@@ -69,7 +69,7 @@"react-dom": "^16.12.0","react-hammerjs": "^1.0.1","react-i18next": "^11.3.3",- "react-icons": "^3.9.0",+ "react-icons": "^3.11.0","react-lottie": "^1.2.3","react-scripts": "^3.4.0","react-sortable-hoc": "^1.5.3",diff --git a/src/App.jsx b/src/App.jsxindex 4cfc6419..756266af 100644--- a/src/App.jsx+++ b/src/App.jsx@@ -50,7 +50,7 @@ import {import { askForOutDir, askForImportChapters, createNumSegments, createFixedDurationSegments, promptTimeOffset, askForHtml5ifySpeed, askForYouTubeInput, askForFileOpenAction } from './dialogs';import { openSendReportDialog } from './reporting';import { fallbackLng } from './i18n';-import { createSegment, createInitialCutSegments, getCleanCutSegments, getSegApparentStart } from './segments';+import { createSegment, createInitialCutSegments, getCleanCutSegments, getSegApparentStart, findSegmentsAtCursor } from './segments';import loadingLottie from './7077-magic-flow.json';@@ -1061,6 +1061,36 @@ const App = memo(() => {return getOutPath(cod, fp, `html5ified-${type}.${ext}`);}, []);+ const firstSegmentAtCursorIndex = useMemo(() => {+ const segmentsAtCursorIndexes = findSegmentsAtCursor(apparentCutSegments, commandedTime);+ return segmentsAtCursorIndexes[0];+ }, [apparentCutSegments, commandedTime]);++ const segmentAtCursorRef = useRef();++ const segmentAtCursor = useMemo(() => {+ const segment = cutSegments[firstSegmentAtCursorIndex];+ segmentAtCursorRef.current = segment;+ return segment;+ }, [cutSegments, firstSegmentAtCursorIndex]);++ const splitCurrentSegment = useCallback(() => {+ const segmentAtCursor2 = segmentAtCursorRef.current;+ if (!segmentAtCursor2) {+ errorToast(i18n.t('No segment to split. Please move cursor over the segment you want to split'));+ return;+ }++ const getNewName = (oldName, suffix) => oldName && `${segmentAtCursor2.name} ${suffix}`;++ const firstPart = createSegment({ name: getNewName(segmentAtCursor2.name, '1'), start: segmentAtCursor2.start, end: currentTimeRef.current });+ const secondPart = createSegment({ name: getNewName(segmentAtCursor2.name, '2'), start: currentTimeRef.current, end: segmentAtCursor2.end });++ const newSegments = [...cutSegments];+ newSegments.splice(firstSegmentAtCursorIndex, 1, firstPart, secondPart);+ setCutSegments(newSegments);+ }, [cutSegments, firstSegmentAtCursorIndex, setCutSegments]);+const loadCutSegments = useCallback((edl) => {const validEdl = edl.filter((row) => ((row.start === undefined || row.end === undefined || row.start < row.end)@@ -1230,6 +1260,7 @@ const App = memo(() => {Mousetrap.bind('+', () => addCutSegment());Mousetrap.bind('backspace', () => removeCutSegment());Mousetrap.bind('d', () => deleteSource());+ Mousetrap.bind('b', () => splitCurrentSegment());return () => {Mousetrap.unbind('space');@@ -1257,11 +1288,12 @@ const App = memo(() => {Mousetrap.unbind('+');Mousetrap.unbind('backspace');Mousetrap.unbind('d');+ Mousetrap.unbind('b');};}, [addCutSegment, capture, changePlaybackRate, cutClick, togglePlay, removeCutSegment,setCutEnd, setCutStart, seekRel, seekRelPercent, shortStep, deleteSource, jumpSeg, toggleHelp,- seekClosestKeyframe, zoomRel, toggleComfortZoom,+ seekClosestKeyframe, zoomRel, toggleComfortZoom, splitCurrentSegment,]);useEffect(() => {@@ -1946,9 +1978,11 @@ const App = memo(() => {updateCurrentSegOrder={updateCurrentSegOrder}setCurrentSegmentName={setCurrentSegmentName}currentCutSeg={currentCutSeg}+ segmentAtCursor={segmentAtCursor}addCutSegment={addCutSegment}removeCutSegment={removeCutSegment}toggleSideBar={toggleSideBar}+ splitCurrentSegment={splitCurrentSegment}/></motion.div>)}diff --git a/src/HelpSheet.jsx b/src/HelpSheet.jsxindex d6c1a3d2..cd2f7ea5 100644--- a/src/HelpSheet.jsx+++ b/src/HelpSheet.jsx@@ -66,6 +66,7 @@ const HelpSheet = memo(({ visible, onTogglePress, ffmpegCommandLog, currentCutSe<div><kbd>BACKSPACE</kbd> {t('Remove current segment')}</div><div><kbd>↑</kbd> {t('Select previous segment')}</div><div><kbd>↓</kbd> {t('Select next segment')}</div>+ <div><kbd>B</kbd> {t('Split segment at cursor')}</div><h2>{t('Timeline/zoom operations')}</h2><div><kbd>Z</kbd> {t('Toggle zoom between 1x and a calculated comfortable zoom level')}</div>diff --git a/src/SegmentList.jsx b/src/SegmentList.jsxindex 135583d0..e03a17c6 100644--- a/src/SegmentList.jsx+++ b/src/SegmentList.jsx@@ -1,6 +1,7 @@import React, { memo } from 'react';import prettyMs from 'pretty-ms';import { FaSave, FaPlus, FaMinus, FaTag, FaSortNumericDown, FaAngleRight } from 'react-icons/fa';+import { AiOutlineSplitCells } from 'react-icons/ai';import { motion } from 'framer-motion';import Swal from 'sweetalert2';import { useTranslation } from 'react-i18next';@@ -8,11 +9,17 @@ import { useTranslation } from 'react-i18next';import { saveColor } from './colors';import { getSegColors } from './util';+const buttonBaseStyle = {+ margin: '0 3px', borderRadius: 3, color: 'white', cursor: 'pointer',+};++const neutralButtonColor = 'rgba(255, 255, 255, 0.2)';+const SegmentList = memo(({formatTimecode, cutSegments, outSegments, getFrameCount, onSegClick,currentSegIndex, invertCutSegments,updateCurrentSegOrder, addCutSegment, removeCutSegment,- setCurrentSegmentName, currentCutSeg, toggleSideBar,+ setCurrentSegmentName, currentCutSeg, segmentAtCursor, toggleSideBar, splitCurrentSegment,}) => {const { t } = useTranslation();@@ -103,13 +110,14 @@ const SegmentList = memo(({const renderFooter = () => {const { segActiveBgColor: currentSegActiveBgColor } = getSegColors(currentCutSeg);+ const { segActiveBgColor: segmentAtCursorActiveBgColor } = getSegColors(segmentAtCursor);return (<><div style={{ display: 'flex', padding: '5px 0', alignItems: 'center', justifyContent: 'center', borderBottom: '1px solid grey' }}><FaPlussize={30}- style={{ margin: '0 5px', borderRadius: 3, color: 'white', cursor: 'pointer', background: 'rgba(255, 255, 255, 0.2)' }}+ style={{ ...buttonBaseStyle, background: neutralButtonColor }}role="button"title={t('Add segment')}onClick={addCutSegment}@@ -117,7 +125,7 @@ const SegmentList = memo(({<FaMinussize={30}- style={{ margin: '0 5px', borderRadius: 3, color: 'white', cursor: 'pointer', background: cutSegments.length < 2 ? 'rgba(255, 255, 255, 0.2)' : currentSegActiveBgColor }}+ style={{ ...buttonBaseStyle, background: cutSegments.length >= 2 ? currentSegActiveBgColor : neutralButtonColor }}role="button"title={`${t('Delete current segment')} ${currentSegIndex + 1}`}onClick={removeCutSegment}@@ -127,7 +135,7 @@ const SegmentList = memo(({size={20}title={t('Change segment order')}role="button"- style={{ padding: 4, margin: '0 5px', background: currentSegActiveBgColor, borderRadius: 3, color: 'white', cursor: 'pointer' }}+ style={{ ...buttonBaseStyle, padding: 4, background: currentSegActiveBgColor }}onClick={onReorderSegsPress}/>@@ -135,9 +143,17 @@ const SegmentList = memo(({size={20}title={t('Label segment')}role="button"- style={{ padding: 4, margin: '0 5px', background: currentSegActiveBgColor, borderRadius: 3, color: 'white', cursor: 'pointer' }}+ style={{ ...buttonBaseStyle, padding: 4, background: currentSegActiveBgColor }}onClick={onLabelSegmentPress}/>++ <AiOutlineSplitCells+ size={20}+ title={t('Split segment at cursor')}+ role="button"+ style={{ ...buttonBaseStyle, padding: 4, background: segmentAtCursor ? segmentAtCursorActiveBgColor : neutralButtonColor }}+ onClick={splitCurrentSegment}+ /></div><div style={{ padding: 10, boxSizing: 'border-box', borderBottom: '1px solid grey', display: 'flex', justifyContent: 'space-between', fontSize: 13 }}>diff --git a/src/segments.js b/src/segments.jsindex 7f5b521c..6fc929bc 100644--- a/src/segments.js+++ b/src/segments.js@@ -24,3 +24,11 @@ export const getCleanCutSegments = (cs) => cs.map((seg) => ({end: seg.end,name: seg.name,}));++export function findSegmentsAtCursor(apparentSegments, currentTime) {+ const indexes = [];+ apparentSegments.forEach((segment, index) => {+ if (segment.start < currentTime && segment.end > currentTime) indexes.push(index);+ });+ return indexes;+}diff --git a/yarn.lock b/yarn.lockindex a0f41dfa..a5607f2b 100644--- a/yarn.lock+++ b/yarn.lock@@ -10928,10 +10928,10 @@ react-i18next@^11.3.3:"@babel/runtime" "^7.3.1"html-parse-stringify2 "2.0.1"-react-icons@^3.9.0:- version "3.9.0"- resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-3.9.0.tgz#89a00f20a0e02e6bfd899977eaf46eb4624239d5"- integrity sha512-gKbYKR+4QsD3PmIHLAM9TDDpnaTsr3XZeK1NTAb6WQQ+gxEdJ0xuCgLq0pxXdS7Utg2AIpcVhM1ut/jlDhcyNg==+react-icons@^3.11.0:+ version "3.11.0"+ resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-3.11.0.tgz#2ca2903dfab8268ca18ebd8cc2e879921ec3b254"+ integrity sha512-JRgiI/vdF6uyBgyZhVyYJUZAop95Sy4XDe/jmT3R/bKliFWpO/uZBwvSjWEdxwzec7SYbEPNPck0Kff2tUGM2Q==dependencies:camelcase "^5.0.0"