Wednesday, 14 June 2023

batch converting audio codecs only in a folder of videos using ffmpeg

 So you have a folder of videos with a problematic audio format?  In my case i have H264 videos using Apples Audio Codec AAC that will not be compatible to play on any non apple devices like Roku etc.   This is annoying when you have many flavours of devices in your house.

The basic command looks like this:

ffmpeg -i "video.mkv" -c:v copy -c:a ac3 "video-ac3.mkv"

but we need to process many files in a folder so we'll create a loop using the following command:

for i in *.mkv; do ffmpeg -i "$i" -c:v copy -c:a ac3 "${i%.*}-ac3.mkv"; done

for i in *.mkv; do: This part initiates a loop that iterates over all files with the ".mkv" extension in the current directory. The loop variable i represents each file in the loop.

ffmpeg -i "$i" -c:v copy -c:a ac3 "${i%.*}-ac3.mkv";: Within the loop, this command is executed for each MKV file.

-i "$i": This specifies the input file for each iteration of the loop. The $i variable represents the current MKV file.

-c:v copy: This option ensures that the video stream is copied without re-encoding, maintaining the original video quality.

-c:a ac3: This option specifies the audio codec to use for the output file. In this case, ac3 is specified, indicating that the audio stream will be encoded using the AC3 codec.

"${i%.*}-ac3.mkv": This specifies the output file path. The ${i%.*} expression extracts the file name without the extension, and -ac3.mkv is appended to create the output file name. For example, if the input file is "video.mkv", the output file will be "video-ac3.mkv".

The command essentially processes each MKV file in the current directory, copies the video stream, and encodes the audio stream using the AC3 codec. The resulting output files will have the same video streams as the input files, but with the audio streams encoded in AC3 format and appended with "-ac3" in their names.

No comments:

Post a Comment