24 October 2018

I had been searching for an easy solution to combine a video file with it’s separate audio file but simply Googling that gives a bunch of results that require downloading software from questionable places.  I then had the idea to find out if it was possible with FFMPEG (a lot of software uses it anyway).  And it turns out it is, with a very simple one liner.  I threw it in some Powershell to make it easy to use.

A graphical file selection for both video and audio files.  Be sure to update the path to the ffmpeg executable to wherever you put it and whatever starting directory you want for the files.  I wrote it so the output just goes to the same directory where the files are but you can easily change that.  If you are going to work with files other than mp4 and m4a, then update (or just remove the line) with the filter. Also, because the video alone is an mp4 and the output will be an mp4, I randomly chose to insert a “2-” in the front of the filename if I don’t type one in at that starting prompt. By default it just adds that to the name of the video file, for the combined output file.

Thanks to this page for the ffmpeg command line: https://kwizzu.com/construct.html

Add-Type -AssemblyName System.windows.forms | Out-Null
$outputFilename = Read-Host "Enter Output Filename"

$ffmpeg = "C:\ffmpeg-4.0.2-win64-static\bin\ffmpeg.exe"

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.showHelp = $true
$OpenFileDialog.initialdirectory = "C:\Users\me\Downloads\"
$OpenFileDialog.filter = "Videos (*.mp4)| *.mp4"
$OpenFileDialog.ShowDialog() | Out-Null
$vidPath = $OpenFileDialog.FileName

If(!$outputFilename) { $outputFilename = "2-$($OpenFileDialog.SafeFileName)"}
$outputFilePath = "C:\Users\me\Downloads\$outputFilename"

$OpenFileDialog.filter = "Videos (*.m4a)| *.m4a"
$OpenFileDialog.ShowDialog() | Out-Null
$audioPath = $OpenFileDialog.FileName

Start-Process -FilePath $ffmpeg -ArgumentList "-i ""$vidPath"" -i ""$audioPath"" -c:v copy -c:a copy ""$outputFilePath"""

Read-Host "Press any key to exit"

This works especially well for YouTube videos or others where you have to download them as separate files. The best part about this is that it is FAST. I tried it first with the built in video creator for Windows 10, it’s slow and it took a 400MB video file and 40MB audio file and created a 1.4GB combined file… wow. With ffmpeg the output file is just the size of the two combined.

You can easily automate this if you have a lot of work. You can make the script constantly run on your machine, monitor a folder every so often, etc… and have it automatically detect two files of the same name (sans extension) and merge them. Lots of ways to expand this.


There are no comments.



You must be logged in to post a comment.

Links

RSS 2.0 Feed

Support

Brave Rewards
This site supports Brave Rewards. Please consider tipping or adding it to your monthly contributions if you find anything helpful!

For other ways: Support

Support this blog! If you have found it helpfu you can send me crypto. Any amount is appreciated!
ETH: 0xBEaF72807Cb5f4a8CCE23A5E7949041f62e8F8f0 | BTC: 3HTK5VJWr3vnftxbrcWAszLkRTrx9s5KzZ | SHIB: 0xdb209f96dD1BdcC12f03FdDfFAD0602276fb29BE
Brave Users you can send me BAT using the browser.