为什么 ffmpeg 不会从该视频开头删除几秒钟?
C:\somedir>mediainfo blah.mp4
General
Complete name : blah.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom/iso2/avc1/mp41)
File size : 1.77 MiB
Duration : 18 s 693 ms
Overall bit rate mode : Variable
Overall bit rate : 796 kb/s
Encoded date : UTC 2020-08-27 22:50:08
Tagged date : UTC 2020-08-27 22:50:08
Writing application : Lavf51.12.1
Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : Main@L3
Format settings : CABAC / 3 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference frames : 3 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 18 s 680 ms
Bit rate : 666 kb/s
Width : 852 pixels
Height : 480 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 25.000 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.065
Stream size : 1.48 MiB (84%)
Encoded date : UTC 2020-08-27 22:50:08
Tagged date : UTC 2020-08-27 22:50:08
Codec configuration box : avcC
Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 18 s 693 ms
Bit rate mode : Variable
Bit rate : 126 kb/s
Channel(s) : 2 channels
Channel layout : L R
Sampling rate : 44.1 kHz
Frame rate : 43.066 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 287 KiB (16%)
Encoded date : UTC 2020-08-27 22:50:08
Tagged date : UTC 2020-08-27 22:50:08
C:\somedir>mediainfo blah.mp4 | find "Duration"
Duration : 18 s 693 ms
Duration : 18 s 680 ms
Duration : 18 s 693 ms
C:\somedir>ffmpeg -ss 4 -i blah.mp4 -c copy blah2.mp4
ffmpeg version N-94664-g0821bc4eee Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 9.1.1 (GCC) 20190807
configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --e
nable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus -
-enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --ena
ble-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enabl
e-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid
--enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
libavutil 56. 33.100 / 56. 33.100
libavcodec 58. 55.101 / 58. 55.101
libavformat 58. 31.104 / 58. 31.104
libavdevice 58. 9.100 / 58. 9.100
libavfilter 7. 58.101 / 7. 58.101
libswscale 5. 6.100 / 5. 6.100
libswresample 3. 6.100 / 3. 6.100
libpostproc 55. 6.100 / 55. 6.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'blah.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2020-08-27T22:50:08.000000Z
encoder : Lavf51.12.1
Duration: 00:00:18.69, start: 0.000000, bitrate: 796 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 852x480 [SAR 1:1 DAR 71:40], 665 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc (default)
Metadata:
creation_time : 2020-08-27T22:50:06.000000Z
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
Metadata:
creation_time : 2020-08-27T22:50:06.000000Z
handler_name : SoundHandler
Output #0, mp4, to 'blah2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.31.104
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 852x480 [SAR 1:1 DAR 71:40], q=2-31, 665 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tb
c (default)
Metadata:
creation_time : 2020-08-27T22:50:06.000000Z
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
Metadata:
creation_time : 2020-08-27T22:50:06.000000Z
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 467 fps=0.0 q=-1.0 Lsize= 1819kB time=00:00:14.66 bitrate=1015.9kbits/s speed=1.63e+03x
video:1518kB audio:287kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.821737%
C:\somedir>mediainfo blah2.mp4 | find "Duration"
Duration : 18 s 693 ms
Duration : 18 s 680 ms
Duration : 18 s 693 ms
C:\somedir>mediainfo blah2.mp4
General
Complete name : blah2.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom/iso2/avc1/mp41)
File size : 1.78 MiB
Duration : 18 s 693 ms
Overall bit rate : 797 kb/s
Writing application : Lavf58.31.104
Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : Main@L3
Format settings : CABAC / 3 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference frames : 3 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 18 s 680 ms
Bit rate : 666 kb/s
Width : 852 pixels
Height : 480 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 25.000 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.065
Stream size : 1.48 MiB (83%)
Codec configuration box : avcC
Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 18 s 693 ms
Bit rate mode : Constant
Bit rate : 126 kb/s
Channel(s) : 2 channels
Channel layout : L R
Sampling rate : 44.1 kHz
Frame rate : 43.066 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 287 KiB (16%)
Default : Yes
Alternate group : 1
C:\somedir>
总而言之,我们有
C:\somedir>mediainfo blah.mp4 | find "Duration"
Duration : 18 s 693 ms
Duration : 18 s 680 ms
Duration : 18 s 693 ms
C:\somedir>ffmpeg -ss 4 -i blah.mp4 -c copy blah2.mp4
C:\somedir>mediainfo blah2.mp4 | find "Duration"
Duration : 18 s 693 ms
Duration : 18 s 680 ms
Duration : 18 s 693 ms
C:\somedir>
答案1
当您使用-c copy
以避免重新编码视频时,只能在关键图片上修剪流。如果比特流中的第二个关键图片出现在 4 秒标记之后,则修剪点将“四舍五入”到第一个关键图片。
您要么接受这种情况,要么重新编码视频(即不要使用该-c copy
选项)。
由 barlop 添加
这是正确的,如果你看一下视频的关键帧
https://stackoverflow.com/questions/18085458/checking-keyframe-interval
ffprobe -loglevel error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=print_section=0 input.mp4 | awk -F',' '/K/ {print $1}'
(将 input.mp4 替换为文件名 blah.mp4)
$ ffprobe -loglevel error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=print_section=0 blah.mp4 | awk -F',' '/K/ {print $1}'
0.000000
5.120000
8.520000
10.240000
14.440000
15.360000
user@hp-probook1 ~/blah
$
看看它是否可以在复制编解码器时缩短六秒。
ffmpeg -ss 6 -i blah.mp4 -c copy test.mp4
对 18sec 文件执行操作,然后mediainfo test.mp4 | find "Dur"
显示新文件为 13sec
但它不会剪切 4 秒,因为 4 秒或之前的最后一个关键帧位于 0 秒。
但以下方法有效
C:\blah>mediainfo blah.mp4 | find "Dur"
Duration : 18 s 693 ms
Duration : 18 s 680 ms
Duration : 18 s 693 ms
C:\blah>
C:\blah>ffmpeg -ss 4 -i blah.mp4 -acodec libmp3lame -vcodec libx264 tt.mp4
C:\blah>mediainfo tt.mp4 | find "Dur"
Duration : 14 s 718 ms
Duration : 14 s 680 ms
Duration : 14 s 718 ms
Duration_LastFrame : -16 ms
C:\blah>
(I re-encoded the audio too, otherwise I got 18s audio and 14s video)
答案2
你告诉它要砍掉一点和复制整个视频;尝试
ffmpeg -ss 4 -i /tmp/blah.mp4 /tmp/blah2.mp4