Advanced Video Coding on Linux
The impact of H.264 on the world of digital video compression is growing. Companies such as Apple are already switching wholeheartedly to it. As part of the MPEG-4 standard (part 10), H.264 is now a part of both the HD-DVD and Blu-ray specifications for high-definition DVD. And for good reason—H.264 can encode video using very low bitrates while maintaining an incredibly high perceived quality.
Of particular interest are the low-bitrate possibilities this video codec provides. Luckily for those who run Linux, the H.264 codec (also known as the Advanced Video Codec, or AVC) has a successful and effective open-source implementation known as x264. In fact, the x264 Project won the Doom9 2005 codec comparison test (see the on-line Resources). x264 continues to make progress and improvements, and it remains an active project. So let's take advantage of what it offers us: an extremely high-quality AVC encoding tool that can be used right away for DVD and home movie backups, to create video clips for streaming over the Web or simply for experimenting with the latest video encoding technology.
The balance of this article focuses on the basic steps involved in creating standard .mp4 files that contain H.264 video coupled with AAC audio (Advanced Audio Codec, also an MPEG standard). The vagaries and subtle corners of hard-core video encoding are beyond the scope of this discussion. But hopefully, this introduction will encourage you to explore the topic further.
Because both AVC and AAC are now MPEG standards, it stands to reason that many tools (commercial and otherwise) are already available that support it. For example, Apple's QuickTime natively supports the video files we will be creating. And, MPlayer, the well-known and successful open-source media player, also supports .mp4 playback.
Creating a standards-compliant video file involves three basic steps: the creation of the encoded video, the creation of the encoded audio and the combination of those two things. Here are the software tools we need:
MPlayer (includes mencoder, cvs version 060109 or higher)
faac 1.24 or higher
MP4Box (part of gpac 0.4.0 or higher)
x264 (compiled with gpac support)
Our goal is to produce a low-bitrate video file suitable for posting on the Web. It will be a small file, but the quality will be exceptional compared with a higher-bitrate XviD encoding. Our source video will be a home movie clip called max.dv, which is a nine-second raw DV file captured directly from a digital video camera.
Let's process the audio first, as it is a pretty straightforward operation. The idea is first to have MPlayer dump the raw pcm audio directly from our video source:
mplayer -ao pcm -vc null -vo null max.dv
This produces a file called audiodump.wav. The video portion of the source file is ignored. Now, encode this wave file to AAC:
faac --mpeg-vers 4 audiodump.wav
The --mpeg-vers parameter specifies the MPEG version. We now have the audio portion of our work finished and can listen to audiodump.aac by playing it with MPlayer.
When it comes to encoding the video, we are faced with several options. The highest quality encodes can be made only by using multiple passes. We actually process the source video twice (or more) in order to allow the encoder to pick the best possible distribution of bits across the destination file. Using multiple passes also enables us to pinpoint the bitrate and resulting file size of the output. However, encoding with an AVC encoder, such as x264, is very processor-intensive and thus can run pretty slowly, so we may not want to sit through a lengthy multipass encoding. Instead, we could run the encoding with one pass. This still will produce outstanding results, but never as good as a multipass encode. We also give up the possibility of targeting the resulting file size and bitrate. It all depends on what is most important to you, time or quality.
Fortunately, x264 provides a good middle ground. An option exists to specify a Constant Rate Factor (or Constant Quality), which instructs x264 to take into account the differences between high- and low-motion scenes. Because your eye loses details in high-motion scenes anyway, x264 uses fewer bits in those spots so that it can allocate them elsewhere, resulting in a much improved overall visual quality. This mode allows the highest quality possible without using multiple passes, which is a great time saver. The cost in using this mode, however, is in giving up the ability to determine the final file size and bitrate. Although this is possible with multiple passes, we would be forced to double the encoding time. So for our example, let's stick with one pass, utilizing the Constant Rate Factor feature (--crf) for greatly improved quality. Good values of the Constant Rate Factor range between approximately 18 and 26 (where a lower value produces higher quality but larger file sizes). Your needs in terms of size vs. time vs. quality may be different, however. If so, you should investigate multipass mode further to gain more control.
The x264 encoder accepts only raw YUV 4:2:0 input. To do this, simply pipe the output of mencoder directly into x264:
mkfifo tmp.fifo.yuv
mencoder -vf format=i420 -nosound -ovc raw -of rawvideo \
-ofps 23.976 -o tmp.fifo.yuv max.dv 2>&1 > /dev/null &
x264 -o max-video.mp4 --fps 23.976 --crf 26 --progress \
tmp.fifo.yuv 720x480
rm tmp.fifo.yuv
As you can see, we must specify the framerate (--fps); otherwise x264 will not know what is being fed into it. Do this similarly for the width and height of the incoming raw video. Encoding in this way enables the x264 default encoding parameters, which are quite good, but we can make a few improvements. In particular, we can make general improvements to some of the encoding strategies it uses without sacrificing too much in the way of extra encoding time. The number and variability of the parameters you can feed into x264 is great, and they are all geared toward improving the quality of the resulting output in some way. However, some options are more expensive, time- and processor-wise, than others. And, some options can sacrifice compatibility with certain media players, notably QuickTime. In order to remain compatible with the existing install base of QuickTime users, we need to keep a few things in mind.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- What's the tweeting protocol?
- New Products
- One Hand Slapping
- Readers' Choice Awards
- RSS Feeds
- Reply to comment | Linux Journal
6 hours 51 min ago - Reply to comment | Linux Journal
9 hours 24 min ago - Reply to comment | Linux Journal
10 hours 41 min ago - great post
11 hours 16 min ago - Google Docs
11 hours 38 min ago - Reply to comment | Linux Journal
16 hours 27 min ago - Reply to comment | Linux Journal
17 hours 14 min ago - Web Hosting IQ
18 hours 47 min ago - Thanks for taking the time to
20 hours 24 min ago - Linux is good
22 hours 22 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
A quick ending.
mplayer -ao pcm -vc null -vo null max.dv
MPlayer SVN-r29237-4.4.1 (C) 2000-2009 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Playing max.dv.
File not found: 'max.dv'
Failed to open max.dv.
Exiting... (End of file)
Also tried sudo with the same result.
Son :) max.dv is the name of
Son :) max.dv is the name of the file you're processing. That the name of his (author's) file. Maybe your file has a different name?
Why don't you try replacing the max.dv with the name of your file!?
Cheers!
H264 coding/decoding in hardware?
Hi everybody,
It is very pleasing to see solid and rapidly growing Free Software / open source support for H264 video encoding standard. There are multiple options for doing H264 under GNU/Linux. The completeness of implementation is also getting better - for example handling of interlaced video had been finally improved in 2009 for libavcodec - it decodes *supposedly* H264 specification compliant Mobilygen MG1264 audio/video codec chip produced streams now - you can check MG1264 MP4 samples here - http://www.linuxmedialabs.com/LMLCD/MG1264samples/ - if D1 (full resolution) samples are NOT playing back for you properly it means that your version of libavcodec / mplayer is not recent enough :-) These are full D1 2000 Kbps samples and one 500 Kbps CIF sample, which should playback well everywhere, except from QT player maybe.
Although this article shows the way to use H264 software encoding based on x264 tool, I think it's a good place to ask author and readers about other known Linux H264 hardware devices - for either PCI / PCI-E bus or USB bus - any H264 done in hardware and usable under Linux for integration - with GNU/GPLed drivers like LML26415 PCI card has, or even binary-only non-free drivers or transparent interfaces like RTP/RTSP streams etc. We are building various multi channel video processing systems and having additional choices for H264 standard (720x480/576) and especially high definition video (1920x1080) done in *hardware* for very low CPU overhead is very important. Although hardware design and OEMing is fun thing to do and we enjoy every moment of it :-), sometimes it's desirable to use something that is already available - if only it's Linux friendly. I would appreciate any feedback here or sent to my email vleo@linuxmedialabs.com
V.L.
avi to mp4
I have one question. Is posible to improve the quality of a avi raw, compressing this in mp4? I have seen in some places taht people download the avi raw and convert it to mp4 codec and the result is better than the source (avi raw). I tried to do this with x624 and similar options like you, but the result is the same than the raw.
Thanks and sorry for my english :S
no it is not unless you use
no it is not unless you use some filters, but you have to remember that h264 and xvid aren't lossless codecs so each time you re-encode something using these codecs you lose quality
The two wires crossing the top of the clock tower ?
Hi,
What happened to the two wires which were crossing the top of the clock tower ? You can see them in the DV image but missing in the xvid and x264.
Thanks
--sig
Interlaced Video
I generally use mencoder to convert interlaced 720:480 m2v transport streams to x264 AVIs. I would like to use this method to make Quicktime friendly files, but mencoder says "Skipping Frame!" (which it always does with this video source) which results in mp4 files in which every other frame is blank. Is there an easy way around this? I've played around with the usual pullup and deinterlace filters that I use to make AVIs, but no luck...
Interlaced Video
A solution is to use -of rawvideo with mencoder's native x264 encoder and then mux it all up with MP4Box... If I use the same quicktime-friendly settings I get a quicktime-friendly MP4 out in the end.
An aspect ratio problem
I enjoyed your article, but I think there is a subtle problem with your examples. You start with a DV stream whose natural resolution is 720x480. This seems to be 3:2 aspect ratio, but the stream is designed to play back at 4:3 aspect ratio or 640x480 resolution. This has to do with the legacy of NTSC video having more pixels per inch along a horizontal scan line than vertically across scan lines. I'm not sure, but I don't think H.264 has an aspect ratio flag to account for this issue (MPEG-2 does have such a flag). As a result, I think all of your H.264 output files are stretched horizontally by 720/640. To get the correct result, you probably have to tell the encoder to output a file with a 4:3 aspect ratio. You don't seem to be doing that in the examples.
Specifying aspect ratio, and issues with Quicktime playback
You can set this ratio by specifying the "sample aspect ratio" to x264 using --sar, and also by specifying the "pixel aspect ratio" to MP4Box using :par. I believe these both effect the aspect ratio in the same way. The calculation of the aspect ratio will depend on the properties of the source video clip. More discussion of aspect ratios here. H.264 does have support for aspect ratio signalling, but it of course depends on the decoder to interpret it correctly. For small web clips, it is often best to just resize the source video so that the aspect ratio is 1:1, which will be displayed correctly by any decoder which understands h264.
As one example, Quicktime 7 will not respect these aspect ratio settings, and part of the article deals with issues of compatibility with Quicktime. Although you can set the aspect ratio in the .mp4 file, it will still appear incorrectly when played in the Quicktime player. To fix this problem, open the properties of the video file in Quicktime and manually specify the correct horizontal and vertical dimensions, then save the file (the ability to do this may be restricted to Quicktime Pro). This (non-standard) method will correct the aspect ratio (for Quicktime-playback only).