我希望能够将视频上传到服务器并在需要时进行剪切。
因此,我首先对其进行编码:
ffmpeg -i input.mp4 -vcodec h264 -acodec aac -strict -2 -preset slow output.mp4
然后我尝试剪切它:
ffmpeg -i input.mp4 -ss 00:30:00 -c copy -t 00:30:00 output.mp4
并且音频和视频轨道不同步,音频大约晚了 10 秒......
那么,有没有办法修复 ffmpeg 剪切问题或者是否有其他方法可以在 linux 中剪切视频?
更新 这是剪切时的 ffmpeg 输出
ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libdcadec --enable-libfreetype --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/mnt/storage/19b7c850b0c05c6f6fd26a7eda34841dddd55e33.mp4.prot':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.40.101
Duration: 00:45:24.92, start: 0.042667, bitrate: 492 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 736x414 [SAR 1:1 DAR 16:9], 345 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 139 kb/s (default)
Metadata:
handler_name : SoundHandler
[mp4 @ 0x2aff400] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 0x2aff400] Codec for stream 1 does not use global headers but container format requires global headers
Output #0, mp4, to '/home/videohosting/uploads/output7.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.40.101
Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 736x414 [SAR 1:1 DAR 16:9], q=2-31, 345 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 139 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=22873 fps=22858 q=-1.0 Lsize= 53406kB time=00:15:24.87 bitrate= 473.0kbits/s
video:36732kB audio:15765kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.732541%
更新2
同时剪切和转换只会输出空的视频文件
命令
ffmpeg -ss 00:05:00 -i input.mp4 -t 00:10:00 -vcodec h264 -acodec aac -strict -2 -preset slow output.mp4
输出
ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04) configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libdcadec --enable-libfreetype --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvo-aacenc --enable-libvidstab libavutil 54. 31.100 / 54. 31.100 libavcodec 56. 60.100 / 56. 60.100 libavformat 56. 40.101 / 56. 40.101 libavdevice 56. 4.100 / 56. 4.100 libavfilter 5. 40.101 / 5. 40.101 libavresample 2. 1. 0 / 2. 1. 0 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 2.101 / 1. 2.101 libpostproc 53. 3.100 / 53. 3.100 Guessed Channel Layout for Input Stream #0.0 : stereo Input #0, asf, from '/mnt/storage/tmp/e7ff19971eab45abf06d034444d94549ef3b5584.mp4': Metadata: SfOriginalFPS : 299700 WMFSDKVersion : 11.0.6001.7000 WMFSDKNeeded : 0.0.0.0000 comment : Footage: Small World Productions, Inc; Tourism New Zealand | Producer: Gary F. Spradling | Music: Steve Ball title : Wildlife in HD copyright : © 2008 Microsoft Corporation IsVBR : 0 DeviceConformanceTemplate: AP@L3 Duration: 00:00:30.09, start: 0.000000, bitrate: 6977 kb/s Stream #0:0(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, 2 channels, fltp, 192 kb/s Stream #0:1(eng): Video: vc1 (Advanced) (WVC1 / 0x31435657), yuv420p, 1280x720, 5942 kb/s, 29.97 fps, 29.97 tbr, 1k tbn, 1k tbc [libx264 @ 0x306fb80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX [libx264 @ 0x306fb80] profile High, level 3.1 [libx264 @ 0x306fb80] 264 - core 142 r2491 24e4fed - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=5 deblock=1:0:0 analyse=0x3:0x113 me=umh subme=8 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=2 b_bias=0 direct=3 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=50 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to '/home/videohosting/uploads/output6.mp4': Metadata: SfOriginalFPS : 299700 WMFSDKVersion : 11.0.6001.7000 WMFSDKNeeded : 0.0.0.0000 comment : Footage: Small World Productions, Inc; Tourism New Zealand | Producer: Gary F. Spradling | Music: Steve Ball title : Wildlife in HD copyright : © 2008 Microsoft Corporation IsVBR : 0 DeviceConformanceTemplate: AP@L3 encoder : Lavf56.40.101 Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=-1--1, 29.97 fps, 30k tbn, 29.97 tbc Metadata: encoder : Lavc56.60.100 libx264 Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s Metadata: encoder : Lavc56.60.100 aac Stream mapping: Stream #0:1 -> #0:0 (vc1 (native) -> h264 (libx264)) Stream #0:0 -> #0:1 (wmav2 (native) -> aac (native)) Press [q] to stop, [?] for help frame= 0 fps=0.0 q=0.0 Lsize= 0kB time=00:00:00.00 bitrate=N/A video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
答案1
由于关键帧,将-ss
和-t
选项与结合起来-c copy
会导致视频不同步和/或持续时间不精确。
更好的方法是进行一次解码/编码循环(尽管这会导致一些质量损失)以获得更好的时间安排。同时进行剪切和转换是更好的选择,并且没有上述缺点。
答案2
我遇到了类似的问题并通过以下方式解决,也许它可以对未来的读者有所帮助。
ffmpeg -i test.mp4 -ss 2 -to 10 -c:v copy -c:a aac out.mp4
也就是说,我重新编码了音频。这样音频就同步了。我认为这没什么区别,但请注意,我的用例需要选项-to
而不是-t
。
答案3
我深入研究了同步问题并找到了两个解决方案:
AV 同步分割
ffmpeg -ss $START_TIME -i "$VIDEO_FILE" -vcodec copy -acodec copy -t $CHUNK_LENGTH "${VIDEO_FILE%.mp4}_part$(($i+1)).mp4"
通过将 -ss $START_TIME 放在 -i "$VIDEO_FILE" 之前,FFmpeg 会以最高效的方式查找指定的开始时间。选项 -vcodec copy 和 -acodec copy 指示 FFmpeg 直接将视频和音频比特流复制到输出文件中,而无需重新编码。
问题:关键帧之前可能会发生分割,从而导致生成的视频片段出现问题。
关键帧调整分割:
利用片段复用器可以在关键帧处分割视频,从而提供更精确的分割:
ffmpeg -i "$VIDEO_FILE" -c copy -map 0 -segment_time $CHUNK_LENGTH -f segment -reset_timestamps 1 -avoid_negative_ts make_zero -segment_start_number 1 "${VIDEO_FILE%.mp4}_part%01d.mp4"
脚本
从以下位置获取工作脚本:https://github.com/devskale/mp4split