Skip to content

Commit

Permalink
update(yafw): Don't shadow errors and fix ffmpeg path logic (#12099)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablopunk committed May 2, 2024
1 parent 21aa655 commit 3ca6afb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
4 changes: 4 additions & 0 deletions extensions/yafw/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# YAFW Changelog

## [Fix custom paths and add error logging] - 2024-04-30

Now custom paths should work properly. Also, added error logging to debug production issues.

## [Initial Version] - 2024-04-18

Welcome to Yet Another Ffmpeg Wrapper (YAFW)!
12 changes: 11 additions & 1 deletion extensions/yafw/src/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ export const COMPRESSION_OPTIONS: Record<CompressionOptionKey, CompressionOption
};
export const DEFAULT_COMPRESSION: CompressionOptionKey = "balanced";
export const VIDEO_FORMATS = ["mp4", "mov", "avi", "mkv", "webm"];
export const PATH = "/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.:/opt/homebrew/bin:/opt/homebrew/sbin/:~/.bin";
export const PATH = [
".",
"/bin",
"~/.bin",
"/usr/bin",
"/usr/gnu/bin",
"/usr/local/bin",
"/opt/homebrew/bin",
"/opt/homebrew/sbin",
"/usr/local/Cellar/ffmpeg",
].join(":");
44 changes: 25 additions & 19 deletions extensions/yafw/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ const isFFmpegInstalledOnPath = (): boolean => {
}
};

const isFFmpegInstalled = (): boolean => {
return ffmpegPathExists() || isFFmpegInstalledOnPath();
};

export function normalizeFilePath(filePath: string): string {
return filePath.replace(/^file:\/\//, "").replace(/%20/g, " ");
}
Expand All @@ -46,35 +42,45 @@ export function fileExists(file: string) {
return fs.existsSync(file);
}

export async function compressVideoFiles(files: string[], compression: CompressionOptionKey): Promise<string[]> {
if (!isFFmpegInstalled()) {
const getFFmpegCommand = (args: string) => {
const isInPath = isFFmpegInstalledOnPath();
const customPathExists = ffmpegPathExists();

if (isInPath) {
return `zsh -l -c 'PATH=${PATH} ffmpeg ${args}'`;
} else if (customPathExists) {
return `${ffmpegPath} ${args}`;
} else {
showToast({
title: "Error",
message: "FFmpeg is not installed. Please install FFmpeg or specify its path in the extension settings.",
style: Toast.Style.Failure,
});
return [];
return null;
}
};

export async function compressVideoFiles(files: string[], compression: CompressionOptionKey): Promise<string[]> {
const one = files.length === 1;
await showToast(Toast.Style.Animated, one ? "Compressing video..." : "Compressing videos...");

const results = await Promise.all(
files.map(async (file) => {
const output = file.replace(/\.\w+$/, ` (yafw ${compression}).mp4`);
const { crf, bitrate, bufsize } = COMPRESSION_OPTIONS[compression];
const command = `${ffmpegPath} -y -i "${file}" -vcodec libx264 -crf ${crf} -b:v ${bitrate} -bufsize ${bufsize} "${output}"`;

return promisify(exec)(command)
.then(({ stdout, stderr }) => {
console.log(stdout);
console.log(stderr);
return output;
})
.catch((err) => {
console.error(err);
return null;
});
const command = getFFmpegCommand(
`-y -i "${file}" -vcodec libx264 -crf ${crf} -b:v ${bitrate} -bufsize ${bufsize} "${output}"`,
);

if (!command) {
return [];
}

return promisify(exec)(command).then(({ stdout, stderr }) => {
console.log(stdout);
console.log(stderr);
return output;
});
}),
);

Expand Down

0 comments on commit 3ca6afb

Please sign in to comment.