Skip to content

Commit

Permalink
Merge pull request #188 from kuanyui/master
Browse files Browse the repository at this point in the history
Add support for system ffmpeg on Linux
  • Loading branch information
paulrouget authored Feb 7, 2024
2 parents 68a6d91 + cbd4db1 commit 3e49ea3
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ jobs:
- run: ./build.sh
- run: sudo dpkg -i ./dist/linux/x86_64/vdhcoapp-linux-x86_64.deb
- run: ./tests/test.mjs /opt/vdhcoapp/vdhcoapp --with-network
- run: sudo dpkg -r net.downloadhelper.coapp
- run: sudo apt-get update
- run: sudo apt-get install ffmpeg
- run: sudo dpkg -i ./dist/linux/x86_64/vdhcoapp-no-ffmpeg-linux-x86_64.deb
- run: ./tests/test.mjs /opt/vdhcoapp/vdhcoapp --with-network --no-ffmpeg-codecs-tests
- run: sudo dpkg -r net.downloadhelper.coapp.noffmpeg
43 changes: 38 additions & 5 deletions app/src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,50 @@ import open from 'open';

const os = require("os");
const path = require('path');
const fs = require("node:fs");

const logger = require('./logger');
const rpc = require('./weh-rpc');

const exec_dir = path.dirname(process.execPath);
let ffmpeg = path.join(exec_dir, "ffmpeg");
let ffprobe = path.join(exec_dir, "ffprobe");

if (os.platform() == "win32") {
ffmpeg += ".exe";
ffprobe += ".exe";
const ffmpeg = findExecutableFullPath("ffmpeg", exec_dir);
const ffprobe = findExecutableFullPath("ffprobe", exec_dir);

if (!fileExistsSync(ffmpeg)) {
logger.error("ffmpeg not found. Please ensure if ffmpeg is installed and try again.");
process.exit(1);
}
if (!fileExistsSync(ffprobe)) {
logger.error("ffprobe not found. Please ensure if ffprobe is installed and try again.");
process.exit(1);
}

function findExecutableFullPath(programName, extraPath = "") {
programName = ensureProgramExt(programName);
const envPath = (process.env.PATH || '');
const pathArr = envPath.split(path.delimiter);
if (extraPath) {
pathArr.unshift(extraPath);
}
return pathArr
.map((x) => path.join(x, programName))
.find((x) => fileExistsSync(x));
}

function fileExistsSync (filePath) {
try {
return fs.statSync(filePath).isFile();
} catch (error) {
return false;
}
}

function ensureProgramExt(programPath) {
if (os.platform() == "win32") {
return programPath + ".exe";
}
return programPath;
}

// Record all started processes, and kill them if the coapp
Expand Down
69 changes: 65 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,17 @@ fi
if [ $publish == 1 ]; then
files=(
"dist/linux/i686/$package_binary_name-linux-i686.tar.bz2"
"dist/linux/i686/$package_binary_name-no-ffmpeg-linux-i686.tar.bz2"
"dist/linux/i686/$package_binary_name-linux-i686.deb"
"dist/linux/i686/$package_binary_name-no-ffmpeg-linux-i686.deb"
"dist/linux/x86_64/$package_binary_name-linux-x86_64.tar.bz2"
"dist/linux/x86_64/$package_binary_name-no-ffmpeg-linux-x86_64.tar.bz2"
"dist/linux/x86_64/$package_binary_name-linux-x86_64.deb"
"dist/linux/x86_64/$package_binary_name-no-ffmpeg-linux-x86_64.deb"
"dist/linux/aarch64/$package_binary_name-linux-aarch64.tar.bz2"
"dist/linux/aarch64/$package_binary_name-no-ffmpeg-linux-aarch64.tar.bz2"
"dist/linux/aarch64/$package_binary_name-linux-aarch64.deb"
"dist/linux/aarch64/$package_binary_name-no-ffmpeg-linux-aarch64.deb"
"dist/mac/x86_64/$package_binary_name-mac-x86_64.dmg"
"dist/mac/x86_64/$package_binary_name-mac-x86_64-installer.pkg"
"dist/mac/arm64/$package_binary_name-mac-arm64.dmg"
Expand Down Expand Up @@ -283,6 +289,8 @@ yq . -o yaml ./config.toml | \

out_deb_file="$package_binary_name-$target.deb"
out_bz2_file="$package_binary_name-$target.tar.bz2"
out_noffmpeg_deb_file="$package_binary_name-no-ffmpeg-$target.deb"
out_noffmpeg_bz2_file="$package_binary_name-no-ffmpeg-$target.tar.bz2"
out_pkg_file="$package_binary_name-$target-installer.pkg"
out_dmg_file="$package_binary_name-$target.dmg"
out_win_file="$package_binary_name-$target-installer.exe"
Expand Down Expand Up @@ -343,20 +351,64 @@ if [ ! $skip_packaging == 1 ]; then

log "Packaging v$meta_version for $target"

# ===============================================
# LINUX
# ===============================================
if [ $target_os == "linux" ]; then
mkdir -p $target_dist_dir/deb/opt/$package_binary_name
mkdir -p $target_dist_dir/deb/DEBIAN
# --------------------------------
# Variation: No ffmpeg shipped
# --------------------------------
cp LICENSE.txt README.md app/node_modules/open/xdg-open \
$target_dist_dir/$package_binary_name \
$target_dist_dir/deb/opt/$package_binary_name

yq ".package.deb" ./config.toml -o yaml | \
yq e ".package = \"${meta_id}.noffmpeg\"" |\
yq e ".conflicts = \"${meta_id}\"" |\
yq e ".description = \"${meta_description} (does not come with pre-built ffmpeg. Relies on system's ffmpeg)\"" |\
yq e ".architecture = \"${deb_arch}\"" |\
yq e ".depends = \"ffmpeg\"" |\
yq e ".version = \"${meta_version}\"" > $target_dist_dir/deb/DEBIAN/control

ejs -f $target_dist_dir/config.json ./assets/linux/prerm.ejs \
> $target_dist_dir/deb/DEBIAN/prerm
chmod +x $target_dist_dir/deb/DEBIAN/prerm

ejs -f $target_dist_dir/config.json ./assets/linux/postinst.ejs \
> $target_dist_dir/deb/DEBIAN/postinst
chmod +x $target_dist_dir/deb/DEBIAN/postinst

log "Building .deb file"
dpkg-deb --build $target_dist_dir/deb $target_dist_dir/$out_noffmpeg_deb_file

rm -rf $target_dist_dir/$package_binary_name-$meta_version
mkdir $target_dist_dir/$package_binary_name-$meta_version
cp $target_dist_dir/deb/opt/$package_binary_name/* \
$target_dist_dir/$package_binary_name-$meta_version
log "Building .tar.bz2 file"
tar_extra=""
if [ $host_os == "mac" ]; then
tar_extra="--no-xattrs --no-mac-metadata"
fi
(cd $target_dist_dir && tar -cvjS $tar_extra -f $out_noffmpeg_bz2_file $package_binary_name-$meta_version)

# --------------------------------
# Variation: ffmpeg binary shipped
# --------------------------------
cp LICENSE.txt README.md app/node_modules/open/xdg-open \
$target_dist_dir/$package_binary_name \
$target_dist_dir/ffmpeg \
$target_dist_dir/ffprobe \
$target_dist_dir/deb/opt/$package_binary_name

yq ".package.deb" ./config.toml -o yaml | \
yq e ".package = \"$meta_id\"" |\
yq e ".description = \"$meta_description\"" |\
yq e ".architecture = \"$deb_arch\"" |\
yq e ".version = \"$meta_version\"" -o yaml > $target_dist_dir/deb/DEBIAN/control
yq e ".package = \"${meta_id}\"" |\
yq e ".conflicts = \"${meta_id}.noffmpeg\"" |\
yq e ".description = \"${meta_description} (With pre-built ffmpeg shipped.)\"" |\
yq e ".architecture = \"${deb_arch}\"" |\
yq e ".version = \"${meta_version}\"" > $target_dist_dir/deb/DEBIAN/control

ejs -f $target_dist_dir/config.json ./assets/linux/prerm.ejs \
> $target_dist_dir/deb/DEBIAN/prerm
Expand Down Expand Up @@ -384,6 +436,9 @@ if [ ! $skip_packaging == 1 ]; then
rm -rf $target_dist_dir/deb
fi

# ===============================================
# Mac
# ===============================================
if [[ $node_os == "mac" ]]; then
if ! [ -x "$(command -v create-dmg)" ]; then
error "create-dmg not installed"
Expand Down Expand Up @@ -481,6 +536,10 @@ if [ ! $skip_packaging == 1 ]; then
fi
fi

# ===============================================
# Windows
# ===============================================

if [ $node_os == "windows" ]; then
install_dir=$target_dist_dir/install_dir
mkdir -p $install_dir
Expand Down Expand Up @@ -536,7 +595,9 @@ if [ $target_os == "linux" ]; then
log "Binary available: $target_dist_dir_rel/ffprobe"
if [ ! $skip_packaging == 1 ]; then
log "Deb file available: $target_dist_dir_rel/$out_deb_file"
log "Deb file available: $target_dist_dir_rel/$out_noffmpeg_deb_file"
log "Tarball available: $target_dist_dir_rel/$out_bz2_file"
log "Tarball available: $target_dist_dir_rel/$out_noffmpeg_bz2_file"
fi
fi

Expand Down
38 changes: 23 additions & 15 deletions tests/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,33 @@ const install_locations = {
let is_windows = process.platform == "win32";

let bin_path;
let arg1 = process.argv[2];
let arg2 = process.argv[3];
let arg2 = process.argv[2];
let arg3n = process.argv.slice(3);

let with_network = false;
if (arg1 == "--with-network" || arg2 == "--with-network") {
let no_ffmpeg_codecs_tests = false;
if (arg2 === "--with-network" || arg3n.includes("--with-network")) {
with_network = true;
}
if (arg2 === "--no-ffmpeg-codecs-tests" || arg3n.includes("--no-ffmpeg-codecs-tests")) {
no_ffmpeg_codecs_tests = true;
}

let arg1_is_bin = false;
if (arg1 && !arg1.startsWith("-")) {
arg1_is_bin = true;
let arg2_is_bin = false;
if (arg2 && !arg2.startsWith("-")) {
arg2_is_bin = true;
}

if (!arg1_is_bin && !is_windows) {
if (!arg2_is_bin && !is_windows) {
let dir = install_locations[process.platform].user[0][0];
let json_path = path.resolve(os.homedir(), dir, "net.downloadhelper.coapp.json");
let json = JSON.parse(await fs.readFile(json_path));
bin_path = json.path;
} else if (!arg1_is_bin && is_windows) {
} else if (!arg2_is_bin && is_windows) {
console.error("Not supported (FIXME)");
process.exit(1);
} else {
bin_path = path.resolve(arg1);
bin_path = path.resolve(arg2);
}

if (!is_windows) {
Expand Down Expand Up @@ -210,13 +214,17 @@ let old_coapp;
}

if (!old_coapp) {
const codecs = await exec("codecs");
assert_deep_equal("codecs", codecs, expected_codecs);
const formats = await exec("formats");
if (os.platform() == "darwin") {
assert_deep_equal("formats", formats, expected_formats);
if (no_ffmpeg_codecs_tests) {
console.warn("Skipping codecs and format tests because of flag --no-ffmpeg-codecs-tests.");
} else {
console.warn("Skipping format test as it fails on Linux and Windows");
const codecs = await exec("codecs");
assert_deep_equal("codecs", codecs, expected_codecs);
const formats = await exec("formats");
if (os.platform() == "darwin") {
assert_deep_equal("formats", formats, expected_formats);
} else {
console.warn("Skipping format test as it fails on Linux and Windows");
}
}
}

Expand Down

0 comments on commit 3e49ea3

Please sign in to comment.