Commit: f39d816bf54e59cd3eb0d1e635017d877b020962 Parent: 1cca889134bded950dad245123d3f5902db65a1d Author: Randy Palamar Date: Wed, 2 Jun 2021 16:38:41 -0600 rewrite extract-audio in posix sh posix sh is more portable. which is unfourtanate since string handling in rc is nicer imo. also the use of arrays in rc makes for some really nice code. especially when you change ifs like in the old code the arguments were switched for easier use: -l lang: added to extract a different language. still defaults to jpn -d outdir: making this into an option allows a list of files at the end of the command to be converted. this is more usable than the old behaviour Diffstat:
| M | bin/extract-audio | | | 54 | ++++++++++++++++++++++++++++++------------------------ |
1 file changed, 30 insertions(+), 24 deletions(-)
diff --git a/bin/extract-audio b/bin/extract-audio @@ -1,39 +1,45 @@ -#!/bin/rc +#!/bin/sh -fn usage { - echo 'usage: '$0' [dir] [outdir]' >[1=2] +usage() { + echo "usage: $0 [-d outdir] [-l lang] file ..." exit 1 } -if (! ~ $#* 1 2) usage +while getopts "d:l" arg; do + case "${arg}" in + d) outdir="${OPTARG}" ;; + l) lang="${OPTARG}" ;; + *) usage ;; + esac +done -flag e + +shift $((OPTIND - 1)) -dir=`{realpath $1} -out=`{realpath $2} +[ $# -gt 0 ] || usage -if (! test -d $out) mkdir -p $out +out="${outdir:-.}" +lang=${lang:-jpn} -cd $"dir +[ -d "$out" ] || mkdir -p "$out" -ifs='.' -for(f in *) { - t=`{echo $f} - newf=$out/^$t(1)^.opus - idx=`{ffprobe -of 'compact=nk=1:s=.' -v error \ - -show_entries 'stream=index,codec_type:stream_tags=language' \ +IFS= +for f in $*; do + t=$(echo $f | cut -d . -f 1) + newf=$out/$t.opus + idx=$(ffprobe -of compact=nk=1:s=. -v error \ + -show_entries stream=index,codec_type:stream_tags=language \ $f | \ - grep audio.jpn} + awk -F . '/audio.'$lang'/ { print $2; exit }') - switch($f) { - case *.mkv *.mp4 + case $f in + *.mkv|*.mp4) ffmpeg -loglevel error -stats \ - -i $f -map 0:$idx(2) \ + -i $f -map 0:$idx \ -map_metadata -1 \ -c libopus -mapping_family 255 \ - -metadata 'title='^$t(1) \ + -metadata title=$t \ $newf - } -} -ifs=' -' + ;; + *) ;; + esac +done