The swiss army knife of lossless video/audio editing
implement shuffle segments order
Mikael Finstad61b7ca2e4489
diff --git a/public/menu.js b/public/menu.jsindex 158bc39c..4aec2af6 100644--- a/public/menu.js+++ b/public/menu.js@@ -207,6 +207,12 @@ module.exports = (app, mainWindow, newVersion) => {mainWindow.webContents.send('invertAllCutSegments');},},+ {+ label: i18n.t('Shuffle segments order'),+ click() {+ mainWindow.webContents.send('shuffleSegments');+ },+ },],},{diff --git a/src/App.jsx b/src/App.jsxindex 328263b5..e8462e95 100644--- a/src/App.jsx+++ b/src/App.jsx@@ -59,7 +59,7 @@ import {checkDirWriteAccess, dirExists, openDirToast, isMasBuild, isStoreBuild, dragPreventer, doesPlayerSupportFile,isDurationValid, isWindows, filenamify, getOutFileExtension, generateSegFileName, defaultOutSegTemplate,hasDuplicates, havePermissionToReadFile, isMac, resolvePathIfNeeded, pathExists, html5ifiedPrefix, html5dummySuffix, findExistingHtml5FriendlyFile,- deleteFiles, getHtml5ifiedPath, isStreamThumbnail, getAudioStreams, getVideoStreams, isOutOfSpaceError,+ deleteFiles, getHtml5ifiedPath, isStreamThumbnail, getAudioStreams, getVideoStreams, isOutOfSpaceError, shuffleArray,} from './util';import { formatDuration } from './util/duration';import { adjustRate } from './util/rate-calculator';@@ -179,6 +179,8 @@ const App = memo(() => {setCutSegments(createInitialCutSegments());}, [clearSegCounter, createInitialCutSegments, setCutSegments]);+ const shuffleSegments = useCallback(() => setCutSegments((oldSegments) => shuffleArray(oldSegments)), [setCutSegments]);+// Store "working" in a ref so we can avoid race conditionsconst workingRef = useRef(working);const setWorking = useCallback((val) => {@@ -2016,6 +2018,7 @@ const App = memo(() => {electron.ipcRenderer.on('batchConvertFriendlyFormat', batchConvertFriendlyFormat);electron.ipcRenderer.on('openSendReportDialog', openSendReportDialog2);electron.ipcRenderer.on('clearSegments', clearSegments);+ electron.ipcRenderer.on('shuffleSegments', shuffleSegments);electron.ipcRenderer.on('createNumSegments', createNumSegments2);electron.ipcRenderer.on('createFixedDurationSegments', createFixedDurationSegments2);electron.ipcRenderer.on('invertAllCutSegments', invertAllCutSegments);@@ -2040,6 +2043,7 @@ const App = memo(() => {electron.ipcRenderer.removeListener('batchConvertFriendlyFormat', batchConvertFriendlyFormat);electron.ipcRenderer.removeListener('openSendReportDialog', openSendReportDialog2);electron.ipcRenderer.removeListener('clearSegments', clearSegments);+ electron.ipcRenderer.removeListener('shuffleSegments', shuffleSegments);electron.ipcRenderer.removeListener('createNumSegments', createNumSegments2);electron.ipcRenderer.removeListener('createFixedDurationSegments', createFixedDurationSegments2);electron.ipcRenderer.removeListener('invertAllCutSegments', invertAllCutSegments);diff --git a/src/util.js b/src/util.jsindex a5cce02b..20fac866 100644--- a/src/util.js+++ b/src/util.js@@ -293,3 +293,23 @@ export const isOutOfSpaceError = (err) => (err && (err.exitCode === 1 || err.code === 'ENOENT')&& typeof err.stderr === 'string' && err.stderr.includes('No space left on device'));++// https://stackoverflow.com/a/2450976/6519037+export function shuffleArray(arrayIn) {+ const array = [...arrayIn];+ let currentIndex = array.length;+ let randomIndex;++ // While there remain elements to shuffle...+ while (currentIndex !== 0) {+ // Pick a remaining element...+ randomIndex = Math.floor(Math.random() * currentIndex);+ currentIndex -= 1;++ // And swap it with the current element.+ [array[currentIndex], array[randomIndex]] = [+ array[randomIndex], array[currentIndex]];+ }++ return array;+}