Okay, let's modify the pipeline to achieve frequent saving using file segmentation and ensure playability of incomplete files using fragmented MP4.
We will replace the mp4mux ! filesink combination with splitmuxsink, which is designed for creating segmented files. We will configure the internal mp4mux used by splitmuxsink to produce fragmented MP4, ensuring the necessary metadata (moov and subsequent moof atoms) is written frequently and at the beginning of fragments, making incomplete files playable.
Here's the modified pipeline:
gst-launch-1.0 -e \
aravissrc features="OverlapMode=On Width=1024 Height=1024 ExposureAuto=Continuous AcquisitionFrameRate=88.5 AcquisitionFrameRateEnable=1 GevSCPSPacketSize=8164 Gain=0 GainAuto=Continuous GammaEnable=1 GammaSelector=sRGB" ! \
video/x-bayer,width=1024,height=1024 ! \
tcamconvert ! \
nvvidconv compute-hw=1 ! \
"video/x-raw(memory:NVMM),format=NV12,width=640,height=640" ! \
tee name=t t. ! \
queue max-size-buffers=3 ! \
m.sink_0 nvstreammux name=m batch-size=1 width=640 height=640 live-source=1 ! \
nvinfer config-file-path=config_infer_primary_yoloV8.txt ! \
nvdsosd ! nvegltransform ! fpsdisplaysink text-overlay=false video-sink=nveglglessink \
t. ! \
queue max-size-buffers=3 ! \
nvvidconv compute-hw=1 ! \
x264enc bitrate=6000 speed-preset=ultrafast tune=zerolatency ! \
splitmuxsink \
location=output_%05d.mp4 \
max-size-time=60000000000 \
muxer-factory=mp4mux \
muxer-properties="properties,fragment-duration=5000" \
async-finalize=true \
max-files=10 -v
Explanation of Changes:
splitmuxsink: Replaces mp4mux ! filesink. This element handles the creation of multiple output files (segments).
location=output_%05d.mp4: Defines the naming pattern for the output files (e.g., output_00000.mp4, output_00001.mp4, ...).
max-size-time=60000000000: Sets the maximum duration of each segment file to 60 seconds (60 * 1,000,000,000 nanoseconds). Adjust this value to control how frequently a new file is created. Shorter times mean more frequent file finalization but more files.
muxer-factory=mp4mux: Specifies that splitmuxsink should use mp4mux internally to create the MP4 container for each segment.
muxer-properties="properties,fragment-duration=5000": This is crucial. It configures the internal mp4mux.
fragment-duration=5000: Tells mp4mux to create a fragmented MP4 file, writing metadata (moof atoms) roughly every 5000 milliseconds (5 seconds). This ensures that even if the pipeline stops abruptly, the already written fragments within the current segment file are self-contained and playable. The initial moov atom is written very early. This directly addresses the need for playback even if the stream ends abruptly.
async-finalize=true: When switching to a new segment file, the process of finalizing and closing the previous file is done in a separate thread. This prevents potential blocking of the main streaming thread, making the saving process asynchronous and reducing the chance of frame drops during file switching. This addresses the "async" aspect.
max-files=10: (Optional but recommended) Limits the total number of segment files kept. Once the limit is reached, the oldest file will be deleted when a new one is created, acting like a rolling buffer. Remove or adjust if you need to keep all segments indefinitely.
queue max-size-buffers=3: Added slightly larger queues (max-size-buffers=3 instead of default 2) before the tee branches. This can sometimes help smooth out processing, especially when one branch (like file writing) might occasionally introduce small delays. Adjust if needed based on performance monitoring.
tune=zerolatency for x264enc: You already had this, which is good. It minimizes encoding delay, helping ensure data reaches the muxer promptly, which is beneficial for frequent fragmentation and saving.
How this addresses the requirements:
- Frequent Saving (Async):
splitmuxsink creates new files periodically (max-size-time). The async-finalize=true property ensures the file closing doesn't block the main pipeline. Furthermore, within each segment file, fragment-duration ensures metadata is written frequently (every 5 seconds in this example).
- Playable Incomplete Files (
moov at start): Using fragment-duration with mp4mux (via splitmuxsink) creates a fragmented MP4. This format inherently writes a minimal moov atom at the beginning and then writes self-contained moof (movie fragment) atoms periodically along with the corresponding media data (mdat). If the power cuts out, the file will be incomplete, but players supporting fragmented MP4 can typically play back all the fully written fragments preceding the interruption.