Converting various video formats to mp4

Updated Reverend Jim 1 Tallied Votes 2K Views Share

I have a Chromecast that I use to stream video from my laptop to our TV. It works great, however, until VLC (Videolan) releases their promised upgrade with Chromecase support, I am limited to streaming files in the mp4 format. Fortunately, ffmpeg allows me to convert other formats to mp4. Unfortunately, figuring out what parameters to use can be daunting. So far I have determined the commands to use to convert avi, flv and mkv. The last two are a simple repackaging of the contents (takes but a few seconds). The first, avi, requires a complete recoding. The following commands perform the conversion to mp4 with no loss in quality.

ffmpeg -y -i in.avi -c:v libx264 -crf 17 -preset slow -c:a aac -strict -2 -b:a 192k -ac 2 out.mp4
ffmpeg -y -i in.flv -vcodec copy -acodec copy out.mp4
ffmpeg -y -i in.mkv -vcodec copy -acodec copy out.mp4

But who wants to have to remember all that and type it in every time. That's why I coded up some vbscript to do it for me. It can be run from the command line (supports wild cards), or you can drag and drop a file onto a shortcut to the script. Copy the code and save it in a file with a name like ConvertVideo.vbs. Before running it you should do the following (only necessary to do this once)

Open a command shell as Administrator and type

cscript //nologo //h:cscript //s

This sets cscript.exe as the default script engine. If you don't do this then you will get a MSGBOX popup window for every console write.

It should be simple to add converters for other file formats. Just add another cmds entry with the correct command line options and modify the helpinfo text. If you figure out extra formats please post the command line here.

'                                                                                       '
'   Name:                                                                               '
'                                                                                       '
'       cv.vbs                                                                          '
'                                                                                       '
'   Description:                                                                        '
'                                                                                       '
'       Converts various video formats to mp4. Specify the file(s) to convert on the    '
'       command line or drag and drop a file onto a shortcut to this script. Ffmpeg is  '
'       used to do the actual conversion. Currently supported input formats are flv     '
'       and avi. Conversion is done while maintaining the quality of the ooriginal      '
'       file. In the case of flv files, all that is really required is a repackaging    '
'       of the flv into an mp4 wrapper. As such, conversion takes just a few seconds.   '
'       On the other hand, avi files require a complete conversion so converting even   '
'       a one hour video could take up to 30 minutes.                                   '
'                                                                                       '
'       Currently, when I try to ChromeCast avi or flv (by dropping onto a Chrome       '
'       session, all that happens is that the file is downloaded from one folder into   '
'       my "downloads" folder. Onlt mp4 files play properly. Until the next major       '
'       release of vlc media player (which is supposed to support direct casting to     '
'       ChromeCast) I'll have to convert.                                               '
'                                                                                       '
'		Note: when specifying a filename on the command line, wildcards "*" and "?"     '
'       may be used to convert more than one file at a time.                            '
'                                                                                       '
'   Audit:                                                                              '
'                                                                                       '
'		2016-01-24	rj	added time stamp to file name output & added mkv option			'
'       2015-10-13  rj  original code                                                   '
'                                                                                       '

'Template ffmpeg commands for various input formats. FILE will be replaced with the		'
'video file name without the extension. To add more formats add another dictionary     	'
'entry with the extension as the key and the value as the ffmpeg command.               '

set cmds = CreateObject("Scripting.Dictionary")

cmds.Add "avi", "ffmpeg -y -i ""FILE.avi"" -c:v libx264 -crf 17 -preset slow -c:a aac -strict -2 -b:a 192k -ac 2 ""FILE.mp4"""
cmds.Add "flv", "ffmpeg -y -i ""FILE.flv"" -vcodec copy -acodec copy ""FILE.mp4"""
cmds.Add "mkv", "ffmpeg -y -i ""FILE.mkv"" -vcodec copy -acodec copy ""FILE.mp4"""

'Create worker objects

set fso = CreateObject("Scripting.FileSystemObject")
set wso = CreateObject("Wscript.Shell")	
set arg = Wscript.Arguments

'If no arguments then show brief help

if arg.Count = 0 Then
	help = "\n" _
	     & "cv filename\n\n" _
	     & "  Convert video file(s) to mp4. * and ? wildcards supported.\n"   _
	     & "  Current input formats are avi, flv & mkv. Existing output files\n" _
	     & "  will be overwritten."
	Wscript.Echo Replace(help,"\n",vbCrLf)
end if

'Set working directory to input file folder

path = fso.GetParentFolderName(arg(0))
if path <> "" then wso.CurrentDirectory = path

'Generate all matching file names. This is done by executing the shell DIR
'command and grabbing the output. Doing this lets me pass off the wildcard
'processing to DIR rather than doing it myself with a regular expression.

set dir = wso.Exec("cmd /c dir """ & fso.GetFileName(arg(0)) & """ /b")

do	'process all matching files

	start = Now

	file = dir.StdOut.ReadLine			'file name from dir command '
	base = fso.GetBaseName(file)		'file name with no extension'
	extn = fso.GetExtensionName(file)	'file extension             '
	'convert known formats
	if cmds.Exists(extn) Then
		Execute "Convert Replace(cmds(extn),""FILE"",base)"
		Progress "  Elapsed time " & TimeSerial(0,0,DateDiff("s",start,Now))
		Wscript.Echo extn & " format not supported"
	End If
loop until dir.StdOut.AtEndOfStream

'Pause script if running in Windowed mode (from drag & drop)

Function Pause
	if Windowed Then
		Wscript.Echo "Press enter to continue"
	End If
End Function

'Show progress if running in non-Windowed (command line) mode

Function Progress (text)
	If not Windowed Then
		Wscript.Echo text
	end if
End Function

'Returns True if running in Windowed (drag & drop) mode

Function Windowed
	Windowed = instr(1,Wscript.FullName,"wscript",1) > 0
End Function

'Run the ffmpeg conversion command in a separate visible thread and wait.

Function Convert (cmd)
	wso.Run cmd, 1, True
End Function
rubberman 1,355 Nearly a Posting Virtuoso Featured Poster

I also use ffmpeg for this stuff. I find that this works well for most all video formats:

ffmpeg -i "input file" -target ntsc-dvd -q 0 -async 20 "output file name".

This will create an mpg file. The -async 20 will sync the audio every 20 ms which works well to keep audio in sync with "the lips". :-) -q 0 will keep the output video quality close to the input quality. It will also transform non-standard audio formats to ac3.

This will work with most any input video format.

Reverend Jim 3,993 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I should also have mentioned that ffmpeg can be downloaded from here.

Bella_2 0 Newbie Poster

As for me, I prefer using online converter that can do the format conversion on webpage directly without any costs. Also the video quality is also wonderful.

Reverend Jim 3,993 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I'm a do-it-yourself kinda guy and I don't like having to trust some web site not to keep a copy of my videos.

Backgroundsun 0 Newbie Poster Banned

I also think that it is better to use online video converters. Now I want to try this tool What else know?

gladisfogson 0 Newbie Poster

It would be great if you create some app or website with your converter, sure people would use it, especially it doesn't influence video quality, so it's amazing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.