Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

FFMpeg Copy Live Stream (Limit to 60s file)

I currently have a working way to get a live stream and start downloading it locally while it is still live.

ffmpeg -i source_hls.m3u8 -c copy output.mkv -y

The problem is I do not actually want to save the entire thing, I just periodically run another command on the output.mkv command to create a clip of part of the live stream.

I was wondering if it was possible to limit the output.mkv file to be only 60s long so once the stream goes over 1 minute it will just cut off the old video and be replaced by the new rolling video.

Is this possible or no?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can come close, using the segment muxer.

ffmpeg -i source_hls.m3u8 -c copy -f segment -segment_time 60 -segment_wrap 2 -reset_timestamps 1 out%02d.mkv -y

This will write to out00.mkv, then out01.mkv, then overwrite out00.mkv, next overwrite out01.mkv and so on.

The segment time is set at 60 seconds, so each segment will be around 60 seconds. The targets for splitting are 60,120,180,240... seconds of the input. However, video streams will be only be split at keyframes at or after the split target. So, if the first keyframe after t=59 is at 66, then the first segment will be 66s long. The next target is 120s. Let's say there's a KF at 121s, so the 2nd segment will be 66 to 121s = 55s long. Something to keep in mind when checking the segments.

Check the file modification times to see which segment contains the earlier data.

If you want to reduce the surplus duration, decrease segment_time and increase segment_wrap correspondingly. segment_time x segment_wrap should be target saved duration + segment_time long.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...