I can't get an ffmpeg encoded libx264 video in a .mp4 container to work with media9 and beamer. The pdf is created using pdfLaTeX. It seems to be some kind of ffmpeg issue since the following work fine:

  • The random.mp4 from the media9 package works
  • So is the .flv default in ffmpeg though the video quality is naturally not as good

    ffmpeg -i Test.mp4  -sameq Test.flv 

Any video I encode in ffmpeg using

ffmpeg -i EB_%03d.png -c:v libx264 -x264opts keyint=30 -r 30 -b:v 1000k -s 640x512 Test.mp4

plays flawless in e.g. VLC (and I see the duration bar in the pdf though not the video). ffmpeg -i Test.mp4 yields for the video stream

Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 640x512, 1021 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc

which is slightly different from ffmpeg -i random.mp4

Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 170 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc

Output from ffmpeg

ffmpeg version 2.0.2 Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 19 2013 08:34:03 with gcc 4.7 (SUSE Linux)
configuration: --shlibdir=/usr/lib --prefix=/usr --mandir=/usr/share/man 
--libdir=/usr/lib --enable-shared --disable-static --enable-debug --disable-stripping 
--extra-cflags='-fomit-frame-pointer -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 
-fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -D_LARGEFILE_SOURCE 
-D_FILE_OFFSET_BITS=64 -I/usr/include/gsm' --enable-gpl --enable-x11grab --enable-version3 
--enable-pthreads --enable-avfilter --enable-libpulse --enable-libvpx --enable-libopus 
--enable-libass --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libspeex 
--enable-libxvid --enable-libx264 --enable-libschroedinger --enable-libgsm --enable-libopencore-amrnb 
--enable-libopencore-amrwb --enable-postproc --enable-libdc1394 --enable-librtmp --enable-libfreetype 
--enable-avresample --enable-libtwolame --enable-libvo-aacenc
  libavutil      52. 38.100 / 52. 38.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.100 / 55. 12.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 79.101 /  3. 79.101
  libavresample   1.  1.  0 /  1.  1.  0
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100

The code snippet for media9

\frametitle{Here should be a .mp4 video}
\framesubtitle{Apparently, something is wrong with my x264 codec}


Ideas anybody?

  • Embedded random.mp4 plays in AR, but Test.mp4 doesn't? Can you make the original *.png available for download somehow? – AlexG Oct 31 '13 at 20:15
  • ubuntuone.com/15GDUTXBbYp7KHpCzUW0k8 some 7 Mb. Hope this helps! – Nuttin Wrong Oct 31 '13 at 20:25
  • Same problem here. There seems to be a problem with ffmpeg rather than with the x264 library. – AlexG Nov 1 '13 at 12:56


Choosing the right libx264 options with ffmpeg / avconv is a bit tricky.

The following ffmpeg command line should produce usable video files (adjust frame -r ... and keyframe -g ... rates to your needs):

ffmpeg -i <input> -vf scale="trunc(iw/2)*2:trunc(ih/2)*2" -c:v libx264 -profile:v high -pix_fmt yuv420p -g 25 -r 25 output.mp4

<input> to be replaced by a video file to be transcoded, or a PNG/JPEG/... file sequence with wildcards, e.g. frame-%d.png for numbered files frame-0.png, frame-1.png etc.

Replace ffmpeg with avconv, if the latter is installed. Both share the same options.

  • Thanks for clearing that up. I'll have a look at libav soon, in the mean time, HandBrake works (a crude re-encode). – Nuttin Wrong Nov 1 '13 at 19:15
  • Thanks, I edited my question tex.stackexchange.com/questions/67663/…? What option do you use to adjust the frame rate or to compress more (at the expense of quality). – alfC May 20 '15 at 8:25
  • @alfC: Frames per second is specified with -r ..., the number of key frames with -g .... I usually take the same number as with the -r option to have one key frame per second. Keyframes come into play when trying to seek within the video using the scrub-bar of whatever player component (e. g. jw-player) is used or the arrow keys of the keyboard with VPlayer.swf. Flash Player can only seek between key frames. As for quality, i have relied on the default settings of libx264 so far; it should be adjusted by setting the bitrate option. – AlexG – AlexG May 20 '15 at 8:45
  • @alfC See section 'Additional Information & Tips' in trac.ffmpeg.org/wiki/Encode/H.264 on bitrate and compatibility options. – AlexG May 20 '15 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.