如何使用 FFMPEG 最好地转换 Flash 兼容的 mp4 文件?

发布于 2024-07-11 20:04:48 字数 339 浏览 8 评论 0原文

我正在尝试使用 ffmpeg 将不同的文件转换为与 flash 兼容的 .mp4 文件,但我似乎无法让它工作。 当然,目标是以最小的文件大小获得最好的质量。

到目前为止,我已经有了这个,它可以工作,但由于某种原因它不能在 Flash 播放器中播放。 结果不太好,我该如何提高转化率?

这是我正在使用的命令:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

极度宠爱 2024-07-18 20:04:48

要创建交错元数据(这将允许中流恢复、倒回/快进),请使用 gpac 实用程序(例如包含在 medibuntu 中)重新交错文件。 如下转换为 mp4,使用 FFmpeg 0.5 或更高版本:

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

然后

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

Tada! 完毕! 这将在 JW Flv 或其他 Flash 播放器中正确播放。

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.

合久必婚 2024-07-18 20:04:48

FLV 和 MP4 是媒体容器。

MPEG-4 第 2 部分和 H.264 是视频编解码器。 (H.264 提供更好的质量)

libx264 是 H.264 编解码器的编码器。

mpeg4 是 MPEG-4 第 2 部分编解码器的编码器。

Flash 只能播放 FLV 容器中的视频编解码器 H.264。

所以参数应该是这样的:

-f flv -vcodec libx264

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
零崎曲识 2024-07-18 20:04:48

在我的片段集合中,我有以下用于此任务的内容。

ffmpeg 第一遍:

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 第二遍:

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

质量设置为 2000k = 视频比特率、128k = 音频比特率和 300 = GOP。 别问我其他参数是什么。 :)

与 MP4Box 交错:

MP4Box -inter 500 output.mp4

创建缩略图/海报图像(尝试 5 以获得您想要的图像)

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

希望这会有所帮助。

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.

身边 2024-07-18 20:04:48

我使用一系列命令来执行此操作,从 mencoder 开始。 (参见下面的脚本)。 诀窍在于 ffmpeg(和 mencoder)的 mp4 文件输出由于其交错方式而通常不兼容闪存,并且它们无法对此行为提供良好的控制。 因此,我生成一个 AVI 文件,然后使用 mp4box 按我想要的方式重新混合它。 这是我的完整脚本,期望获得一个 .avi 文件,并生成一个可 flash 播放的 .mp4 文件。

#!/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

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
伴我心暖 2024-07-18 20:04:48

如果您使用 ffmpeg 制作 .mp4,请查看 ffpresets/ 目录并使用 -vpre。 默认选项(对于所有编解码器,尤其是 libx264)非常糟糕,实际上所有这些选项都应该设置为自定义选项。 -vpre 负责 x264 的大部分工作。

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.

生生漫 2024-07-18 20:04:48

实际上,您不应该使用 maxrate,尤其是当您将比特率明确设置为 200k 时,不应该使用 10000k 的 maxrate。 事实上,仔细看看这个,我真的不认为你明白这些东西的大部分用途:)

对于初学者来说,你调用输入文件output.mp4,加上输出文件(实际上应该是/dev/null)第 1 步)称为 yourinfile.avi,并且您实际上尚未设置容器格式,因此最终得到的是 h264 编码的 avi 文件。

对于高质量 1080p HD flash 兼容的 h264 编码 MPEG-4 视频,请尝试此操作,首先在与您正在编码的视频相同的目录中创建一个包含以下内容的文件,并将其命名为 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

然后从命令行:

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

应该可以与 flash 10+ 完美配合

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+

提笔落墨 2024-07-18 20:04:48

只是一些关于为什么会发生这种情况的想法

  • mp4 仅在 flash 9.0.115.0 或更高版本中受支持,您使用的是哪个版本?
  • 托管该文件的网络服务器是否有 mp4 mime 类型设置?

转换为 FLV 时可以播放吗?这是一个选项吗? 然后您将使用类似以下选项的内容

ffmpeg -i input.file -f flv -r 25 -b 560000 -s 610x340 -acodec mp3 -ac 2 -ab 64 -ar 44100 output.file

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
太阳公公是暖光 2024-07-18 20:04:48

我在网络上的某个地方发现了这个(非常冗长,我不确定其中一半的作用)片段。 它是为执行两次传递而设计的,因此它不会在第一次传递时输出任何视频,您需要将其设置为执行 -pass 2 才能运行第二次传递并输出实际视频。

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

要在下载过程中获得渐进式播放,您还需要将元数据移动到文件的开头。 ffmpeg 将其放在最后,Flash 需要它才能开始播放。 一个有用的工具是 QTIndexSwapper

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文