Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update(yafw): Don't shadow errors and fix ffmpeg path logic #12099

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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