Encoding Video for Android

Android can decode H.263 and H.264 video and AAC audio using MPEG-4 and 3GP containers. Android 2.3 and higher can play WebM files. While WebM is an open and patent-free format, H.264 is often still preferred for backwards compatibility and hardware acceleration on existing hardware.

If your source is a DVD, skip to HandBrake section

Using Arista Transcoder

The Arista Transcoder is an extremely easy-to-use application that uses GStreamer to convert nearly any video format to Preset output formats. There are a number of Presets for specific Android devices, creating files that are the highest-possible quality supported by the device (normally H.264/AAC at the screen resolution of the device). This application works for DVDs, though only for the main title on the disc.

Supported operations

Source

Support Matrix

Workaround

Individual file

SUPPORTED

DVD

PARTIAL

Only for main title. Extract other DVD titles to .VOB files before conversion, using a different program (such as mplayer --dumpstream).

Instructions

  1. Install the 'arista' package from the Ubuntu Universe repository.
  2. Click "Create Conversion".
  3. Choose the source file, destination directory and Android Preset that matches your device.
  4. Enqueue other files if required by repeating the above.
  5. Wait for the file(s) to complete conversion.

Using Avidemux (GUI)

Supported operations

Source

Support Matrix

Workaround

Individual file

SUPPORTED

Instructions

This method has been tested in Ubuntu 9.04, adapted from this article. It will produce a movie encoded with mpeg4 codec, fast but not optimal.

  1. Install the avidemux package from the Ubuntu multiverse repository
  2. Configure video options:
    1. Click on the video dropdown on the left and select MPEG-4 ASP (lavc)
    2. Click Configure->Encoding Mode->Single Pass - Bitrate

    3. Enter in any bitrate up to 512 to be safe, then click Ok.
    4. Click Filters->MPlayer Resize->Double Click on it-> Width:432, Height:320 if source video is standard resolution.

      1. For widescreen source, choose Width:480, Height:272.

      2. Click Ok, then close.
  3. Configure audio options:
    1. Click Audio->AAC (FAAC)

    2. Configure->Bitrate 96

    3. Click Format->MP4

  4. Click Save and specify a name.mp4 for your video, encoding will start
  5. Once enconding has completed, plug in your phone via USB and transfer the video to it, anywhere. The videos can be opened directly using a file manager such as Astro.

Using WinFF (GUI)

This method will achieve superior compression using a newer video codec and higher level of control.

http://winff.org/images/screenshots/winff044-ubuntu-small.jpg

Supported operations

Source

Support Matrix

Workaround

Individual file

SUPPORTED

Install WinFF on Ubuntu 9.04 or later using by clicking on the following link: apt:winff. For older version of Ubuntu, please click on the link for the instructions: Instructions for WinFF installation

Instructions

  • Click on Options and verify that Additional Options are selected -- they will show up on the bottom of the screen
  • Click on Add and select one or multiple movies that you want to convert.
  • Select Convert to: Ipod
  • Select Device Presets: H.264 for Ipod *Linux* (4:3) for standard resolution or H.264 for Ipod *Linux* (16:9) for wide resolution
  • In the Additional Options below, choose Video Settings and type Video Size: 432 x 320 for standard resolution and 480 x 272 for wide resolution
  • In Audio Settings: If you are planning to listen primarily with headphones, do nothing. If you are planning to listen on the speakerphone, select Audio Channels: 1 to create mono sound.
  • Click on Convert toolbar icon.

Using HandBrake (GUI)

http://trac.handbrake.fr/wiki/LinGuiScreenshots

Supported operations

Source

Support Matrix

Individual file

SUPPORTED

DVD

SUPPORTED

Installation

Summary of instructions from https://launchpad.net/~handbrake-ubuntu/+archive/ppa.

Ubuntu 9.10 karmic and later: add ppa:handbrake-ubuntu/ppa to your system's Software Sources.

Ubuntu 9.04 and earlier: Copy and paste the following command into the terminal to add the new WinFF repository (example for JAUNTY, SUBSTITUTE WITH YOUR VERSION):

echo "For 9.04 and earlier versions only!"
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com A58BCAE3
echo "deb http://ppa.launchpad.net/handbrake-ubuntu/ppa/ubuntu jaunty main" | sudo tee /etc/apt/sources.list.d/handbrake.list
sudo apt-get update && sudo apt-get install handbrake-gtk

Video Conversion Instructions

  1. When encoding a DVD, select File, "/dev/dvd". Name might be different.
  2. When encoding a file, click on Source and select a desired source file.
  3. Click on Preset: Apple -> iPhone & iPod Touch

  4. Click on Video tab. In Quality, reduce quality to 49 and uncheck Constant Rate Factor
  5. Click on Start.
  6. Once encoding has completed, plug in your phone via USB and transfer the video to it, anywhere. The videos can be opened directly using a file manager such as Astro.

Preset File for Import

Tested on HTC Hero: To import, choose "options" at the bottom right of the window, then import. Android_Tested_on_Hero.plist

Expert mode: Using ffmpeg (command line)

Supported operations

Source

Support Matrix

Individual file

SUPPORTED

DVD

NOT SUPPORTED

Introduction

This method will give you the greatest control of the resulting output.

For most versions of Ubuntu, ffmpeg in Medibuntu contains this "unlocked" ffmpeg. Enable Medibuntu with the instructions here.

MPEG4 (standard) encoding

The default encoding of the videos is MPEG-4 ASP (i.e. "xvid") format video using ffmpeg's mpeg4 encoder. If you are interested in higher quality encodings, which will take quite a bit more time but may result in better quality, see H.264 section below.

To convert a file source-video.avi to a desired format, run the following command:

ffmpeg -i source-video.avi -s 480x320 -vcodec mpeg4 -acodec aac -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 output-video.mp4

This is VERY basic. For full options, go to FFmpeg Documentation

  • -s is "size" of output video.
  • -vcodec Choices for Android are mpeg4 and h263. IMHO I get better quality and marginally smaller file size with mpeg4.
  • -acodec aac is the audio codec. I know of no other choice here for this purpose.
    • Older versions of ffmpeg called this option -acodec libfaac.
  • -ac 1 Number of audio channels. Use 1 to save filesize since you only have one speaker. Unless you like wearing those painful earbuds all the time. Then use -ac 2.
  • -ar 16000 Audio sampling rate in Hertz. Some formats won't work with anything other than 8kHz. 16 kHz seems okay to me, considering what I'm watching it on. Don't expect HiFi from a phone.

  • -r 13 Frames per second. Larger is smoother, but 30 fps requires double the file size of 15 fps. 12 is about the slowest my eyes can comfortably watch. Since DVDs are right at 25 FPS, 12.5 being half of this, I go with 13. My camera makes 30 FPS; in that case, use -r 15.
  • -ab 32000 bitrate of the audio -- 32 kbps in this case. Normally I'd balk at such low numbers, but remember, I'm trying to keep filesize low. I'm not broadcasting to a large audience with this, am I? No, it's for me. For you? Use your best judgement. Experiment a little.
  • -aspect 3:2 Obviously, this is the aspect ratio of the output file. I'm not sure it matters, when the output size is being set before.

While ffmpeg is running, you should see the following:

Output #0, mp4, to 'terminal.mp4':
   Stream #0.0: Video: mpeg4, yuv420p, 480x320 [PAR 1:1 DAR 3:2], q=2-31, 200 kb/s, 13.00 tb(c)
   Stream #0.1: Audio: aac, 16000 Hz, mono, s16, 32 kb/s

H.264 Encoding

H.264 (MPEG-4 AVC) is a newer codec that Android supports. It's able to deliver similar quality at half the bitrate of MPEG-4 ASP! However, it is an extremely slow and CPU-intensive encoder. Expect the encode to take 5x longer than mpeg4 or xvid. You also need ffmpeg with x264 support.

Due to HTC Dream limited feature support for H.264, the command for doing the encode is that much more complex. If you are interested in technical details, HTC Dream supports only the Constrained Baseline Profile (CBP) dialect of H.264; videos that use more advanced features of H.264 play incorrectly, or not at all. In case of incorrect video encoding, the most typical error is a black screen during playback on HTC Dream while the audio plays.

Note that since we are striving for better quality, the audio encoding is also set to higher standards. Assuming source movie has aspect ratio 4:3, the command is:

ffmpeg -i source-video.avi -s 432x320 -b 384k -vcodec h264 -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -subq 7 -trellis 1 -refs 5 -bf 0 -flags2 +mixed_refs -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 -acodec aac output-video.mp4

Some versions of ffmpeg use older syntax, that is, -vcodec libx264 and -acodec libfaac.

For example of a typical American camcoder which records at 640x480 resolution with 30 frames per second rate, we will resize image and change the rate (-r 15):

ffmpeg -i myparty.avi -s 432x320 -b 384k -vcodec h264 -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -subq 7 -trellis 1 -refs 5 -bf 0 -flags2 +mixed_refs -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 -acodec aac -r 15  myparty.mp4

While ffmpeg is running, you should see the following:

Output #0, mp4, to 'myparty.mp4':
  Stream #0.0: Video: h264, yuv420p, 432x320, q=10-51, 384 kb/s, 15.00 fps(c)
  Stream #0.1: Audio: aac, 44100 Hz, stereo, 64 kb/s

Encoding for Streaming

In order to encode the resulting mp4 files for streaming, you can use the qt-faststart utility, also from the ffmpeg package.

qt-faststart input.mp4 output.mp4

Expert mode: Using mencoder (command line)

Supported operations

Source

Support Matrix

Individual file

SUPPORTED

DVD

SUPPORTED

Introduction

These instructions are only applicable to encoding DVD tracks into HTC Dream compatible video format. This is not a preferred method due to its complexity. Please read a chapter about H.264 above in the FFmpeg description.

Installation

Upgrade to the latest version of software using the following PPA repositories: https://launchpad.net/~rvm/+archive/mplayer and https://launchpad.net/~rvm/+archive/smplayer.

Additionally, install gpac package from universe repository.

Conversion

The following command a track number 10 into an AVI file. As mentioned previously, the source has aspect ratio of 4:3, so we choose 432x320 as output most appropriate for the HTC Dream screen.

mencoder dvd://10 -dvd-device /dev/dvd -aid 128 -o output.avi -ovc x264 -x264encopts bitrate=640:nocabac:direct_pred=auto:me=umh:frameref=2:level_idc=21:partitions=all:subq=6:threads=auto:trellis=1:vbv_maxrate=768:vbv_bufsize=244:bframes=0 -oac faac -faacopts br=96:mpeg=4:object=2 -channels 1 -srate 48000 -vf scale=432:320,harddup

Warning: Currently, mencoder is not able to produce MP4 containers usable by mobiles players. Do not use the following mencoder option: -of lavf -lavfopts format=mp4.

After that, convert AVI to MP4 containers using these instructions.

Alternatively, use MP4Box utility from gpac package which strictly complies to MPEG4 standard.

MP4Box -aviraw video output.avi 
MP4Box -aviraw audio output.avi
mv output_audio.raw output_audio.aac
MP4Box -add output_video.h264 -add output_audio.aac output.mp4
rm output.avi

Plug-in your phone via USB and copy output.mp4 to your Android mobile device.

Get help

As always, Ubuntuforums is a great place to ask for help. Please use the Multimedia Production category.

Primary source

Inspired by

Additional information


AndroidVideoEncoding (last edited 2011-11-17 00:03:55 by luna-tick)