Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop ffmpeg gently (to allow it to finish writing mp4 file) when streaming from udp

i'm trying to receive a udp stream on a server and write it to an mp4 file. this works fine:

  ffmpeg -i udp://127.0.0.1:12345 -codec copy out.mp4

if i press CTRL+C (or send SIGINT to the process) the ffmpeg quits and i have a working mp4 file.

however, if the data stops coming in on the udp port (e.g. after 10 minutes of streaming) and i try to stop ffmpeg it requires 2 interrupt signals at which point ffmpeg exits abruptly and results in an unplayable mp4 file.

does anyone know how to prevent ffmpeg from hanging when there's no input data or to force it to write out the mp4 header?

i know i can specify a url option

 udp://127.0.0.1:12345?timeout=<microseconds>

however, i need to be able to ignore occasional (5-10 minute long) pauses during the process of recording and only quit at the end. so even if i set timeout=60000000 it will cause my app to wait for 10 minutes when i want to stop the stream (e.g. if there's no data) and i need it to quit immediately when i send a SIGINT to it

like image 438
ierdna Avatar asked Apr 11 '14 19:04

ierdna


1 Answers

You can add movflags, even you forced stopped ffmpeg, you will still have playable mp4 file.
ffmpeg -i input -movflags +frag_keyframe+separate_moof+omit_tfhd_offset+empty_moov output

More details here (Gyan's answer):
https://video.stackexchange.com/a/18179/33701

Other suggestions:
Add flags like -reconnect_streamed 1, ffmpeg will try to reconnect if error occurs.
Docs: https://ffmpeg.org/ffmpeg-protocols.html

like image 100
Justin Avatar answered Nov 09 '22 03:11

Justin