ffmpeg is not even cutting this and other mp4s to the nearest second

0

I have a file

Other - Analogue Clock-JEJqy1Wlovw.mp4 which is just a download from https://www.youtube.com/watch?v=JEJqy1Wlovw

It's 2 minutes 13 seconds long.

I had some issues cutting it to the second.

I was able to use this command

ffmpeg -ss 0 -i infile.mp4 -c copy -t 60 output.mp4

to make a file that was 60 seconds, so no problem so far.

That's the file exactly 1 minute, no issue there so far.

C:\vids\a>dir
blahhclo.mp4   9,590,540 bytes

C:\vids\a>


C:\vids\a>mediainfo blahhclo.mp4
General
Complete name                            : blahhclo.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 9.15 MiB
Duration                                 : 1mn 0s
Overall bit rate                         : 1 278 Kbps
Writing application                      : Lavf58.3.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.0
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 3 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 1mn 0s
Bit rate                                 : 1 146 Kbps
Width                                    : 1 920 pixels
Height                                   : 1 080 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.022
Stream size                              : 8.21 MiB (90%)

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40
Duration                                 : 1mn 0s
Bit rate mode                            : Constant
Bit rate                                 : 126 Kbps
Channel count                            : 2 channels
Channel positions                        : Front: L R
Sampling rate                            : 44.1 KHz
Compression mode                         : Lossy
Delay relative to video                  : 40ms
Stream size                              : 920 KiB (10%)



C:\vids\a>

But I do have an issue when I try to cut it at the following times, and it's whether I try to cut the 1 minute one or the whole 2 min 13sec one.

If I cut with -ss 0 -t 1, it produces a file that is 1s 22ms.

So that's not too bad that's fairly close to one second. 1.022 seconds is quite good for an attempt to cut one second.

If I cut with -ss 0 -t 2, it produces a file that is 3s 19ms That is terrible! it's meant to be one second!

If I cut with -ss 0 -t 30 then it is fine it does 30s 1ms.

Or this video for example, also an mp4 https://www.youtube.com/watch?v=pJ3-xf26wDE

C:\blah>ffmpeg -ss 189 -i vid.mp4 -c copy -t 6 someoutput.mp4

C:\blah>mediainfo someoutput.mp4

produces a duration of 8s 383ms

That is so wrong though 'cos it should be 6 seconds! So that is two seconds too long!

C:\blah>ffmpeg -ss 191 -i vid.mp4 -c copy -t 6 someoutput2.mp4

C:\blah>mediainfo output2.mp4

shows that ffmpeg produced a duration of 10s 403ms

which is crazily wrong 'cos the ffmpeg command specified 6 seconds!

So that's not just one second out, that is 4 seconds out!

That's almost 50% longer than it should be!

barlop

Posted 2018-01-21T22:01:00.503

Reputation: 18 677

Question was closed 2019-04-27T10:48:54.120

Answers

0

If you need frame accuracy, don't use -c copy, which means the same as -c:a copy -c:v copy -c:s copy. It copies the bitstream, and if the specified start or end time isn't falling on an I-Frame, you get the nearest one, which can be quite far away from the desired location. See also the Seeking wiki.

You can however re-encode the video to get accurate cutting. The audio can be copied, as basically, an audio stream has "only keyframes". For example, in order to encode the video to H.264 and copy the audio, apply the command:

ffmpeg -ss 191 -i vid.mp4 -c:v libx264 -c:a copy -t 6 someoutput3.mp4 

That works; it produces a file that is 6s 26ms. So that is very much to the nearest second as opposed to the one with bitstream copy, that was 10.4 seconds.

Re-encoding may reduce the quality of your video stream. To change the quality of the video, read more about it in the H.264 encoding guide.

This command:

ffmpeg -ss 193 -i vid.mp4 -c:v libx264 -c:a copy -t 4 someoutput4.mp4

Produces a 4s 26ms file. Close to 4s, as it should be, whereas if I did it with bitstream copying it'd be just over 5 seconds.

barlop

Posted 2018-01-21T22:01:00.503

Reputation: 18 677

You don't need to re-encode the audio, you can use copy for that. – Gyan – 2018-01-22T05:03:58.130

@Mulvya okay. can you explain why? – barlop – 2018-01-22T06:42:44.130

All audio frames are keyframes, so there is no imprecision with seeking. Only video keyframes may be sparse. – Gyan – 2018-01-22T06:44:08.580

I attempted to condense the points in your answer to make them more clear. – slhck – 2019-04-27T10:46:23.980