views:

7781

answers:

8

I am trying to convert different files to a flash compatible .mp4 file with ffmpeg, but i cant seem to succeed. Of cource the objective is to get the greatest quality with the smallest file size.

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

ffmpeg -i input.file -f mp4 -vcodec mpeg4 -r 25 -b 560000 -s 610x340 -acodec aac -ac 2 -ab 64 -ar 44100 output.file
A: 

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
olle
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.
Espen Christensen
A: 

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

grapefrukt
+2  A: 

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
tst
This is no longer true.
ko-dos
Exactly which statement is no longer true?
tst
I think he means h.264 works in mp4, not just flv.
Jegschemesch
exactly, flash can actually play .mp4 containers on Player 9 Update 3 (9,0,115,0)
gonxalo
Additionally, cramming h.264 codec video into flv files is problematic (no seeking) and Adobe themselves don't recommend doing it.
Stu Thompson
+2  A: 

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
slacy
+5  A: 

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.

MP4Box > installing an AIR app just to move the meta-data to the start of the file :)
Legooolas
Does qt-faststart in ffmpeg's source tree not work?Also, you really want to use -vcodec libx264 -vpre hq.
alex strange
+2  A: 

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.

alex strange
A: 

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.

kitsched
A: 

any final conclusion on getting mp4 for best size/quality ratio? :)