#!/bin/bash # muxsa-pngaac2mp4 # # part of muxsa, https://git-nks-public.tik.uni-stuttgart.de/edu/muxsa # # this script tells ffmpeg (https://ffmpeg.org/) to read # a series of slides in png file format (created with muxsa-kvm2png), # a soundtrack.aac (recorded with audacity), and # a ffmpeg concat multiplexer sequence file (created with muxsa-al2fc) # and multiplex everything together # into a mp4 video of the slideshow with background narration # MIT License # # Copyright (c) 2023 Sebastian Kiesel # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. ############################################################################ # see https://trac.ffmpeg.org/wiki/Slideshow echo "This is muxsa-pngaac2mp4." # 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 # last resort: default output file name if [ -z "$VIDEO_OUT" ] ; then VIDEO_OUT="out.mp4" fi 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 [ -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 # 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 \ $AUDIO_CODER_OPTS \ -movflags +faststart -brand mp42 "$VIDEO_OUT" ############################################################################