Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

FFMPEG - cut video result in longer cut

I'm trying to cut the last 8 seconds of a video I got; the problem is that ffmpeg is picking the last 10 seconds instead and I don't really know why. I checked and there is no offset between "0:00:00" and the effective start of the video, so why should it take more seconds?

this is my code:

ffmpeg.exe -ss 00:01:02 -i "F:\temp\input.mp4" -c copy -t 00:00:08 "D:\temp\1_output.mp4"

this is the output:

ffmpeg version N-90893-gcae6f806a6 Copyright (c) 2000-2018 the FFmpeg

developers built with gcc 7.3.0 (GCC) configuration: --enable-gpl

--enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-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 --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-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 libavutil 56. 17.100 / 56. 17.100 libavcodec 58. 19.100 / 58. 19.100 libavformat 58. 13.100 / 58. 13.100 libavdevice 58. 4.100 /

  1. 4.100 libavfilter 7. 21.100 / 7. 21.100 libswscale 5. 2.100 / 5. 2.100 libswresample 3. 2.100 / 3. 2.100 libpostproc 55. 2.100 / 55. 2.100 Input #0,

mov,mp4,m4a,3gp,3g2,mj2, from 'F:\temp\input.mp4': Metadata:

major_brand : isom

minor_version : 512

compatible_brands: isomiso2avc1mp41

encoder : Lavf58.13.100 Duration: 00:01:10.34, start: 0.000000, bitrate: 14542 kb/s

Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 576x1024, 14346 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)

Metadata:

handler_name : VideoHandler

Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 193 kb/s (default)

Metadata:

handler_name : SoundHandler Output #0, mp4, to 'D:\temp\1_output.mp4': Metadata:

major_brand : isom

minor_version : 512

compatible_brands: isomiso2avc1mp41

encoder : Lavf58.13.100

Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 576x1024, q=2-31, 14346 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc

(default)

Metadata:

handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 193 kb/s (default)

Metadata:

handler_name : SoundHandler Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy) Press [q] to stop, [?] for help frame= 252

fps=0.0 q=-1.0 Lsize= 3089kB time=00:00:07.99 bitrate=3164.7kbits/s

speed=1e+003x video:2841kB audio:240kB subtitle:0kB other

streams:0kB global headers:0kB muxing overhead: 0.283155%

I don't really understand what i'm doing wrong

Thanks

Upvotes: 0

Views: 1501

Answers (2)

user14816009
user14816009

Reputation:

The behavior you're encountering is due to the way -c copy works in FFmpeg. When using -c copy, FFmpeg performs a stream copy, meaning it doesn't re-encode the video or audio but rather copies the original stream directly. Because of this, FFmpeg can only cut the video at keyframes (I-frames).

If the keyframes in your video are not located every 1 second, this can lead to imprecise cut timings when using stream copy. In the case of your video, it's possible that there's a keyframe around 10 seconds after the time you've specified, causing FFmpeg to include an extra couple of seconds in the output.

To get more precise cuts, I propose 2 solutions here:

  1. Re-encode the video: This will give you frame-accurate cuts but will also take more time and may reduce the video quality if not done correctly.

    ffmpeg.exe -ss 00:01:02 -i "F:\temp\input.mp4" -t 00:00:08 "D:\temp\1_output.mp4"
    
  2. Use -c copy with -avoid_negative_ts 1: This will make FFmpeg try to avoid negative timestamps, which might help in some situations, but it still relies on keyframes.

    ffmpeg.exe -ss 00:01:02 -i "F:\temp\input.mp4" -c copy -t 00:00:08 -avoid_negative_ts 1 "D:\temp\1_output.mp4"
    

Upvotes: 1

Bora Savkar
Bora Savkar

Reputation: 75

do not use "-c copy" it sucks up everything.

just try like this and you are good to go...

ffmpeg.exe -i "F:\temp\input.mp4" -ss 00:01:02 -to 00:01:10 "D:\temp\1_output.mp4"

Upvotes: -1

Related Questions