Skip to content

Instantly share code, notes, and snippets.

@torbiak
Created July 28, 2017 09:19
Show Gist options
  • Save torbiak/ffd59a791555c0cf1073222df1788aa7 to your computer and use it in GitHub Desktop.
Save torbiak/ffd59a791555c0cf1073222df1788aa7 to your computer and use it in GitHub Desktop.
Sync audio/video files in some format in SRC to another format in DST.
#!/bin/bash
# usage: syncxcode SRC DST
set -eu
max_procs=3
src_format=flac
dst_format=mp3
function sync {
set -eu
local file=${1:?no filepath given}; shift
# Declare rel on separate line to avoid masking realpath's return value.
local rel
rel=$(realpath --relative-to "$src_dir" "$file")
local src=$src_dir/$rel
local dst=$dst_dir/${rel%.*}.$dst_format
if [[ -e "$dst" ]]; then
echo "= $rel"
return
fi
echo "> $rel"
mkdir -pv "$(dirname "$dst")"
ffmpeg -loglevel error -i "$src" "$dst"
}
export -f sync
src_dir=$(realpath "${1:?no source dir given}"); shift
dst_dir=$(realpath "${1:?no destination dir given}"); shift
export src_dir dst_dir dst_format
# The first argument to bash is the name of the shell, used in error and
# warning messages.
find "$src_dir" -type f -name "*.$src_format" -print0 \
| xargs -P $max_procs -I{} -0 bash -c 'sync "$1"' bash {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment