Optimize video for Youtube on Linux with FFMPEG

On observing a colleague at work with a Macintosh, I was impressed with the FFMPEGX tool, and disappointed that there was no equivalent on Linux. Thankfully, I couldn’t have been more wrong – it’s built around the FFMPEG tool, which is originally for the Unix / Linux platform. You will have to prepare yourself for the command line, but you won’t have the nag screen and waves of brushed metal you get on the Mac.

In my case, I needed to optimise some video for uploading to Youtube. Youtube’s own page lists their optimal format. Crunchgear also suggests a video bitrate.

  • Video codec: MPEG4 (DivX or Xvid are specifically mentioned)
  • Video bitrate: between 700 and 1000 kbps
  • Audio codec: MP3
  • Resolution: 320×240

First, let’s install ffmpeg:

sudo apt-get install ffmpeg

Next, run ffmpeg to figure out what format your source video is actually in. In my case, I was manipulating video coming from a Fujifilm F31FD camera. Here’s what I typed:

$ ffmpeg -i DSCF0370.AVI

And here’s the important part of the output:

Input #0, avi, from 'DSCF0370.AVI':
Duration: 00:03:51.0, start: 0.000000, bitrate: 9350 kb/s
Stream #0.0: Video: mjpeg, yuvj422p, 640x480, 30.00 fps(r)
Stream #0.1: Audio: pcm_u8, 16000 Hz, mono, 128 kb/s

So, we can pretty clearly see that it’s 640×480 MJPEG video at 30fps with 16KHz mono audio. In order to optimize for Youtube, I need to change the resolution and video codec, and possibly also encode the audio to MP3 (but it’s such a low bitrate that it’s not going to make much difference).

Here’s the command line to do that:

$ ffmpeg -i DSCF0370.AVI -b 800 -s 320x240 -vcodec mpeg4 -r 30000/1001 out.avi

To break this down quickly:

  • -i denotes the input file
  • -b denotes the output bitrate
  • -s denotes the output size
  • -vcodec sets the output codec to MPEG4. What comes out is actually a kind of DivX5 file. We could have set this FLV and got the Flash video format that Youtube actually uses, but I’m following the instructions!
  • -r 30000/1001 sets the framerate to 30000/1001 = 29.97003 fps, which is the standard frame rate for NTSC video. Oddly, I couldn’t get the encode to work without this.
  • I could have set the output audio to MP3 using the -acodec mp3 option, but the ffmpeg that comes as standard in the Ubuntu repositories doesn’t support it. Ya boo sucks. The default output is MP2 format – I guess I can live with that.

So – while executing that command, the salient part of ffmpeg’s output looks like this:

Output #0, avi, to 'out.avi':
Stream #0.0: Video: mpeg4, yuv420p, 320x240, q=2-31, 800 kb/s, 29.97 fps(c)
Stream #0.1: Audio: mp2, 16000 Hz, mono, 64 kb/s

… and we can check that plays OK like this:

vlc out.avi

Voila! Now you can upload right into Youtube while preserving a modicum of quality.


Comments

3 responses to “Optimize video for Youtube on Linux with FFMPEG”

  1. […] Оптимизация видео с цифровой камеры для публикации на YouTube. Когда снимался небольшой ролик про NeoGS и Пентагон – при его длительности около 3 минут – размер просто зашкаливал – около 125 мегабайт. Конечно, ни о какой публикации в реальном времени речи нет вообще. Ситуацию можно исправить. Зарубежный коллега приводит вот такой способ предварительной конвертации видео с камеры: $ ffmpeg -i DSCF0370.AVI -b 800 -s 320×240 -vcodec mpeg4 -r 30000/1001 out.avi Полный текст статьи вот здесь. […]

  2. Hello, I wanted to let you know that your code did not work for me until I actually specified a full path for the output, i.e. I had to specify ~/somedir/out.avi instead of just ‘out.avi’.

  3. I have used FFMPEG in a perl script to achieve this.

    $info = `ffmpeg -i $path$file 2>&1 /dev/null`;@fields = split(//, $info);

    And just find out what items in @fields you need to extract.

Leave a Reply

Your email address will not be published. Required fields are marked *