4 ms·
Don’t quote me on this, but I think ffmpeg lets you choose between starting at the nearest Iframe or re-encoding the first GOP.
by mdonahoe 7y ago
Don’t quote me on this, but I think ffmpeg lets you choose between starting at the nearest Iframe or re-encoding the first GOP.
- ehsankia 7y agoI believe by default it snaps to the nearest iframe. At least that's been my experience.
- bscphil 7y agoYeah, my understanding is that it's actually very hard to get ffmpeg not to do this, which is why I'd be interested in understanding exactly what this link is supposedly doing. I believe it's almost certainly iframe snapping. If anyone knows of anyway to enforce reencoding up to the first iframe in the copy, I'd love to hear it.
- cm2187 7y agoNo it is possible, but the -ss needs to be after the -i not before: https://ffmpeg.org/ffmpeg.html#Main-options https://ffmpeg.org/ffmpeg.html#Main-options > -ss position (input/output) > When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved. > When used as an output option (before an output url), decodes but discards input until the timestamps reach position. It is much slower when used an output option, and I don't really know what it does when you do stream copy, but I suspect it leaves the whole segment with some sort of offset instruction. I often get artefacts when I play it back on vlc, so I try to avoid that option (unless I am re-encoding).
- bscphil 7y agoIt's slower when used as an output option because it only has an effect on ffmpeg's output; that is, ffmpeg literally has to decode the whole stream up to that point because it doesn't know it can seek. I believe this has no effect on whether or not you can cut precisely - if transcoding is not enabled, even if ffmpeg is sticking the frames before the first keyframe in the output file, they're effectively garbage from the point of view of the decoder (which might explain why you see artifacts in some players).
- _Gyan_ 7y agoThere is no provision for partial stream encoding. It's either complete streamcopy or encoding.
- nosoop 7y agoThis indeed does snap to keyframes when input seeking is used. On output seeking, it cuts the video at the exact time, but there isn't enough information to rebuild the image as it's still a truncated copy of the stream. It's possible to reencode a (h264-encoded, at least) video up to the first keyframe; it's discussed in this StackExchange submission: https://video.stackexchange.com/a/23542 https://video.stackexchange.com/a/23542 Coincidentally, I refined the above approach very recently and posted the result to my own blog: https://csrd.science/blag/2020/trimming-an-h264-video-and-preserving-most-of-the-original-video-stream.html https://csrd.science/blag/2020/trimming-an-h264-video-and-pr... The gist is to extract and reencode the segment containing the group of pictures to be cut (using the stream segment muxer), then rejoin the subsequent segments and remux the trimmed audio back in afterwards.
- bscphil 7y agoNice work! Would definitely like to see the complete write up and code you talk about at the end, especially if you manage to test it on mkv or mp4 containers.