Wednesday 26 November 2014

How to build the ultimate media converter (no experience required)

How to build the ultimate media converter (no experience required)

Introduction and the basics


It's the first rule of multimedia: files are never in quite the format you need. Maybe an application doesn't support a video, it won't play on your mobile, you get a picture, but no sound – there are all kinds of potential problems.


You could go searching for free software to convert video, audio or image files from one format to another – but it'll take a while, and you might have to try out several packages, many of which come with a stack of annoying adware.


What's more, a lot of this freeware doesn't actually carry out any conversions at all. Instead, many packages just act as a shell for the open source FFMPEG. You choose a file or two, and they get FFMPEG to do the actual work.


Fortunately, there's an alternative: forget the freeware, download FFMPEG yourself and just use that directly.


This won't be an option for everyone. FFMPEG is a command line tool, and it'll take a little thought and time to get it set up correctly – but don't let that put you off. Considering the complexity of what it's doing, FFMPEG has to be one of the easiest command line tools to use, and you'll be able to use many of its most important capabilities in just a few seconds.


ffmpeg documentation


Getting started


FFMPEG is available for download from the project site. There are various builds for Windows, Mac and Linux – 32 or 64-bit, static or shared – which is great if you know what you need. But if you're not sure, and just want something basic to try out on a PC, go to Zeranoe, then download and unzip the latest 32-bit static build.


Later, you might try out FFMPEG by double-clicking ff-prompt.bat in the folder you've just unzipped. But first you'll need to learn a few commands, and they can be as simple as this:


ffmpeg -i source.mov destination.mp4


That's the basis for video conversion, right there: FFMPEG will read the first (MOV) file name and export it as an MP4.


You need an FLV instead? Just use that as the extension:


ffmpeg -i source.mov destination.flv


No complex command line switches are required, at least not yet – FFMPEG is smart enough to figure out the file format you need from its extension. And it supports a very wide range of formats, too, including 3GP, AVI, FLV, MPG, MKV, OGV and more (FFMPEG's online documentation has the full list).


But it gets better, because the program doesn't only work with videos. FFMPEG can also convert audio files from one format to another:


ffmpeg -i source.mp3 destination.ogg


Again, all you need to do is provide an appropriate destination extension, like AAC, FLAC, MP3, OGG, WAV or WMA and FFPEG will convert to or from that format.


There's support for converting images:


ffmpeg -i source.jpg destination.png


This works with BMP, JPG, PNG, TIFF and others (again, the official documentation has the full list).


Of course you don't have to manually enter these commands every time, either. Most of our examples can be embedded in a script, making conversions as easy as a drag and drop (we'll cover a few batch file basics later).


ffmpeg local help


Video slideshows


Simple format conversion is useful, but FFMPEG really starts to get interesting when you combine format types.


Here, for example, we're converting a video to an audio file:


ffmpeg -i video.mov audio.mp3


It's exactly the same syntax, the same rules (use the audio extension to define your export format), only this time the program is taking the soundtrack from our movie and saving it (without re-encoding, if possible) to the destination.


Another interesting option allows us to convert videos into animated GIFs:


ffmpeg -i video.mov animated.gif


That needs to be used with care – the GIF will be huge unless your source clip is low resolution and just a few seconds long – but it's still potentially very useful.


It's not much more difficult to extract the individual frames from a video, although again we'd only do this with relatively small and short files:


ffmpeg -i video.mov frame%d.jpg


Here FFMPEG is saving every frame from our video as a series of JPEGs, frame1.jpg, frame.2.jpg, frame3.jpg and so on.


If you'd really like to get creative, then it's even possible to create a video from scratch by using other source files. The basic principles are easy enough to understand:


ffmpeg -i picture.jpg -i music.mp3 myvideo.mp4


This time we're making a video with a single still image and a soundtrack (much like those YouTube clips with a song and a picture of the artist).


You can even create full video slideshows from a sequence of images. This can become a lot more complicated as you need to manually define the video details, but in principle you might use something like this:


ffmpeg -framerate 1 -pattern_type glob -i '*.jpg' -c:v libx264 out.mp4


Here we're building a video slideshow from sequential images and saving it as an MP4. (There's much more to this, and advanced users should check out the FFMPEG wiki for details.)


Going further


We've concentrated very much on the basics thus far, but that's not always powerful enough. What if we want to change the resolution of the output video? Use a different audio codec? Ignore the first few seconds of the video, maybe just convert five seconds from somewhere in the middle? None of this is any problem, but it's going to require a little more work.


Resizing a video is a key first step when you'd like to play it on a mobile device, and once again FFMPEG has several tools to help. The most powerful is the scale filter:


ffmpeg -i video.mpg -vf scale=720:480 squashed.mp4


Here our video file is being re-encoded at a resolution of 720 x 480. That's fine, unless of course your input video is a different aspect ratio, in which case it'll look squashed or stretched. To avoid problems, use -1 as one of the scale values, like so:


ffmpeg -i video.mpg -vf scale=720:-1 aspect.mp4


This time FFMPEG sets the output width to 720 pixels, then calculates the height to match its aspect ratio, which should mean it looks just fine.


Trimming unwanted footage is another way to cut file size, and it's also fairly easy:


ffmpeg -ss 5 -t 30 -i video.mpg out.mp4


The -ss 5 option tells FFMPEG to start converting from around the 5 second point of the input video (this may not be exact – it depends on keyframes and other issues), and -t 30 indicates that you only want the next 30 seconds converting.


This should now be producing good results, but if your mobile device can't play the video at all then it could mean you need to use another codec. Exactly what's best will depend on your hardware and the source movie, but you could start with something like one (not both) of these:


ffmpeg -i test.mov -vcodec mpeg4 -acodec mp3 recoded2.mp4


ffmpeg -i test.mov -vcodec h264 -acodec aac recoded2.mp4


The first command sets our output video codec to MPEG4, and audio to MP3, very standard settings. The second creates an Apple-friendly H.264 video using AAC audio, as long as there's a suitable AAC encoder available (if there's a problem, FFMPEG will give you an alternative encoder – like libvo_aacenc – and all you have to do is use that instead of 'aac'). Both are very standard settings and should play on most modern devices.


If there are still problems or further tweaks you want to make, then it's just a matter of finding those options in the official documentation, and using them in your FFMPEG command. (We've considered options individually here, just for clarity, but FFMPEG will take as many as you can fit on the command line.)


Here's an all-in-one example:


ffmpeg -ss 5 -i test.mov -vf scale=640:-1 -vcodec mpeg4 -b:V 2000k -acodec mp3 -b:a 128k recoded.mp4


You should now be able to see that we're trimming the first five seconds, resizing the video, setting new video and audio codecs and defining new bitrates: not bad at all for a single line. It's still not exactly convenient to enter that every time you need to do something, of course, but that can be avoided with a little work.


ffmpeg all in one


Drag and drop conversions


FFMPEG's various options aren't difficult to understand, as we've seen. And while it's not naturally easy to use, creating a few batch files can make a real difference.


To begin, add the FFMPEG BIN folder to your system's PATH, so that Windows can find it. In Explorer, right click This PC, select Properties > Advanced system settings > Environment Variables, double click "Path" in the System Variables list, and add a semi-colon and your BIN folder to the end of the current path (C:\PROGRAM FILES (X86)\Something would become C:\PROGRAM FILES (X86)\Something;C:\FFMPEG\bin).


To test this, open a command prompt, type ffmpeg and press [Enter]: if you see help on FFMPEG syntax, rather than "ffmpeg is not recognised..." then it's worked just fine.


With the preparations complete, use Notepad to create a batch file called To-MP4.bat, containing the following line:


ffmpeg -i %1 %1.mp4


Now drag and drop any video onto that file and it'll launch FFMPEG with your source file as a parameter, like "ffmpeg -i c:\video\dragged.mov c:\video.dragged.mov.mp4". Your new file will appear in the same folder as the source, with the same name, and an MP4 extension, without you having to type anything at all.


Create additional batch files as required, replacing MP4 with some other extension, for whatever other conversions you need.


This approach is simple, but limited, as it only converts one file at a time. To work with a group of files, use a batch file like this:


for %%a in (*.avi) do ffmpeg -i "%%a" "%%a".mp4


Place this file in a folder containing your source AVIs, double click it, and just wait for any conversions to finish.


There's a lot more scope for batch file trickery here, but even these core basic steps will take you a long way. Use different extensions, add extra commands, string them together to create your own scripts, maybe use Task Scheduler to run automatically, and you'll soon have the ultimate in media conversion toolkits – with no other software required.
















http://ift.tt/1uHpbCU

No comments:

Post a Comment