所以如果现在解决了我的 ffmpeg 旅程的所有问题,我只需要在转换视频后立即在 Flash 播放器中播放,而无需等到视频播放 100% 我使用:
if ($success_msg) {
$tmp_parts = explode('.', $file['name']);
$ext = array_pop($tmp_parts);
$ext = strtolower($ext);
if($ext == "avi" && $convert_avi == true) {
$convert_source = _VIDEOS_DIR_PATH.$new_name;
$conv_name = substr(md5($file['name'].rand(1,888)), 2, 10).".mp4";
$converted_file = _VIDEOS_DIR_PATH.$conv_name;
$ffmpeg_command = 'ffmpeg -i '.$convert_source.' -acodec libfaac -vcodec libx264 -s 1280x720 -ar 44100 -async 44100 -r 29.970 -ac 2 -qscale 5 '.$converted_file;
echo exec($ffmpeg_command);
$sql = "UPDATE pm_temp SET url = '".$conv_name."' WHERE url = '".$new_name."' LIMIT 1";
$result = @mysql_query($sql);
unlink($convert_source);
}
echo $success_msg;
}
转换视频
编辑 #1 ffmpeg 连接输出与快速启动功能:
root@1tb:~# ffmpeg -i sample.avi -movflags faststart -acodec libfaac -vcodec libx264 -s 1280x720 -ar 44100 -async 44100 -r 29.970 -ac 2 -qscale 5 consample.mp4
ffmpeg version 0.7.15, Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 22 2013 07:18:58 with gcc 4.4.5
configuration: --enable-libdc1394 --prefix=/usr --extra-cflags='-Wall -g ' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-avfilter --enable-libdirac --disable-decoder=libdirac --enable-libfreetype --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-libvpx --enable-librtmp --extra-libs=-lgcrypt --disable-altivec --disable-armv5te --disable-armv6 --disable-vis
libavutil 50. 43. 0 / 50. 43. 0
libavcodec 52.123. 0 / 52.123. 0
libavformat 52.111. 0 / 52.111. 0
libavdevice 52. 5. 0 / 52. 5. 0
libavfilter 1. 80. 0 / 1. 80. 0
libswscale 0. 14. 1 / 0. 14. 1
libpostproc 51. 2. 0 / 51. 2. 0
[mp3 @ 0x15389100] Header missing
[mpeg4 @ 0x15386dc0] Invalid and inefficient vfw-avi packed B frames detected
Input #0, avi, from 'sample.avi':
Metadata:
encoder : VirtualDubMod 1.5.10.2 (build 2540/release)
Duration: 00:01:01.81, start: 0.000000, bitrate: 1194 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 640x352 [PAR 1:1 DAR 20:11], 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s
[MOV/3GP/MP4/3G2 muxer @ 0x15386c80] [Eval @ 0x7ffffec71230] Undefined constant or missing '(' in 'faststart'
[MOV/3GP/MP4/3G2 muxer @ 0x15386c80] Unable to parse option value "faststart"
Invalid value 'movflags' for option 'faststart'
您必须
使用-movflags faststart
以避免播放器必须缓冲整个视频。
通常,mp4的内部索引将被写入文件的末尾(因为在处理文件时未知/不完整)。 faststart
将执行另一个muxer传递,以便在完成后将该索引移动到前面,以便玩家可以在不先读取整个文件的情况下读取它。
确保您的 ffmpeg 构建足够新以支持此功能。
例:
# -strict 2 just for allowing the aac encoder.
$ ffmpeg -i in.mkv -strict -2 -movflags faststart out.mp4
ffmpeg version 57577-g83b8d74 Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 28 2013 15:31:30 with Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Input #0, matroska,webm, from 'in.mkv':
Duration: 00:00:23.02, start: 0.000000, bitrate: 4128 kb/s
Stream #0:0: Video: h264 (High), yuv420p, 1280x716 [SAR 1:1 DAR 320:179], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Stream #0:1: Audio: mp3, 44100 Hz, stereo, s16p, 192 kb/s (default)
...
Output #0, mp4, to 'out.mp4':
Metadata:
encoder : Lavf55.19.104
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x716 [SAR 1:1 DAR 320:179], q=-1--1, 12800 tbn, 25 tbc (default)
Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s (default)
Stream mapping:
Stream #0:0 -> #0:0 (h264 -> libx264)
Stream #0:1 -> #0:1 (mp3 -> aac)
Press [q] to stop, [?] for help
...
[mp4 @ 0x7f9c44016e00] Starting second pass: moving the moov atom to the beginning of the file
...
video:5426kB audio:361kB subtitle:0 global headers:0kB muxing overhead 0.291754%
...
由于您的 ffmpeg 很旧,因此使用 qt-faststart
而不是 ffmpeg 本身可能会有更好的运气。ffmpeg 中有一个自 0.5(或其他东西)以来的tools/qt-faststart.c
,您可以cc -o qt-faststart tools/qt-faststart.c
编译并使用./qt-faststart in.mp4 out.mp4
.