6

I'm trying to batch convert a set of video files where some have aac audio and some have wav, raw PCM or other similar formats.

I'm looking for a way to:

  • Copy the audio stream if it's already aac (-c:a copy)
  • Re-encode the audio stream to aac if it's not already aac (-c:a aac -b:a 160k)

My current approach is to manually identify which files do not have the aac audio stream and convert them separately, but it would be nice to convert all the files together.

Is there a way to insert a conditional statement for audio into an FFMpeg command like the following?

for f in *.mkv; do
    ffmpeg -i "$f" -c:v libx264 -c:a [some magic here] "${f%.*}_new.mkv"
done

2 Answers 2

7

There is no conditional statement in ffmpeg to encode or stream copy if the input is not a particular format. However, you can use ffprobe to determine the audio format then use ffmpeg to encode or stream copy the audio.

Here's a simple script demonstrating how to do this. Copy and save it, give it execute permission with chmod +x yourscriptname, then run it with ./yourscriptname input.foo:

#!/bin/bash

audioformat=$(ffprobe -loglevel error -select_streams a:0 -show_entries stream=codec_name -of default=nw=1:nk=1 "$1")

if [ "$audioformat" = "aac" ]; then
  ffmpeg -i "$1" -c:v libx264 -c:a copy "${1%.*}_new.mkv"
else
  ffmpeg -i "$1" -c:v libx264 -c:a aac "${1%.*}_new.mkv"
fi

You can easily modify this to fit your needs.

1
  • This works perfectly for my needs! Easy to adapt for what I'm working with. Thanks a lot. Commented Feb 18, 2017 at 9:45
2

This is a surprisingly subtle problem. It's taken me quite a while and many passes to solve all the corner cases. I've put my solution online here.

My particular use case is making *.mp4 files for playback on an iPad Air 2 using macOS, but the tools being used are all generally available on Linux, and the solution is readily adapted to other device types.

Here are some of my notes on doing that adaptation, from the project's home page:

The project consists of three Bash shell scripts which drive ffmpeg, ffprobe, mediainfo, and atomicparsley to convert files of any type supported by these programs into H.264 *.mp4 files suitable for playing on an iPad Air 2 class device.

The scripts do contain some macOSisms, so the scripts will need some adjustment for use on Linux, or for use on Windows under Cygwin, MSYS, or WSL:

  1. The rmtrash program acts like rm except that the files are moved to the user's Trash folder. Many similar tools exist for other systems, or you could just replace this line in ipad-h264 with an rm call or some other "move out of the way" type of command.

  2. The open call is a macOS mechanism for opening a file using the GUI program associated with it. I have *.mp4 files associated with Subler here because the next step after remuxing or transcoding is to look at the tags, apply cover art, etc. Under Cygwin, you can get the same effect with cygstart. I assume there is a way to get a similar effect on Linux. Alternately, you could just hard-code the next step in place of my open command.

You can download the software from the repository using the links in the Downloads section of the home page.

1
  • The code at the link you've shared does seem to offer some useful bits to customize for my needs. I'll have to dig into it a bit to see what needs to be modified. I don't expect too many edge cases with the files I'm working with. Commented Feb 18, 2017 at 6:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .