Linux: Convert .AVI File To Apple iPod / iPhone MP4 [M4V] Format

last updated in Categories , ,

I store all avi files on my Debian / Ubuntu powered Linux server. How do I convert all avi files and play on an iPhone using Linux command line options? How to play AVI files on an iPhone or iPod?

You need to install and use ffmpeg command. It is a very fast video and audio converter that can also grab from a live audio/video source. It can also convert between arbitrary sample rates and resize video on the fly with a high quality polyphase filter.



The syntax is as follows:

ffmpeg  -i "input.avi" "output.mp4"
ffmpeg -b bitrate -i "input.avi" "output.mp4"
ffmpeg -i input.avi -acodec libfaac -ab 128k -ar 44100 -vcodec mpeg4  -b 1250K output.m4v
ffmpeg [other-options] -b bitrate -i "input.avi" "output.mp4"
ffmpeg [other-options] -b bitrate -i "New Year Party.avi" "New Year Party.mp4"


  • -i input.avi : Input file name.
  • -b bitrate : Set the video bitrate in bit/s (default = 200 kb/s).
  • output.mp4 : Output file.


Type the following command to convert japantrip_01.avi to japantrip_01.mp4 format:
$ ffmpeg -b 1250k -i japantrip_01.avi japantrip_01.mp4
Sample outputs:

ffmpeg version 0.7.13, Copyright (c) 2000-2011 the FFmpeg developers
  built on Jun 13 2012 14:14:09 with gcc 4.4.5
  configuration: --enable-libdc1394 --prefix=/usr --extra-cflags='-Wall -g ' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-avfilter --enable-libdirac --disable-decoder=libdirac --enable-libfreetype --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-libvpx --enable-librtmp --extra-libs=-lgcrypt --disable-altivec --disable-armv5te --disable-armv6 --disable-vis
  libavutil    50. 43. 0 / 50. 43. 0
  libavcodec   52.123. 0 / 52.123. 0
  libavformat  52.111. 0 / 52.111. 0
  libavdevice  52.  5. 0 / 52.  5. 0
  libavfilter   1. 80. 0 /  1. 80. 0
  libswscale    0. 14. 1 /  0. 14. 1
  libpostproc  51.  2. 0 / 51.  2. 0
[avi @ 0x19c7800] max_analyze_duration 5000000 reached at 5011200
Seems stream 0 codec frame rate differs from container frame rate: 47.95 (5994/125) -> 23.98 (24000/1001)
Input #0, avi, from 'japantrip_01.avi':
    encoder         : MEncoder VERSION
  Duration: 00:23:09.63, start: 0.000000, bitrate: 856 kb/s
    Stream #0.0: Video: h264 (Main), yuv420p, 848x480 [PAR 427:424 DAR 427:240], PAR 61440:61003 DAR 2048:1151, 23.98 fps, 23.98 tbr, 23.98 tbn, 47.95 tbc
    Stream #0.1: Audio: mp3, 44100 Hz, stereo, s16, 320 kb/s
[buffer @ 0x19c72a0] w:848 h:480 pixfmt:yuv420p tb:1/1000000 sar:61440/61003 sws_param:
[mpeg4 @ 0x19cbde0] Invalid pixel aspect ratio 61440/61003, limit is 255/255 reducing
Output #0, mp4, to 'japantrip_01.mp4':
    encoder         : Lavf52.111.0
    Stream #0.0: Video: mpeg4, yuv420p, 848x480 [PAR 141:140 DAR 2491:1400], q=2-31, 200 kb/s, PAR 61440:61003 DAR 2048:1151, 24k tbn, 23.98 tbc
    Stream #0.1: Audio: libfaac, 44100 Hz, stereo, s16, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
  Stream #0.1 -> #0.1
Press [q] to stop, [?] for help
frame=   86 fps=  0 q=31.0 size=     260kB time=00:00:03.58 bitrate= 593.1k
frame=  172 fps=171 q=31.0 size=     438kB time=00:00:07.17 bitrate= 500.4k
frame=  264 fps=175 q=31.0 size=     633kB time=00:00:11.01 bitrate= 470.7k
frame=  373 fps=185 q=24.8 size=     827kB time=00:00:15.55 bitrate= 435.2k
frame=33132 fps=193 q=31.0 size=   62626kB time=00:23:01.88 bitrate= 371.3k
frame=33226 fps=193 q=31.0 size=   62815kB time=00:23:05.80 bitrate= 371.3k
frame=33319 fps=193 q=31.0 Lsize=   63702kB time=00:23:07.90 bitrate= 376.0
kbits/s dup=1 drop=0    
video:52144kB audio:10819kB global headers:0kB muxing overhead 1.175113%

To list and verify both files, enter:
$ ls -l
$ file japantrip_01.avi
$ file japantrip_01.mp4

Sample outputs:

Fig.01: ffmpeg output
Fig.01: ffmpeg output

Avi To m4v format

Type the following command
$ ffmpeg -i input.avi -acodec libfaac -ab 128k -ar 44100 -vcodec mpeg4 -b 1250K output.m4v
$ file output.m4v

Sample outputs:

output.m4v: ISO Media, MPEG v4 system, version 2

Avi To MOV format

Type the following command

ffmpeg -i "input.avi" -acodec libmp3lame -ab 192 ""


  • -acodec libmp3lame : Force audio codec to libmp3lame.
  • -ab bitrate : Set the audio bitrate in bit/s i.e. to 192k (default = 64k).

Not a fan of command line tools?

Try handbrake application. It is an open-source, cross-platform (OS X / Linux/Unix / BSD / MS-Windows), multithreaded video transcoder software application.

Fig.02: Handbreak GUI App For AVI to M4V (iPhone / iPOD) Format Conversion
Fig.02: Handbreak GUI App For AVI to M4V (iPhone / iPOD) Format Conversion

How do I sync files to my iPod or iPhone?

You need to use native Apple iTunes application. See how to run iTunes under Linux operating system for more information.


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

8 comment

  1. ipad2:

    ffmpeg -y -i "$1" -acodec aac -strict experimental -ar 48000 -ab 128k -ac 2 -vcodec libx264 -b 1200k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+partp8x8+partb8x8 -subq 7 -trellis 1 -refs 5 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 1200k -maxrate 1200k -bufsize 1200k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -r 30 -g 90 -aspect 16:9 -s 1024x768 -threads 0 -async 2 ~/ipadvideos/"$1".mp4


    ffmpeg -y -i "$1" -acodec aac -ar 48000 -ab 128k -ac 2 -s 1024x576 -vcodec libx264 -b 1200k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+partp8x8+partb8x8 -subq 7 -trellis 1 -refs 5 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 1200k -maxrate 1200k -bufsize 1200k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 4:3 -r 30 -g 90 -async 2 -strict experimental ~/hptouchpad/"$1".mp4


    ffmpeg -y -i "$1" -acodec aac -ar 48000 -ab 128k -ac 2 -s 1024x576 -vcodec libx264 -b 1200k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+partp8x8+partb8x8 -subq 7 -trellis 1 -refs 5 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 1200k -maxrate 1200k -bufsize 1200k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 16:9 -r 30 -g 90 -async 2 -strict experimental ~/hptouchpad/"$1".mp4
  2. -b bitrate parameter

    fix this parameter at a very high value, for example -b 6000k

    otherwise the movie will be in a low resolution and a very low quality image.

  3. I find that the HandbrakeCLI is much easier to use and works just as well as ffmpeg for most day to day stuff. Also, you can combine it with the ‘find’ command using the -exec switch to recursively go through directories converting video. I use it on those large AVIs I get on my camera.

    Still, have a question? Get help on our forum!