Tuesday, 25 January 2011

Video for Android or iPhone

Long gone are the days when mobile phones were simply used for making telephone calls.

Nowadays these mini devices that we still call "mobile phones" more closely resemble mini-computers with many many applications from Internet browsing, email, social networking, navigation (GPS and compass based) to entertainment; both audio and visual.

Follows is my guide to converting video for natively supported playback on both Android and iPhones.  That is after following this guide, you should be able to watch the converted videos using any type video player on either your Android or iPhone.

The Target Video Format: H.264
According to the specifications:
  • Android specifications here
  • iPhone specifications here
So both handsets support the H.264 video format out of the box.

OK, but what is this format?  Well, if you really really want to know and have a spare 10 mins, have a look at H.264 on Wikipedia oh and yea I know, it does go on a bit!

Put simply and quickly, it's the format to use for mobile video applications ;o)

Tool to Convert Video to H.264 Format
So we know where we want to get (H.264) how do we get there?

And here is where we get to use my favorite video conversion tool: FFMpeg

FFMpeg is free command-line software licensed under the LGPL or GPL that can be used to convert audio and video with minimal fuss. It has the advantage of being able to convert both video and audio at the same time (rather than splitting video and audio and converting them separately) which means that conversions are quick and perhaps more importantly easy.  Sure it's command-line stuff but believe me; command-line is good!

It also has the advantage that under Windows it doesn't require installation.  The whole tool runs from a single .exe file.

The final plus point is that FFMpeg can used to convert a plethora of different input audio and video formats.  Here is a taster (as run on my Ubuntu laptop):

chris@lappy:~$ ffmpeg -formats
FFmpeg version SVN-r26068, Copyright (c) 2000-2010 the FFmpeg developers
  built on Dec 21 2010 14:01:48 with gcc 4.4.5
  configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab
  libavutil     50.35. 0 / 50.35. 0
  libavcore      0.16. 0 /  0.16. 0
  libavcodec    52.100. 0 / 52.100. 0
  libavformat   52.88. 0 / 52.88. 0
  libavdevice   52. 2. 2 / 52. 2. 2
  libavfilter    1.69. 0 /  1.69. 0
  libswscale     0.12. 0 /  0.12. 0
  libpostproc   51. 2. 0 / 51. 2. 0
File formats:
 D. = Demuxing supported
 .E = Muxing supported
  E 3g2             3GP2 format
  E 3gp             3GP format
 D  4xm             4X Technologies format
 D  IFF             IFF format
 D  ISS             Funcom ISS format
 D  MTV             MTV format
 DE RoQ             raw id RoQ format
  E a64             a64 - video for Commodore 64
 D  aac             raw ADTS AAC
 DE ac3             raw AC-3
  E adts            ADTS AAC
 D  aea             MD STUDIO audio
 DE aiff            Audio IFF
 DE alaw            PCM A-law format
 DE alsa            ALSA audio output
 DE amr             3GPP AMR file format
 D  anm             Deluxe Paint Animation
 D  apc             CRYO APC format
 D  ape             Monkey's Audio
 D  applehttp       Apple HTTP Live Streaming format
 DE asf             ASF format
  E asf_stream      ASF format
 DE ass             Advanced SubStation Alpha subtitle format
 DE au              SUN AU format
 DE avi             AVI format
  E avm2            Flash 9 (AVM2) format
 D  avs             AVS format
 D  bethsoftvid     Bethesda Softworks VID format
 D  bfi             Brute Force & Ignorance
 D  bink            Bink
 D  c93             Interplay C93
 D  caf             Apple Core Audio Format
 DE cavsvideo       raw Chinese AVS video
 D  cdg             CD Graphics Format
  E crc             CRC testing format
 DE daud            D-Cinema audio format
 DE dirac           raw Dirac
 DE dnxhd           raw DNxHD (SMPTE VC-3)
 D  dsicin          Delphine Software International CIN format
 DE dts             raw DTS
 DE dv              DV video format
 D  dv1394          DV1394 A/V grab
  E dvd             MPEG-2 PS format (DVD VOB)
 D  dxa             DXA
 D  ea              Electronic Arts Multimedia Format
 D  ea_cdata        Electronic Arts cdata
 DE eac3            raw E-AC-3
 DE f32be           PCM 32 bit floating-point big-endian format
 DE f32le           PCM 32 bit floating-point little-endian format
 DE f64be           PCM 64 bit floating-point big-endian format
 DE f64le           PCM 64 bit floating-point little-endian format
 DE ffm             FFM (FFserver live feed) format
 D  film_cpk        Sega FILM/CPK format
 DE filmstrip       Adobe Filmstrip
 DE flac            raw FLAC
 D  flic            FLI/FLC/FLX animation format
 DE flv             FLV format
  E framecrc        framecrc testing format
  E framemd5        Per-frame MD5 testing format
 DE g722            raw G.722
  E gif             GIF Animation
 D  gsm             raw GSM
 DE gxf             GXF format
 DE h261            raw H.261
 DE h263            raw H.263
 DE h264            raw H.264 video format
 D  idcin           id Cinematic format
 DE image2          image2 sequence
 DE image2pipe      piped image2 sequence
 D  ingenient       raw Ingenient MJPEG
 D  ipmovie         Interplay MVE format
  E ipod            iPod H.264 MP4 format
 D  iv8             A format generated by IndigoVision 8000 video server
 D  ivf             On2 IVF
 D  jack            JACK Audio Connection Kit
 D  lmlm4           lmlm4 raw format
 D  lxf             VR native stream format (LXF)
 DE m4v             raw MPEG-4 video format
  E matroska        Matroska file format
 D  matroska,webm   Matroska/WebM file format
  E md5             MD5 testing format
 DE mjpeg           raw MJPEG video
 DE mlp             raw MLP
 D  mm              American Laser Games MM format
 DE mmf             Yamaha SMAF
  E mov             MOV format
 D  mov,mp4,m4a,3gp,3g2,mj2 QuickTime/MPEG-4/Motion JPEG 2000 format
  E mp2             MPEG audio layer 2
 DE mp3             MPEG audio layer 3
  E mp4             MP4 format
 D  mpc             Musepack
 D  mpc8            Musepack SV8
 DE mpeg            MPEG-1 System format
  E mpeg1video      raw MPEG-1 video
  E mpeg2video      raw MPEG-2 video
 DE mpegts          MPEG-2 transport stream format
 D  mpegtsraw       MPEG-2 raw transport stream format
 D  mpegvideo       raw MPEG video
  E mpjpeg          MIME multipart JPEG format
 D  msnwctcp        MSN TCP Webcam stream
 DE mulaw           PCM mu-law format
 D  mvi             Motion Pixels MVI format
 DE mxf             Material eXchange Format
  E mxf_d10         Material eXchange Format, D-10 Mapping
 D  mxg             MxPEG clip file format
 D  nc              NC camera feed format
 D  nsv             Nullsoft Streaming Video
  E null            raw null video format
 DE nut             NUT format
 D  nuv             NuppelVideo format
 DE ogg             Ogg
 D  oma             Sony OpenMG audio
 DE oss             Open Sound System playback
  E psp             PSP MP4 format
 D  psxstr          Sony Playstation STR format
 D  pva             TechnoTrend PVA file and stream format
 D  qcp             QCP format
 D  r3d             REDCODE R3D format
 DE rawvideo        raw video format
  E rcv             VC-1 test bitstream
 D  rl2             RL2 format
 DE rm              RealMedia format
 D  rpl             RPL/ARMovie format
 DE rso             Lego Mindstorms RSO format
 DE rtp             RTP output format
 DE rtsp            RTSP output format
 DE s16be           PCM signed 16 bit big-endian format
 DE s16le           PCM signed 16 bit little-endian format
 DE s24be           PCM signed 24 bit big-endian format
 DE s24le           PCM signed 24 bit little-endian format
 DE s32be           PCM signed 32 bit big-endian format
 DE s32le           PCM signed 32 bit little-endian format
 DE s8              PCM signed 8 bit format
 DE sap             SAP output format
 D  sdp             SDP
 D  shn             raw Shorten
 D  siff            Beam Software SIFF
 D  smk             Smacker video
 D  sol             Sierra SOL format
 DE sox             SoX native format
 DE spdif           IEC958 - S/PDIF (IEC-61937)
 DE srt             SubRip subtitle format
  E svcd            MPEG-2 PS format (VOB)
 DE swf             Flash format
 D  thp             THP
 D  tiertexseq      Tiertex Limited SEQ format
 D  tmv             8088flex TMV
 DE truehd          raw TrueHD
 D  tta             True Audio
 D  tty             Tele-typewriter
 D  txd             Renderware TeXture Dictionary
 DE u16be           PCM unsigned 16 bit big-endian format
 DE u16le           PCM unsigned 16 bit little-endian format
 DE u24be           PCM unsigned 24 bit big-endian format
 DE u24le           PCM unsigned 24 bit little-endian format
 DE u32be           PCM unsigned 32 bit big-endian format
 DE u32le           PCM unsigned 32 bit little-endian format
 DE u8              PCM unsigned 8 bit format
 D  vc1             raw VC-1
 D  vc1test         VC-1 test bitstream format
  E vcd             MPEG-1 System format (VCD)
 D  video4linux     Video4Linux device grab
 D  video4linux2    Video4Linux2 device grab
 D  vmd             Sierra VMD format
  E vob             MPEG-2 PS format (VOB)
 DE voc             Creative Voice file format
 D  vqf             Nippon Telegraph and Telephone Corporation (NTT) TwinVQ
 D  w64             Sony Wave64 format
 DE wav             WAV format
 D  wc3movie        Wing Commander III movie format
  E webm            WebM file format
 D  wsaud           Westwood Studios audio format
 D  wsvqa           Westwood Studios VQA format
 D  wv              WavPack
 D  x11grab         X11grab
 D  xa              Maxis XA File Format
 D  yop             Psygnosis YOP Format
 DE yuv4mpegpipe    YUV4MPEG pipe format

Downloading and Installing FFMpeg
I've uploaded the versions of FFMpeg I use to my dropbox:

  • Windows version is here (3.5Mb zip)
  • Compiled Ubuntu Linux version is here (30.6Mb zip)
Feel free to download and use them.  They may not be the latest or greatest, but both versions work perfectly in this application for me.

Installation is simple:
  • Windows: Extract the zip to a folder on C:\ drive. For example C:\Conversion
  • Ubuntu Linux: Extract the zip and install the 2 deb files using sudo dpkg -i filename.deb

Converting Video to H.264 Format
So we know where we are going and what to use to get there.  Lets begin!

Open a command prompt or a terminal session and navigate to the folder containing the file to be converted. For ease on windows, I copy the file to be converted to the folder that also contains ffmpeg.exe.  As mentioned, I use C:\Conversion.

The following commands are the same for both Windows and Linux and for example I'm going to be converting an avi video called "movie.avi".

The Basic Command:
Here's what you need.  This is the killer command!
As is, this command should produce nice quality output video with stereo sound.

ffmpeg -i Movie.avi -acodec libfaac -ac 2 -ab 128k -vcodec mpeg4 -b 900k -r 25 Movie.mp4
Agreed this looks like a scary hairy command-line, but it isn't really.  OK, so what did all that mean?  Here is the breakdown:

-i = input file [in our case Movie.avi]
-acodec = output audio codec to use for conversion [in our case libfaac]
-ac = output audio channels [in our case 2 - stereo!] [*]
-ab = output audio bitrate [in our case 128k] [*]
-vcodec = output video codec to use for conversion [in our case mpeg4]
-b = output video bitrate [in our case 900k] [*]
-r = output frames per second [in our case 25] [*]
Movie.mp4 = name of the output file

[*] - Tweak-able values.  See 'Tweaking' section below.

Conversion off and running:

As a starter for ten, and as video conversion can take a while, I normally let the conversion run for 30 seconds or so, and then press "q" to quit encoding.

I then copy the resulting file to the phone just to double check that the phone will indeed play the converted file, an all looks good.  The last thing you want to do is wait around for the encoding to finish and then find out that the video wont play on the target phone.   

If all is good, just up arrow and hit return to kick off the conversion again, overwriting the previous output file.

On occasion I have found an issue where the output video won't play.  As far as I can tell it is due to the video size being too big for the phone.  In this case you need to look reduce the output frame size.  Here is how:

Look at the output from original test conversion, specifically where FFMpeg tells you about the input video:

You are looking for the input video frame size. In the screenshot above it is 608 wide x 464 high.

If encoding for an iPhone, you are looking to drop these numbers to less than 640 wide x 480 high (Android varies depending on individual devices).

From there I usually breakout the calculator and equally divide numbers down.  Remember whatever you divide one number by (one third, two thirds, half, etc) divide the other number by the same amount. Don't just take an arbitrary number off of the original width and height.  Thats when you start getting strange stretched or compressed output videos!

The additional switch to specify output file width and height is -s.  Rolling this into the original command:

ffmpeg -i Movie.avi -acodec libfaac -ac 2 -ab 128k -vcodec mpeg4 -b 900k -r 25 -s 200x400 Movie.mp4

Remember these:
-ac = output audio channels [*]
-ab = output audio bitrate [*]
-b = output video bitrate [*]
-r = output frames per second [*]

[*] - Tweak-able values.

Yes you guessed it, these values can be tweaked to your hearts desire!  Be that to gain a better quality output file, or to reduce the H.264 output filesize down (at the expense of quality).

To save time, here are some examples.

Medium Quality Video, Stereo Sound:
ffmpeg -i Movie.avi -acodec libfaac -ac 2 -ab 128k -vcodec mpeg4 -b 500k -r 25 Movie.mp4

Low Quality Video, Mono Sound:
ffmpeg -i Movie.avi -acodec libfaac -ac 1 -ab 16k -vcodec mpeg4 -b 200k -r 13 Movie.mp4

Top Quality Video, Quality Stereo Sound*:
ffmpeg -i Movie.avi -acodec libfaac -ac 2 -ab 160k -vcodec mpeg4 -b 1500k -r 30 Movie.mp4
These are the maximums for the iPhone 3GS (iPhone 4 is different). *Be careful with the -b value as FFMpeg treats this as minimum, so will typically look to encode video above this number. If output video doesn't play, tweak -b value down so that encoded output video bitrate doesn't go above 1500. 

Acceptable Video, Stereo Sound (The standard command from above):
ffmpeg -i Movie.avi -acodec libfaac -ac 2 -ab 128k -vcodec mpeg4 -b 900k -r 25 Movie.mp4

So there you have it.  You can now convert your videos for native playback on Android or iPhones.

Feel free to drop me a comment below if you have any questions and I'll do my best to help.

Happy portable viewing.

- Chris

Newer Post Older Post Home