Skip to content

Commit

Permalink
remove open
Browse files Browse the repository at this point in the history
use instead shell.showItemInFolder #714
  • Loading branch information
mifi committed Nov 22, 2022
1 parent 2c61c11 commit cdd879f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
"json5": "^2.2.0",
"lodash": "^4.17.19",
"mime-types": "^2.1.14",
"open": "^7.0.3",
"semver": "^7.1.3",
"string-to-stream": "^1.1.1",
"strtok3": "^6.0.0",
Expand Down
28 changes: 15 additions & 13 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ const App = memo(() => {
const metadataFromPath = paths[0];
await concatFiles({ paths, outPath, outDir, outFormat, metadataFromPath, includeAllStreams, streams, ffmpegExperimental, onProgress: setCutProgress, preserveMovData, movFastStart, preserveMetadataOnMerge, chapters: chaptersFromSegments, appendFfmpegCommandLog });
if (clearBatchFilesAfterConcat) closeBatch();
openDirToast({ icon: 'success', dirPath: outDir, text: i18n.t('Files merged!') });
openDirToast({ icon: 'success', filePath: outPath, text: i18n.t('Files merged!') });
} catch (err) {
if (isOutOfSpaceError(err)) {
showDiskFull();
Expand Down Expand Up @@ -1325,13 +1325,14 @@ const App = memo(() => {
enableOverwriteOutput,
});

let concatOutPath;
if (willMerge) {
setCutProgress(0);
setWorking(i18n.t('Merging'));

const chapterNames = segmentsToChapters && !invertCutSegments ? segmentsToExport.map((s) => s.name) : undefined;

await autoConcatCutSegments({
concatOutPath = await autoConcatCutSegments({
customOutDir,
outFormat: fileFormat,
isCustomFormatSelected,
Expand Down Expand Up @@ -1361,7 +1362,8 @@ const App = memo(() => {
}
}

if (!hideAllNotifications) openDirToast({ dirPath: outputDir, text: msgs.join(' '), timer: 15000 });
const revealPath = concatOutPath || outFiles[0];
if (!hideAllNotifications) openDirToast({ filePath: revealPath, text: msgs.join(' '), timer: 15000 });
} catch (err) {
if (err instanceof RefuseOverwriteError) {
showRefuseToOverwrite();
Expand Down Expand Up @@ -1409,12 +1411,12 @@ const App = memo(() => {
? await captureFramesFfmpeg({ customOutDir, filePath, fromTime: currentTime, captureFormat, enableTransferTimestamps, numFrames: 1 })
: await captureFrameFromTag({ customOutDir, filePath, currentTime, captureFormat, video, enableTransferTimestamps });

if (!hideAllNotifications) openDirToast({ dirPath: outputDir, text: `${i18n.t('Screenshot captured to:')} ${outPath}` });
if (!hideAllNotifications) openDirToast({ filePath: outPath, text: `${i18n.t('Screenshot captured to:')} ${outPath}` });
} catch (err) {
console.error(err);
errorToast(i18n.t('Failed to capture frame'));
}
}, [filePath, getCurrentTime, usingPreviewFile, customOutDir, captureFormat, enableTransferTimestamps, hideAllNotifications, outputDir]);
}, [filePath, getCurrentTime, usingPreviewFile, customOutDir, captureFormat, enableTransferTimestamps, hideAllNotifications]);

const extractSegmentFramesAsImages = useCallback(async (index) => {
if (!filePath) return;
Expand All @@ -1425,8 +1427,8 @@ const App = memo(() => {

try {
setWorking(i18n.t('Extracting frames'));
await captureFramesFfmpeg({ customOutDir, filePath, fromTime: start, captureFormat, enableTransferTimestamps, numFrames });
if (!hideAllNotifications) openDirToast({ dirPath: outputDir, text: i18n.t('Frames extracted to: {{path}}', { path: outputDir }) });
const outPath = await captureFramesFfmpeg({ customOutDir, filePath, fromTime: start, captureFormat, enableTransferTimestamps, numFrames });
if (!hideAllNotifications) openDirToast({ filePath: outPath, text: i18n.t('Frames extracted to: {{path}}', { path: outputDir }) });
} catch (err) {
handleError(err);
} finally {
Expand Down Expand Up @@ -1742,8 +1744,8 @@ const App = memo(() => {
try {
setWorking(i18n.t('Extracting all streams'));
setStreamsSelectorShown(false);
await extractStreams({ customOutDir, filePath, streams: mainStreams, enableOverwriteOutput });
openDirToast({ dirPath: outputDir, text: i18n.t('All streams have been extracted as separate files') });
const extractedPaths = await extractStreams({ customOutDir, filePath, streams: mainStreams, enableOverwriteOutput });
openDirToast({ filePath: extractedPaths[0], text: i18n.t('All streams have been extracted as separate files') });
} catch (err) {
if (err instanceof RefuseOverwriteError) {
showRefuseToOverwrite();
Expand All @@ -1754,7 +1756,7 @@ const App = memo(() => {
} finally {
setWorking();
}
}, [customOutDir, enableOverwriteOutput, filePath, mainStreams, outputDir, setWorking]);
}, [customOutDir, enableOverwriteOutput, filePath, mainStreams, setWorking]);

const detectBlackScenes = useCallback(async () => {
if (!filePath) return;
Expand Down Expand Up @@ -2161,8 +2163,8 @@ const App = memo(() => {
try {
setWorking(i18n.t('Extracting track'));
// setStreamsSelectorShown(false);
await extractStreams({ customOutDir, filePath, streams: mainStreams.filter((s) => s.index === index), enableOverwriteOutput });
openDirToast({ dirPath: outputDir, text: i18n.t('Track has been extracted') });
const extractedPaths = await extractStreams({ customOutDir, filePath, streams: mainStreams.filter((s) => s.index === index), enableOverwriteOutput });
openDirToast({ filePath: extractedPaths[0], text: i18n.t('Track has been extracted') });
} catch (err) {
if (err instanceof RefuseOverwriteError) {
showRefuseToOverwrite();
Expand All @@ -2173,7 +2175,7 @@ const App = memo(() => {
} finally {
setWorking();
}
}, [customOutDir, enableOverwriteOutput, filePath, mainStreams, outputDir, setWorking]);
}, [customOutDir, enableOverwriteOutput, filePath, mainStreams, setWorking]);

const batchFilePaths = useMemo(() => batchFiles.map((f) => f.path), [batchFiles]);

Expand Down
21 changes: 14 additions & 7 deletions src/ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,20 @@ function getPreferredCodecFormat({ codec_name: codec, codec_type: type }) {
}

async function extractNonAttachmentStreams({ customOutDir, filePath, streams, enableOverwriteOutput }) {
if (streams.length === 0) return;
if (streams.length === 0) return [];

console.log('Extracting', streams.length, 'normal streams');

let streamArgs = [];
await pMap(streams, async ({ index, codec, type, format: { format, ext } }) => {
const outPaths = await pMap(streams, async ({ index, codec, type, format: { format, ext } }) => {
const outPath = getSuffixedOutPath({ customOutDir, filePath, nameSuffix: `stream-${index}-${type}-${codec}.${ext}` });
if (!enableOverwriteOutput && await pathExists(outPath)) throw new RefuseOverwriteError();

streamArgs = [
...streamArgs,
'-map', `0:${index}`, '-c', 'copy', '-f', format, '-y', outPath,
];
return outPath;
}, { concurrency: 1 });

const ffmpegArgs = [
Expand All @@ -362,15 +363,17 @@ async function extractNonAttachmentStreams({ customOutDir, filePath, streams, en

const { stdout } = await runFfmpeg(ffmpegArgs);
console.log(stdout);

return outPaths;
}

async function extractAttachmentStreams({ customOutDir, filePath, streams, enableOverwriteOutput }) {
if (streams.length === 0) return;
if (streams.length === 0) return [];

console.log('Extracting', streams.length, 'attachment streams');

let streamArgs = [];
await pMap(streams, async ({ index, codec_name: codec, codec_type: type }) => {
const outPaths = await pMap(streams, async ({ index, codec_name: codec, codec_type: type }) => {
const ext = codec || 'bin';
const outPath = getSuffixedOutPath({ customOutDir, filePath, nameSuffix: `stream-${index}-${type}-${codec}.${ext}` });
if (!enableOverwriteOutput && await pathExists(outPath)) throw new RefuseOverwriteError();
Expand All @@ -379,6 +382,7 @@ async function extractAttachmentStreams({ customOutDir, filePath, streams, enabl
...streamArgs,
`-dump_attachment:${index}`, outPath,
];
return outPath;
}, { concurrency: 1 });

const ffmpegArgs = [
Expand All @@ -395,9 +399,10 @@ async function extractAttachmentStreams({ customOutDir, filePath, streams, enabl
} catch (err) {
// Unfortunately ffmpeg will exit with code 1 even though it's a success
// Note: This is kind of hacky:
if (err.exitCode === 1 && typeof err.stderr === 'string' && err.stderr.includes('At least one output file must be specified')) return;
if (err.exitCode === 1 && typeof err.stderr === 'string' && err.stderr.includes('At least one output file must be specified')) return outPaths;
throw err;
}
return outPaths;
}

// https://stackoverflow.com/questions/32922226/extract-every-audio-and-subtitles-from-a-video-with-ffmpeg
Expand All @@ -418,8 +423,10 @@ export async function extractStreams({ filePath, customOutDir, streams, enableOv
// TODO progress

// Attachment streams are handled differently from normal streams
await extractNonAttachmentStreams({ customOutDir, filePath, streams: outStreams, enableOverwriteOutput });
await extractAttachmentStreams({ customOutDir, filePath, streams: attachmentStreams, enableOverwriteOutput });
return [
...(await extractNonAttachmentStreams({ customOutDir, filePath, streams: outStreams, enableOverwriteOutput })),
...(await extractAttachmentStreams({ customOutDir, filePath, streams: attachmentStreams, enableOverwriteOutput })),
];
}

async function renderThumbnail(filePath, timestamp) {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useFfmpegOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
const { streams } = await readFileMeta(metadataFromPath);
await concatFiles({ paths: segmentPaths, outDir, outPath, metadataFromPath, outFormat, includeAllStreams: true, streams, ffmpegExperimental, onProgress, preserveMovData, movFastStart, chapters, preserveMetadataOnMerge, appendFfmpegCommandLog });
if (autoDeleteMergedSegments) await tryDeleteFiles(segmentPaths);

return outPath;
}, [concatFiles, filePath]);

const html5ify = useCallback(async ({ customOutDir, filePath: filePathArg, speed, hasAudio, hasVideo, onProgress }) => {
Expand Down
5 changes: 2 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import pMap from 'p-map';

const { dirname, parse: parsePath, join, basename, extname, isAbsolute, resolve } = window.require('path');
const fs = window.require('fs-extra');
const open = window.require('open');
const os = window.require('os');
const { shell } = window.require('electron');

Expand Down Expand Up @@ -128,9 +127,9 @@ export function handleError(arg1, arg2) {
}


export const openDirToast = async ({ dirPath, ...props }) => {
export const openDirToast = async ({ filePath, ...props }) => {
const { value } = await toast.fire({ icon: 'success', timer: 5000, showConfirmButton: true, confirmButtonText: i18n.t('Show'), showCancelButton: true, cancelButtonText: i18n.t('Close'), ...props });
if (value) open(dirPath);
if (value) shell.showItemInFolder(filePath);
};

export function setFileNameTitle(filePath) {
Expand Down
3 changes: 1 addition & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10694,7 +10694,6 @@ __metadata:
mkdirp: ^1.0.3
moment: ^2.29.4
mousetrap: ^1.6.5
open: ^7.0.3
p-map: ^4.0.0
patch-package: ^6.2.1
pify: ^5.0.0
Expand Down Expand Up @@ -11714,7 +11713,7 @@ __metadata:
languageName: node
linkType: hard

"open@npm:^7.0.3, open@npm:^7.4.2":
"open@npm:^7.4.2":
version: 7.4.2
resolution: "open@npm:7.4.2"
dependencies:
Expand Down

0 comments on commit cdd879f

Please sign in to comment.