How to best convert Flash compatible mp4 files with FFMPEG?

断了今生、忘了曾经 提交于 2019-12-02 14:15:34

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.

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

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.

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

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.

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+

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!