How to best convert Flash compatible mp4 files with FFMPEG?
Asked Answered
M

8

17

I am trying to convert different files to a flash compatible .mp4 file with ffmpeg, but I can't seem to get it to work. Of course the objective is to get the greatest quality with the smallest file size.

So far I have this, which works, but it doesn't play in a flash player for some reason. The result isn't that great, how can I improve this conversion?

This is the command I'm using:

ffmpeg -i input.file -f mp4 -vcodec mpeg4 -r 25 -b 560000 -s 610x340 -acodec aac -ac 2 -ab 64 -ar 44100 output.file
Moneylender answered 17/1, 2009 at 14:13 Comment(0)
A
14

To create the interleaving metadata (which will allow for mid stream resumes, and rewinds/fforwards), use the gpac utilities (included in medibuntu for example) to re-interleave the file. Convert to mp4 as follows, with FFmpeg version 0.5 or better:

ffmpeg -f mp4 -i video.mov -b 400k video.mp4

then

/usr/bin/MP4Box -inter 500 video.mp4

Tada! Done! This will stream properly in JW Flv or other flash players.

Armistead answered 20/4, 2009 at 17:4 Comment(3)
MP4Box > installing an AIR app just to move the meta-data to the start of the file :)Ellison
Does qt-faststart in ffmpeg's source tree not work? Also, you really want to use -vcodec libx264 -vpre hq.Decretive
@Derek Anderson: The extra mp4box command can now be replaced by '-movflags +faststart' in the ffmpeg command line.Garfieldgarfinkel
F
4

FLV and MP4 are media containers.

MPEG-4 part 2 and H.264 are video codecs. (and H.264 gives much better quality)

libx264 is an encoder for H.264 codec.

mpeg4 is an encoder for MPEG-4 part 2 codec.

Flash can only play video codec H.264 in FLV container.

So the params should be like that:

-f flv -vcodec libx264
Flybynight answered 6/3, 2009 at 10:19 Comment(3)
Exactly which statement is no longer true?Flybynight
exactly, flash can actually play .mp4 containers on Player 9 Update 3 (9,0,115,0)Lightfoot
Additionally, cramming h.264 codec video into flv files is problematic (no seeking) and Adobe themselves don't recommend doing it.Meunier
T
4

In my snippet collection I have the following for this task.

ffmpeg first pass:

ffmpeg -y -i input.mp4 -pass 1 -vcodec libx264 -b 2000k -g 300 -bf 3 -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 -sc_threshold 40 -flags +loop -cmp +chroma -me_range 16 -me_method umh -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 -directpred 3 -flags2 +dct8x8+wpred+bpyramid+mixed_refs -trellis 1 -partitions +parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 -acodec libfaac -ab 128k output.mp4

ffmpeg second pass:

ffmpeg -y -i input.mp4 -pass 2 -vcodec libx264 -b 2000k -g 300 -bf 3 -refs 6 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 -sc_threshold 40 -flags +loop -cmp +chroma -me_range 16 -me_method umh -subq 7 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 -directpred 3 -flags2 +dct8x8+wpred+bpyramid+mixed_refs -trellis 1 -partitions +parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 -acodec libfaac -ab 128k output.mp4

The quality setting are 2000k = video bitrate, 128k = audio bitrat and 300 = GOP. Don't ask me what the other parameters are. :)

Interleaving with MP4Box:

MP4Box -inter 500 output.mp4

Create a thumbnail / poster image (play around with 5 to obtain your desired image)

ffmpeg -itsoffset -5 -i output.mp4 -vcodec mjpeg -vframes 1 -an -f rawvideo -s 1280x720 thumbnail.jpg

Hope this helps.

Translucent answered 18/5, 2010 at 11:44 Comment(0)
K
3

I use a sequence of commands to do this, starting with mencoder. (see script below). The trick is that ffmpeg's (and mencoder's) mp4 file output is not generally flash compatible due to the way it's interleaved and they don't offer good control over this behavior. So, I produce an AVI file and then use mp4box to remux it the way I want. Here's my complete script, that expects to be given an .avi file, and produces a flash-playable .mp4 file.

#!/bin/bash
#
ORIGDIR=`pwd`
FILE=$@
VID_OUTPUT=${FILE/.AVI/.tmp.avi}
FINAL_OUTPUT=${FILE/.AVI/.mp4}
MENCODER=~/src/mplayer/svn/mplayer/mencoder
BITRATE=500
RES=640:480
LOG=mencoder.$FILE.log

TEMPDIR=`mktemp -d /tmp/transcode.XXXXXXXX`
pushd $TEMPDIR
echo "Pass 1"

PARAMS=keyint=250:bframes=0:qp_min=10:qp_max=51:turbo=1:mixed_refs=0:frameref=1:deblock=0,0:qblur=0:vbv_bufsize=10000:vbv_maxrate=10000:keyint_min=25:me=hex:me_range=16:ip_factor=1.4:pb_factor=1.3:chroma_qp_offset=0:vbv_init=0.9:ratetol=1.0:cplx_blur=20:nocabac:noweight_b:nob_pyramid:partitions=p8x8,b8x8,i4x4:no8x8dct:nossim:deadzone_inter=21:deadzone_intra=11

EXTRA_PARAMS=subq=5:trellis=0:

$MENCODER $ORIGDIR/$FILE -o $VID_OUTPUT -oac faac -srate 44100 -ovc x264 -x264encopts bitrate=$BITRATE:$PARAMS:pass=1 -vf scale=$RES 2>&1 >> $LOG
echo "Pass 2"
$MENCODER $ORIGDIR/$FILE -o $VID_OUTPUT -oac faac -srate 44100 -ovc x264 -x264encopts bitrate=$BITRATE:$PARAMS:pass=2 -vf scale=$RES 2>&1 >> $LOG
MP4Box -aviraw video $VID_OUTPUT
MP4Box -aviraw audio $VID_OUTPUT
TEMP_AUDIO=${VID_OUTPUT/.avi/}_audio.raw
AAC_AUDIO=${TEMP_AUDIO/.raw/.aac}
H264_VIDEO=${VID_OUTPUT/.avi/}_video.h264
echo TEMP is $TEMP_AUDIO
echo AAC is $AAC_AUDIO
echo H264 is $H264_VIDEO
mv $TEMP_AUDIO $AAC_AUDIO
MP4Box -add $H264_VIDEO:fps=29.97 -add $AAC_AUDIO $FINAL_OUTPUT
mv $LOG $ORIGDIR
mv $FINAL_OUTPUT $ORIGDIR
popd
echo Files left in $TEMPDIR
# rm -fr $TEMPDIR
Kawai answered 6/3, 2009 at 23:3 Comment(0)
D
2

If you're using ffmpeg to make .mp4s, look in the ffpresets/ directory and use -vpre. The default options (for all codecs, but especially libx264) are very bad and practically all of them should be set to something custom. -vpre takes care of most of it for x264.

Decretive answered 28/4, 2009 at 0:43 Comment(0)
D
1

Actually you shouldn't be using maxrate, especially not a maxrate of 10000k when you have your bitrate explicitly set to 200k. In fact looking at this even closer I really don't think you understand what most of that stuff is for :)

For starters you're calling the input file output.mp4, plus the output file (which should actually be /dev/null for pass 1) is called yourinfile.avi and you haven't actually set a container format so what you would end up with is an h264 encoded avi file.

Try this for high quality 1080p HD flash compatible h264 encoded MPEG-4 videos, first create a file with the following contents in the same directory as the video you're encoding and name it something like flash-mp4.ffpreset

vcodec=libx264
b=5000k
acodec=libfaac
ab=256k
ac=2
ar=44100
coder=1
flags=+loop+mv4
cmp=+chroma
partitions=+parti8x8+parti4x4+partp8x8+partb8x8
me_method=umh
subq=8
me_range=16
g=250
keyint_min=25
sc_threshold=40
i_qfactor=0.71
b_strategy=2
qcomp=0.6
qmin=10
qmax=51
qdiff=4
bf=3
refs=4
directpred=3
trellis=1
flags2=+wpred+mixed_refs+dct8x8+fastpskip
wpredp=2
fflags=+rtphint
profile=1

then from the command line:

ffmpeg -y -i sourcefile.avi -fpre ./flash-mp4.ffpreset -f mp4 outputfile.f4v

That should play perfectly with flash 10+

Disseise answered 16/9, 2011 at 4:32 Comment(0)
O
0

Just some ideas of why this might happen

  • mp4 is only supported in flash 9.0.115.0 or higher, what version are you using?
  • does the webserver hosting the file have the mp4 mime type setup?

does it play when converting to FLV and is that an option? You would then use something like the following options

ffmpeg -i input.file -f flv -r 25 -b 560000 -s 610x340 -acodec mp3 -ac 2 -ab 64 -ar 44100 output.file
Operand answered 17/1, 2009 at 17:39 Comment(1)
Of course, i could convert it to FLV but i would rather use h.264 with mp4 to get the best possible quality to size ratio. The server handles preconverted mp4 files so there should be no problem there, so the problems is in my ffmpeg conversion string.Moneylender
Y
0

I found this (very verbose, i'm not sure what half of it even does) snippet somewhere on the web. It's made for doing two passes, so it won't output any video on the first pass, you'll need to set it to do -pass 2 to run the second pass and output the actual video.

ffmpeg -y -i output.mp4 -vcodec libx264 -acodec libfaac -ab 12000 -ac 2 -b 200000 -threads 2 -flags +loop -cmp +chroma -partitions 0 -subq 1 -trellis 0 -refs 1 -coder 0 -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -maxrate 10M -bufsize 10M -rc_eq \’blurCplx^(1-qComp)\’ -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -s 304x224 -pass 1 yourinfile.avi

To get progressive playback during download you will also need to move the metadata to the start of the file. ffmpeg puts this at the end and flash needs this before it can start playing. A useful tool for this is QTIndexSwapper

Yatzeck answered 17/1, 2009 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.