#!/bin/bash # This script generates scripts inside each subdirectory of the current directory; # so it is expected that each film's files (the film, the subtitles) are put in # separate subdirectories. # # The assumption is that the AVI container contains H264-encoded videos, but that's # not strictly required, it's just that H264-encoded video seems to be the preferred # option these days. # # The scripts that this script generates *echo* the commands that would need to be # run. So the expected workflow is something like: # # mkdir film1 # cp ... film1/ # mkdir film2 # cp ... film2/ # ./combine-avi-and-srt-into-mkv # cd film1 # ./go # does it look ok? # ./go | bash # cd .. # cd film2 # ./go # does it look ok? # ./go | bash # cd .. for DIR in */; do cd "$DIR" DIRSTEM=${DIR%/} echo "$DIRSTEM ..." { echo "#!/bin/bash" echo "DIRSTEM=\"$DIRSTEM\"" echo "COUNT=0" echo "for SRT in *.srt; do" echo " FILESTEM=\"\${SRT%.srt}\"" # AVI cannot be converted "simply" to MKV because AVI lacks timing information that is needed in the MKV. So we # need some special options ('-fflags +genpts') to get the timing information. Note that video is not reencoded # but it should be if it is not already in H264 format. echo " echo \"ffmpeg -nostats -loglevel 0 -y -fflags +genpts -i \\\"\$FILESTEM.avi\\\" -c:v copy -c:a copy \\\"\$FILESTEM-recontainered.mkv\\\"\"" # Copy over all streams in the first file ('-map 0') and all streams in the second file ('-map 1') echo " echo \"ffmpeg -nostats -loglevel 0 -y -i \\\"\$FILESTEM-recontainered.mkv\\\" -i \\\"\$FILESTEM.srt\\\" -map 0 -map 1 -c copy -metadata:s:s:0 language=eng \\\"\$FILESTEM-recontainered-with-subs.mkv\\\"\"" echo " ((COUNT++))" echo "done" # If the film was in multiple *parts* then combine then. Otherwise create a symlink to the combined name # so that the copy-back-to-pub commandline script/loop doesn't need handle the the different cases. echo "if ((\$COUNT >= 2)); then" echo " echo \"mkvmerge -q -o \\\"\$DIRSTEM-recontainered-with-subs-and-merged.mkv\\\" \$(FIRST=true; for SRT in *.srt; do FILESTEM=\"\${SRT%.srt}\"; { \$FIRST && echo -n \"\\\"\$FILESTEM-recontainered-with-subs.mkv\\\" \"; } || echo -n \"+\\\"\$FILESTEM-recontainered-with-subs.mkv\\\" \"; FIRST=false; done)\"" echo "else" echo " echo \"ln -s *-recontainered-with-subs.mkv \\\"\$DIRSTEM-recontainered-with-subs-and-merged.mkv\\\"\"" echo "fi" } > go chmod 700 go cd .. done