diff --git a/bin/muxsa-pngaac2mp4 b/bin/muxsa-pngaac2mp4 index e4cb2cc..587eed7 100755 --- a/bin/muxsa-pngaac2mp4 +++ b/bin/muxsa-pngaac2mp4 @@ -38,41 +38,94 @@ echo "This is muxsa-pngaac2mp4." -OUTFILE="out.mp4" -if [ "$1" ] ; then - OUTFILE="$1" - # make sure that file ends on .mp4 - OUTFILE="${OUTFILE%%.mp4}.mp4" +# first, we need to define 3 file names - check command line, then defaults: +# SLIDES_IN - concat multiplexer control file with png slide names +# AUDIO_IN - one .wav or .m4a sound file, or concat file with several wav/m4a +# VIDEO_OUT - mp4 output + +while getopts "a:s:o:" OPTION ; do + case "$OPTION" in + a) + if [ -r "$OPTARG" ] ; then + AUDIO_IN="$OPTARG" + else + echo "error: AUDIO_IN file $OPTARG does not exist. abort." + exit 1 + fi + ;; + s) + if [ -r "$OPTARG" ] ; then + SLIDES_IN="$OPTARG" + else + echo "error: SLIDES_IN file $OPTARG does not exist. abort." + exit 1 + fi + ;; + o) + VIDEO_OUT="$OPTARG" + ;; + *) + echo "Incorrect options provided - ignored" + ;; + esac +done +shift "$((OPTIND-1))" +# backward compatibility: before getopts we only accepted OUT as parameter +if [ -z "$VIDEO_OUT" -a -n "$1" ] ; then + VIDEO_OUT="$1" fi -if [ ! -f slides.concat ] ; then - echo "error: file slides.concat missing. abort." - echo "consider using muxsa-al2fc for creating one." +if [ -z "$AUDIO_IN" -a -r "soundtrack.m4a" ] ; then + echo "Setting AUDIO_IN to default file name soundtrack.m4a" + AUDIO_IN="soundtrack.m4a" +fi +if [ -z "$AUDIO_IN" -a -r "soundtrack.wav" ] ; then + echo "Setting AUDIO_IN to default file name soundtrack.wav" + AUDIO_IN="soundtrack.wav" +fi +if [ -z "$AUDIO_IN" -a -r "audio.concat" ] ; then + echo "Setting AUDIO_IN to default file name audio.concat" + AUDIO_IN="audio.concat" +fi +if [ -z "$AUDIO_IN" ] ; then + echo "No input audio file given and no default file name found. abort." exit 1 fi -if [ -f soundtrack.m4a ] ; then - AUDIOFILE="-i soundtrack.m4a" - AUDIOOPTS="-c:a copy" -else - if [ -f soundtrack.wav ] ; then - AUDIOFILE="-i soundtrack.wav" - AUDIOOPTS="-c:a aac -b:a 128k" - else - if [ -f audio.concat ] ; then - AUDIOFILE="-f concat -i audio.concat" - AUDIOOPTS="-c:a aac -b:a 128k" - else - echo "neither soundtrack.m4a, .wav, nor audio.concat found. abort." - exit 1 - fi - fi +if [ -z "$SLIDES_IN" -a -r "slides.concat" ] ; then + echo "Setting SLIDES_IN to default file name slides.concat" + SLIDES_IN="slides.concat" +fi +if [ -z "$SLIDES_IN" ] ; then + echo "No input slides file given and no default file name found. abort." + exit 1 fi -ffmpeg -f concat -i slides.concat ${AUDIOFILE} \ +# now we got all input and output file names + +# depending on the type of the audio input, we need to give different options +case "${AUDIO_IN##*.}" in + m4a|M4A|aac|AAC) + AUDIO_SOURCE_OPTS="-i" + AUDIO_CODER_OPTS="-c:a copy" + ;; + concat|CONCAT|sequence|SEQUENCE|seq|SEQ) + AUDIO_SOURCE_OPTS="-f concat -i" + AUDIO_CODER_OPTS="-c:a aac -b:a 128k" + ;; + *) + AUDIO_SOURCE_OPTS="-i" + AUDIO_CODER_OPTS="-c:a aac -b:a 128k" + ;; +esac + +# time to start ffmpeg +# no "quotes" around the $..._OPTs on purpose +set -ex +ffmpeg -f concat -safe 0 -i "$SLIDES_IN" $AUDIO_SOURCE_OPTS "$AUDIO_IN" \ -vsync cfr -vf fps=25 \ -c:v libx264 -preset slow -tune stillimage -crf 23 -pix_fmt yuv420p \ - ${AUDIOOPTS} \ - -movflags +faststart -brand mp42 "${OUTFILE}" + $AUDIO_CODER_OPTS \ + -movflags +faststart -brand mp42 "$VIDEO_OUT" ############################################################################