Need help combining commands in to a script to run in Ubuntu

49 Views Asked by At

I have a bunch of video files with DTS audio that I need to convert to AC3. I have found commands that can do different parts of the task but I'm not sure how to put it all together to make a script that works. I have found the following script that will convert the audio from all my files to AC3 however I would prefer for it to only convert the DTS audio files to AC3. The script is as follows.

shopt -s globstar
for f in **/*.mkv; 
do 
    fname="${f##*/}"
    ffmpeg -i "$f" -c:v copy -c:s copy -c:a ac3 "/some/directory/$fname" &&
    mv "/some/directory/$fname" "$f"
done

I have also found this command that will return what sort of audio codec the mkv file is using.

`ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 input.mkv`

So I'm wondering how I can combine the two to create a script that will do what I want it to do. I'm guessing I can add an if function using ffprobe at the beginning of the script? Something like this...

shopt -s globstar
for f in **/*.mkv; 
do 
    fname="${f##*/}"
    if ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of 
       default=nokey=1:noprint_wrappers=1 "$f" | egrep "DTS"; then
       ffmpeg -i "$f" -c:v copy -c:s copy -c:a ac3 "/some/directory/$fname" &&
       mv "/some/directory/$fname" "$f"
    fi
done

Are there any problems with that? I haven't written scripts since I was in high school playing around with my graphics calculator because I was bored so any help would be greatly appreciated.

1

There are 1 best solutions below

0
On

So I think I came up with something that works for me. I'm sure someone that actually knows what they're doing could make this cleaner and more efficient but for anyone that is interested this is what I came up with...

shopt -s globstar
for f in **/*.mkv; 
do 
    fname="${f##*/}"
    if ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 "$f" | egrep "dts"; then
       if [[ $(ffprobe -v error -select_streams a:0 -show_entries stream=channels -of default=nokey=1:noprint_wrappers=1 "$f") > 2 ]]; then
            ffmpeg -hide_banner -loglevel panic -i "$f" -c:v copy -c:s copy -c:a ac3 -b:a 640k "./Temp/$fname" &&
            mv -v "./Temp/$fname" "$f"
       else ffmpeg -hide_banner -loglevel panic -i "$f" -c:v copy -c:s copy -c:a ac3 -b:a 320k "./Temp/$fname" &&
            mv -v "./Temp/$fname" "$f"
       fi
    fi

done
-Just save that to a text file and give it a name (for example DTSconverter.sh)
-Make it executable
-Put it in the root directory where the movies you wish to work on are stored
-Create a folder called 'Temp" in the that same root directory
-Execute the script by opening a terminal in the root directory and typing ./DTSconverter.sh (or whatever file name you gave the script)

This will search for any movies that have a DTS audio track in the directory that the script is placed in, including subdirectories, and transcode the track to AC3. It also accounts for Stereo or Surround tracks and adjusts the bitrate accordingly.