Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting / segmenting video stream with gstreamer

Is there a way to split a video into segments of specified length? Ie 60 minute video into 12x5 mins.

like image 808
Saideira Avatar asked Dec 29 '22 02:12

Saideira


2 Answers

It seems the "official" way to do this without reencoding and without losing frames is using the splitmuxsink element:

For example for a MKV file input:

gst-launch-1.0 filesrc location=input.mkv ! matroskademux ! h264parse ! splitmuxsink location=file%02d.mkv max-size-time=300000000000 muxer=matroskamux

The max-size-time gives the segment length in nanoseconds (sic), so the above value is 5 minutes. Because it cuts on keyframes, the real segment length will not be exactly 5 min but will vary by a few seconds.

like image 140
regnarg Avatar answered Jan 01 '23 20:01

regnarg


Afaik, you will need to develop your own "segmenter". This is similar to what you need to create HTTP Live streaming files. There might be a simpler solution, but here is what I would do:

You can make a relative small program (in python) using multifilesink next-file=buffer (or next-file marker, if segments can't fit in memory).

You'll need to encode or demux your input stream, and mux the stream back. Cut the muxed stream on key-frame when the desired duration is reached, and push the aggregated buffer (or mark a fake key frame, for example, to force sink to create a new file). Reinitialize the muxer (or append muxer streamheader) to get file with correct header than can be played seperately (depending on the muxer).

like image 26
elmarco Avatar answered Jan 01 '23 21:01

elmarco