1

Overview:

I'm trying to remux my mostly MKV library to MP4. I have a script that has gone through and extracted subtitles from the video files.

I have another script that will remux any .mkv files in a given folder. What I need is for the script to only remux folders where it detects that there are .srt files.

All the .srt files end with ***.eng.srt

Here's my current remux script:

#!/bin/bash 
IFS=$'\n' 
clear 
for filename in *.mkv 
do 
echo "$filename" 
newfilename="${filename%.mkv}.mp4" 
echo "${newfilename}" 
ffmpeg -fix_sub_duration -i $filename -c:v copy -c:a copy -c:s mov_text $newfilename 
done

Thanks! any help will be greatly appreciated.

0

I think you're looking for this:

#!/bin/bash 

clear
for filename in *.mkv 
do 
    ## Exit if there are no srt files
    [ ! -e *.srt ] && echo "No .srt files found, exiting" && exit
    echo "$filename" 
    newfilename="${filename%.mkv}.mp4" 
    echo "${newfilename}" 
    ffmpeg -fix_sub_duration -i "$filename" -c:v copy -c:a copy -c:s mov_text "$newfilename" 
done

I removed the IFS='\n' since it's not doing anything useful there, your for loop is quite safe even with names containing whitespace as long as you quote your variables.

The [ -e foo ] tests for the existence of foo. Conversely, [ ! -e foo ] is true if there is no foo. In this case, foo is a glob (*.srt) so the test will be false if any matching files are found. In the script above, the && mean that if no srt files are found, the error message will be printed and the script will exit.

  • Awesome! Thanks, seems to be working, any suggestions on adding the ability to delete the .mkv once the mp4 has been remuxed? – Pat Woodward Oct 14 '15 at 0:11
  • @PatWoodward glad it works. As for removing the mkv, just add && rm "$filename" to the end of the ffmpeg line. – terdon Oct 14 '15 at 7:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.