Using ffmpeg on Ubuntu to convert DV videos for video sharing websites

To begin with, this is the problem that I had. I had recorded a short clip using my Panasonic NV-GS330 camcorder. I had to upload it to Vimeo, my video sharing website of choice.

From the camcorder to the computer

Importing the video from the camcorder to the laptop was not much of a problem. I plugged in the camcorder to my laptop using a firewire card. Then I used Kino to position the tape (the camcorder uses mini dv tapes) just before the beginning of the clip I wanted to import and hit the capture button. Soon, the dv capture file for the clip was lying in my home directory.

Figuring out the output format

What is your format of choice for uploading to the video sharing website? Regardless of the format you use, all video sharing website will re-encode it to the video standard(or quality) that they use to serve the clips to the viewers. To avoid changing too many things in this re-encoding, which decreases quality, it is always better to go with the recommendations of the video sharing website. Luckily, vimeo has quite a detailed page about the characteristics of the video they would like their users to upload. This makes it easy for me, so I note it down:

Using ffmpeg

Now comes the tough part - using the non-linear video editor ffmpeg and it’s millions of knobs :) . Actually, in it’s simplest form ffmpeg is really really easy to use! Just using ffmpeg -i input.dv -vcodec h264 output.avi will do the job for you. But it is not going to give you the requirements that you had just drawn up. For one, the default bitrate is 200kbps, which will give you terrible video. Also, I have video in 16:9 format, as my camcorder records in widescreen, and I have to convert the video into 4:3 640x480 format. The right way to do it, so that you don’t have to crop off any video data, is to convert the video into a letterbox form, which ffmpeg can do very effectively.

Resizing video from 16:9 to 4:3 (letterboxing) using ffmpeg

ffmpeg has a -s (width)x(height) parameter which can let you resize the video. To preserve the aspect ratio for my video in the 4:3 format with 640 being the final width of the video, my video would first need to be scaled to the required width as it is the larger of the two dimensions. Then black bars would be added to the height to make it add up to 480. The magic resizing size is 640x360 (-s 640x360), with 40 pixels each, above and below the video (-padtop 60 -padbottom 60), to reach 480 in the vertical dimension. The padding color is usually black (-padcolor 000000) but you can also go with white to make the video merge into the usually white background of the websites. Black, to me, gives quite a retro feel to the video ;) and so I would go with that.

The actual command line

There is a huge list of switches that can be used in ffmpeg to get the video of your choice. The advanced ones require you to understand intimately how video compression works. A lot of optimization is possible which can get you the best results for the nature of the video. However, I decided to keep my learning curve as short as I could and came up with a configuration which works for me.

/usr/bin/ffmpeg -threads 2 -f dv -i input.dv -vcodec h264 -g 30 -deinterlace -b 900k -s 640x360 -padtop 60 -padbottom 60 -padcolor 000000 -aspect 4:3 -acodec mp3 -ab 64k output.avi

An easier way

Of course, there are numbers here that you would like to tweak for your needs, and therefore I have created this little bash wrapper script to make it really easier for you to tweak it. Edit the config values at the top of the script to suit your taste. You can also configure the metadata (like author name, copyright, etc.) of your videos. Remember to at least change these, unless you want me to own the copyrights to your uploaded videos ;).

This is the advantage of using a linear video editing tool like ffmpeg. It is so easy to script it for regular uses!

Tweaking your file size and quality

With the configuration values earlier, I was able to convert a 28MB DV file(about 8 seconds in duration, these files are big!) to an excellent 1MB AVI file. With a video bitrate of 500kbps however, this came down to about 600kb and an acceptable video quality. You should tweak with the video bit rate to see which one gives you the best trade off for your video.

General
Draft of Indian Government's policy on Open Standards in e-governance released Blog split

Comments

comments powered by Disqus