How to record last X hours of a live stream via FFMPEG? - ffmpeg

In normal case, saving a live stream in a local file is easy. But I'm looking for a way to keep last X amount of time of the stream. i.e last 2 days.
Obviously the file must be updated constantly.
Any help? Thanks.

Related

ffmpeg libav: Any way to set stream info in an input format instead of searching for it?

I'm writing a player for an RTMP stream using the ffmpeg API. I know the usual way to get the stream info into an input format is with avformat_find_stream_info. And that works. However, because it's RTMP it takes a long time for it to scan enough of the stream to pick up the info. I've played with max_analyze_duration and probesize and it's a bit better, but it still takes 10-15 seconds to load. That's way too long for my application.
But I'm the one making the stream on the other end, so I know exactly what's in it. It seems like it would make more sense for me to tell the input format what the stream info is rather than asking it to search for it. But I can't find any examples of this, and my attempts to use avformat_new_stream with an input format aren't working.
Does anyone know if this is possible? And if so, could you point me in the direction of how?
Thanks!
This is what is known as an XY problem
Yes, you can spoof the sequence header (assuming h.264/aac). But it won't accomplish what you want. What is happening is your RTMP server (reflector) is eating the first GOP. So even if the analyze was done faster, you must first wait for the first video key frame anyway.
You need to configure your RTMP server to send the full GOP (in nginx+rtmp the setting is wait_key on)

How to convert images to a video by the time they have been taken/saved and not by name?

I have a Livestream that takes pictures every one or two seconds. Then I have a folder in which the pictures are being saved.
Usually I would just simply take ffmpeg -start_number -i IMG_%d.JPG video.webm (I need the end format in .webm). I want to use the saved pictures to form a time-lapse to put up next to the stream. So people can see the last 24h/ 7d and 90d. So I wondered if I could code that all the pics of that time period are used and only them.
I later want it to happen automatic, so i dont have to write the code new every day.
Thanks for your help

Converting subtitles for different framerates

I'm trying to make a simple CLI program that parses a SRT subtitle file and creates a new one, editing the timestamps to fit the desired framerate.
Eg I have a one-hour video track that runs at 25.0fps, with proper subtitles.
When encoding the same video at 23.976fps, the output video is a few seconds shorter (3 seconds approximately)
I've tried applying the following cross product to each time value in my srt file :
timestamp = timestamp * outputfps / inputfps
This produces captions that are approx. 3 minutes earlier compared to the input SRT (for the last captions - for the first ones the delay is obviously lesser), where the maximum delay should be 3 seconds, according to the new video file length.
This is all new for me and it seems obvious that something's wrong with the way I convert these timestamps. Could you please highlight my mistake?
Edit : According to j_random_hacker clever answer, the video should have the same duration at 25 than at 12 fps, which is easily verified. Seems like the 3 seconds offset I have is there no matter what the output framerate is - I guess there's some sort of trimming happening back there.
The main question remains : how does one convert a subtitle track so it doesn't go out of sync as the video file plays? (see my own comment below if this is unclear)

ffmper with vlc - too heavy screen grabbing, resulting injumpy file, frame loss

I'm using 'vlc/ffmpeg' package to grab the screen and convert it to H.264 file.
The problem arises when the host is heavily loaded. I need to maintain correct time stamps and use the 5 fps (relatively low frame rate). Yet sometimes the resulting file jumps few seconds forward, apparently due to frame loss.
I can deal with the frame loss, it's OK, but I need to duplicate lost frames to maintain correct timing.
My configuration file:
vlc.exe screen:// -I dummy --verbose=2 --one-instance :screen-fps=5 :screen-caching=10000 :sout=#transcode{venc=x264{preset=ultrafast,tune=zerolatency},vcodec=h264,fps=5,vb=3000,width=1024,height=576,acodec=none}:file{dst="C:\tmp\output.mp4"}
What should I add/config to preserve proper time stamps and clip duration?
Many thanks for your help.
OK, I found adding 'copyts' option does exactly what I need.

When recording MP4 using ffmpeg suddenly power off

Now I used C language and ffmpeg realize a multiplex real-time audio and video to MP4 files of the program and everything works fine, but when in the process of reuse of sudden power failure, the recording is MP4 file is damaged, VLC can not play this file.
I think reason is no call to write the trailer function av_write_trailer , causing index and time stamp information lost, I use araxis merge tool compared the successful call av_write_trailer function of file and a no av_write_trailer to call the damaged files and found two different points:
1. Damaged files in the file header box number value not right
2. The damaged file no end of file.
Now I want to repair after power on my program can automatically repair the damaged files, in Google did not find effective methods.
my train of thought is in the normal recording process saves per second a damaged file is missing two information: box number and end of file, save it to a local file, when writing the MP4 file integrity delete this file after, if power off damaged, then in the next power, read the file and the corresponding information to write the damaged files corresponding position to. But now the problem is that I don't know how to save the number of box and the end of the file, I this is feasible? If possible, what should I do? Looking forward to your reply!
The main cause of MP4 file damage is due to header or trailer not written properly on the file , then , whole file become a junk data. Thus none of the media player able to play the broken mp4 file.
So,
First , broken file has to be repaired before playing the file.
there are some applications and tricks available to repair and get the data back
links are given below :
http://grauonline.de/cms2/?page_id=5 (Windows / Mac)(paid :( )
https://github.com/ponchio/untrunc (Linux based OS)(ofcourse,free!!!)
Second, Manually repairing the corrupt file using HEX editor.
Logic behind this hack :
This hack requires a broken mp4 file and good video file where both videos are captured from the same camera .Also its size should be larger than the broken mp4 file.
Open both video file in any HEX editor. Copy trailer part from good video file to broken video file and save it!Done!!
Note : Always have a backup of video file.
follow these links for detailed informations :
http://janit.iki.fi/repair-corrupted-mp4-video/
https://www.lfs.net/forum/thread/45156-Repair-a-corrupt-mp4-file%3F
http://hackaday.com/2015/04/02/manual-data-recovery-with-a-hex-editor/
http://www.hexview.org/hex-repair-corrupt-file.html
Third, Even tough MP4 file has many advantages , this kind of error is unpredictable and difficult to handle it.
Thus , Using format such as MPG and AV_CODEC_ID_MPEG1VIDEO/AV_CODEC_ID_MPEG2VIDEO (FFMPEG) may help to avoid this kind of error. The mentioned MPG format does not require any header/trailer.if there is any sudden power failure MPG file can play the file whatever frames are stored so far.
Note : there are other formats and codec also available with this kind of properties.

Resources