FFmpeg to the Rescue: Muxing Audio and Video Files

So, there I was downloading videos from YouTube to include in a PowerPoint for a training course I was producing. Unfortunately, my download tool, wondershare’s excellent video converter, downloaded separate audio and video streams, the video in an MP4 wrapper and the audio as an MP3 file. So I had to mux the two to input them into PowerPoint.

A quick Google search led to a definitive StackExchange article here. I quickly surmised that I had to accomplish two things.

  • Convert the MP3 file to AAC since MP4 files can’t include MP3 audio
  • Mux the H.264-encoded video and newly AAC-encoded audio into a single file

I tried the following command string which worked the first time.

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac muxed.mp4

To explain, after designating the two input files, the script copies the existing video file as-is (c:v copy), converts the MP3 audio to aac (c:a aac), and muxes the two into muxed.mp4.

Then I tried this:

ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a copy muxed.mp4

Interestingly, FFmpeg converted the MP3 audio to AAC and muxed the file without so much as an informational message. Just for fun, I converted audio.mp3 into a wav file and tried this:

ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a copy muxed.mp4

As before, FFmpeg converted the WAV file to AAC and produced the muxed file. So, FFmpeg seems pretty error-tolerant with this command.

Of course, if you want to specify the bitrate (-b:a 128k), sample rate (-ar 48000), or channels (-ac 2, for stereo), you should specify the AAC codec (-c:a aac) as below.

ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -b:a 128k -ar 48000 -ac 2 muxed.mp4

If you need to swap out audio from an existing file with new audio, check the StackExchange article.

Want to Learn More about FFmpeg?

If you’re looking for a fast and easy way to learn FFmpeg, check out the 2018 Edition of my book, Learn to Produce Video with FFmpeg in 30 Minutes or Less. This edition updates the book to FFmpeg 4.0, and includes.

  • Packaging to DASH and HLS formats with open-source tool Bento4
  • How to create a hybrid HEVC/H.264 encoding ladder for HLS deployment
  • How to encode and package VP9 files into DASH format with FFmpeg 4.0
  • How to transcode inputs into multiple files simultaneously using the H.264, HEVC, and VP9 codecs for live ABR streaming
  • Encoding to the Alliance for Open Media AV1 format with FFmpeg 4.0

The update costs $34.95 in print and $29.95 for a downloadable PDF. Click here for more information on the book.

About Jan Ozer

Avatar photo
I help companies train new technical hires in streaming media-related positions; I also help companies optimize their codec selections and encoding stacks and evaluate new encoders and codecs. I am a contributing editor to Streaming Media Magazine, writing about codecs and encoding tools. I have written multiple authoritative books on video encoding, including Video Encoding by the Numbers: Eliminate the Guesswork from your Streaming Video (https://amzn.to/3kV6R1j) and Learn to Produce Video with FFmpeg: In Thirty Minutes or Less (https://amzn.to/3ZJih7e). I have multiple courses relating to streaming media production, all available at https://bit.ly/slc_courses. I currently work as www.netint.com as a Senior Director in Marketing.

Check Also

Meet Open Source Video Downloader YT-DLP

I’m updating my book, Video Encoding by the Numbers, which means lots of research into what other encoding professionals are doing. One …

Simplify Your Workflow: Command-Line Variables in FFmpeg Batch Files

Creating batch files with variables is one of the more efficient ways to run FFmpeg. …

Navigating Rate Shaping and Zero Rating: Key Takeaways from Qwilt’s Findings

Many streaming publishers focus on the top rung of their encoding ladders and let the …

Leave a Reply

Your email address will not be published. Required fields are marked *