A quick and dirty solution for an annoying problem: How to use ffmpeg to copy the video stream from one file and the audio stream from another

No idea if this will be useful to anybody, but just in case…

I downloaded a video the other day, Final Girl (from here), and for some annoying and unknown reason, it doesn’t play on my TV. So, I converted it from AVI to MP4 using ffmpeg (I use the static build downloaded from here). Problem is, my crappy conversion loses the audio quality. The downloaded file has six channel audio, and is great quality. (The movie is not so great though. I should’ve read the reviews.)

So here’s my quick and dirty fix:

  1. Convert to mp4 with crappy audio.
  2. Use my hacky batch file, that uses ffmpeg again, to copy the video stream from the converted file, and the audio stream from the original file, into a new file.

The parameters used in the first conversion are not that important. The important part is coming later. Basically, I could have used a batch file that looked something like this:

@echo off
pushd %~p1

G:\Tools\ffmpeg\ffmpeg.exe -i "%~nx1" -q:v 1.0 -an -y "%~n1.mp4"


popd

(I wrote most of my batch files years ago. My “standard pattern” for writing batch files is as above. Switch echo off so it doesn’t echo the commands; “push” the current directory to the directory of the first parameter – and I always have all relevant files in the same directory; do some shit; then “pop” to the original directory. In the old days that meant I could then call another batch file, and always knew in which directory I would end up. My old batch files included GOTO statements to handle errors and could be chained so they’d call each other, or be called from a batch file that made use of a few other batch files, so pushd and popd was crucial for flow control. Actually I don’t use batch files for anything complicated anymore because they are a pain in the arse, but still use the pattern.)

That will convert to MP4 using default settings, without sound. (-an tells it not to convert the audio.) Not actually what I did… I have an application of my own that generates the command line. Also not important, but included for interest. My program only supports certain ffmpeg video/audio conversions and certain audio/video filters… quite a few actually, including converting between all the formats I know of, changing aspect ratio to fix it for files that set it wrong, changing contrast, increasing or decreasing volume, removing noise, cropping, logo-blurr and so on. But it doesn’t know how to fiddle with streams and some other shit, so I still use batch files for that. (The reason this command-line is calling ffmpeg64.exe is that I have both builds – renamed so as not to conflict – in my application’s directory, and my code launches the process of the relevant one depending on whether it’s running on 32 bit or 64 bit Windows.) The actual command-line used was this:

G:\Documents\Visual Studio 2012\Projects\RomyView\RomyView\bin\Debug\ffmpeg64.exe"  -i "G:\Videos\movies\Final.Girl.2015.BRRip.XviD.AC3-EVO.avi" -c:v libx264 -vprofile Baseline -preset Veryfast -b:v 367.35k -maxrate 2500k -bufsize 1200k -threads 0 -acodec libvo_aacenc -b:a 128k -r 23.976 -ar 44100 -ac 2 -y "G:\Videos\movies\Final.Girl.2015.BRRip.XviD.AC3-EVO.mp4"

The important part follows:

This batch file, which I then used to copy the video from the new file and the audio from the old file, looks like this:

@echo off
pushd %~p1

REM Copy the video stream from parameter one, and the audio stream from parameter two.

G:\Tools\ffmpeg\ffmpeg.exe -i "%~nx1" -i "%~nx2" -map 0:0 -map 1:1 -c:v:0:0 copy -c:a:1:0 copy -y "%~n1 streams copied.MP4"

popd

i.e. I opened a command prompt, and just typed out a line to the effect of:
call G:\Tools\CopyStreamsMp4.cmd “Path_To_New_File” “Path_To_Old_File”

Edit: Correction – I’d copied the wrong batch file here. (I have a few for slightly different uses.) The second input parameter is supposed to be “%~nx2“. I’d copied one of my versions that’s used simply to copy the streams to a different but compatible container file (itunes to standard MP4), which used the same parameter for both, that is the same file for both streams.

Advertisements

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in How To. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s