ffmpeg: cut video but getting the wrong length
Asked Answered
H

5

6

I'm using ffmpeg to cut a video. Here is the input information:

    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 5113 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)

And this is the command I use:

    ffmpeg -y -ss 10 -t 5 -i test.mp4 -c copy output.mp4

But the output duration is about 9.66 seconds.

It worked when I removed the '-c copy' option or the input doesn't contain the 'tv, bt709' tag. But it's slower.

I wonder how I can use '-c copy' and get the right piece of video I need?

Honoria answered 15/10, 2018 at 5:31 Comment(2)
I was wrong. The input without 'tv, by709' tag still doesn't work.Honoria
Codec copy can only spice on key frames.Keeling
K
13

In my situation, I don't use -c copy but still receive an incorrect duration. Upon investigation, I found that it caused by "video chapters". So disable chapters by -map_chapters -1 will output the correct duration.

ffmpeg -ss 04:20 -to 04:41 -i "$input_file" -c:a aac -c:v h264_videotoolbox -b:v 29445k -map_chapters -1 "output.mp4"
-ss 04:20              : From
-to 04:41              : To
-i "$input_file"       : Input file
-c:a aac               : Set AAC codec for audio output
-c:v h264_videotoolbox : h264 hw encoder on Mac
-b:v 29445k            : Output video bitrate
-map_chapters -1       : Disable video chapters
"output.mp4"           : Output file
Keenan answered 17/6, 2021 at 15:54 Comment(1)
Thank you, would never had guessed that by myself.Coincidentally
L
2

The first command will cut from 00:01:00 to 00:03:00 (in the original), using the faster seek.

The second command will cut from 00:01:00 to 00:02:00, as intended, using the slower seek.

The third command will cut from 00:01:00 to 00:02:00, as intended, using the faster seek.

ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4

ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4

ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4

Full document enter link description here

Lizabethlizard answered 24/8, 2019 at 17:31 Comment(1)
This seems to be a more common root cause than the map_chapters, interesting to see placement of options being so impactful!Hunger
T
0

from the ffmpeg documentation:

-t duration (input/output) When used as an input option (before -i), limit the duration of data read from the input file.

When used as an output option (before an output url), stop writing the output after its duration reaches duration.

duration must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.

Given this, if you want a 5 seconds video stream try to specify the duration you want to read from the input file (before the -i tag) and the duration you want from your final output file (before the output url)

Terrify answered 15/10, 2018 at 5:45 Comment(3)
I used 'ffmpeg -ss 10 -t 5 -i test.mp4 -t 5 output.mp4'. The duration was right, but the output doesn't match the 10 to 15 seconds of the input which is what I want.Honoria
This might interest you, there are a lot of caveat when cutting content: https://trac.ffmpeg.org/wiki/SeekingTerrify
Thanks a lot. I've read it. Tried several ways though, it just doesn't work when doing codec copy and want it to be fast...Honoria
N
0

Check you file by ffprobe,

ffprobe -show_entries format <input_file>

Check the property, start_time, maybe in your case the property no a zero.

Nikko answered 2/8, 2024 at 20:26 Comment(0)
S
-1

just delete -c copy and you will get the exact cut.

"ffmpeg -y -ss 10 -t 5 -i test.mp4 output.mp4"

Squamation answered 29/4, 2021 at 17:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.