Dealing with 302 Redirection in FFMPEG - ffmpeg

I am using FFMPEG Library to Stream Audio from RTSP URL. My Sample Streaming URL is:
rtsp://username:password#machine-ip/708314c4eba2a041
And I am using the following command for streaming this RTSP URL:
ffmpeg -i RTSP_URL -f mov C:\FFMPEG_Recordings\bay.mov
So, the above FFMPEG command will capture the media stream from RTSP_URL and will store in bay.mov media file.
Sometimes, I get 302 Redirection Error from the Server which is actually propagating streams. Such as:
[rtsp # 0000000002cc8100] Status 302: Redirecting to rtsp://server-ip:server-port/708314c4eba2a041?token=708114c4e99dbcd1^LA^0^0^0^1427248150^e77149b2a2c209982a74367d0f72c2e11ba6636c
And after this process gets stuck (on Command Prompt) until I press CTRL+C twicely to terminate it in cmd where I run this command.
While It should start streaming from the redirected URL automatically.
I read that It's a FFMPEG's bug on FFMPEG Track and also read about it on FFMPEG Discussion Community but didn't get any solution or workaround for this.
Please guide to overcome this scenario If anyone ever encountered it that what are workarounds for this. Thnaks

Related

How to stop FFMPEG from running if connection to input stream is lost?

I am capturing thumbnails from a webcam RTMP stream every 1 second to JPG files. Here's my command line:
ffmpeg -i rtsp://192.168.1.89:554/11 -f image2 -r 1 thumb%03d.jpg
How can I make FFMPEG die with an error if the input RTMP stream is lost for a given timeout period? Currently, if I lose connection to the webcam, FFMPEG starts throwing "Unknown error" messages, but when the network reconnects, ffmpeg appears to reconnect to the stream, but does not output any more captured frames. I have to manually kill the process and restart it to again start capturing frames.
It would be nice to simply have ffmpeg die when it loses connection for a specific timeout period. Then I could monitor the process and restart when it ends.
Any ideas?
You can set ffmpeg timeout for RTSP :
timeout Set maximum timeout (in seconds) to wait for incoming
connections.
A value of -1 means infinite (default). This option implies the
rtsp_flags set to ‘listen’.
stimeout Set socket TCP I/O timeout in
microseconds.
The solution is actually to put a timeout on the socket connection:
[...] -rtsp_transport tcp -stimeout 30000000 -i rtsp://192.168.1.89:554/11 [...]
(30 seconds in this case)

How to check live stream is still alive use "ffprobe" command?

I want to schedule a job script to check a live stream is still alive use "ffprobe" command. So that I can change database state for those steam already dead.
I tried the command:
ffprobe -v quiet -print_format json -show_streams rtmp://xxxx
but when the stream is not avaiable, the command will hang.
I tried add -timeout argument, but still cannot work properly.
The timeout option seems to be for listen mode. I would try to run ffmpeg using the timeout command or use something similar for the framework/language your using.
So for example:
timeout 10s ffprobe -v quiet ...
And maybe even use --kill-after. And then look at the exit code to determinate what happened.

Recording from online stream and listening to it at the same time (ffmpeg / ffplay)

Sometimes I like to record programmes from online radio channels, live or archived streams too. When there is no interesting actual programmes in the radios, I also would like listening to it at the same time while recording. I am using such command lines, which is called from Ruby script - to help parsing radios' timetables / programme pages and constructing the proper URLs of archived programmes which usually contains some timecode, such as 20160616_083000.mp3, etc.
So my command line to call from Ruby script looks like:
programmes.each{|datepart,programme_length|
cmd=%Q{ffmpeg -y -i http://example.com/stream/#{datepart}.mp3 -t #{programme_length} -c:a libmp3lame -b:a 160k "#{fname}" -c copy -t #{programme_length} -f mp3 -f rtp rtp://127.0.0.1:8888}
system cmd
}
It resides in a loop to record the previously parsed and selected programmes. Of cource the programmes are recorded properly and at the same time ffmpeg streams it as an mp3 rtp stream as well on localhost at the given port. In another terminal window I connect to the streamed data with one-liner as follows:
while true; do ffplay -i rtp://127.0.0.1:8888 -autoexit; done
I am using the -autoexit switch which should be stop playing the stream when it is ended and the "while" loop should be connect again to the new stream which is served by the programme recording "each" loop. Unfortunately it keeps playing after the end, and doesn't initiate a new connection to the newly started stream. How to use ffplay properly to stop playing after rtp stream is ended and let it connect again to the new stream?

Process UDP packets and forward them at the same time

I have a question that is very similar to this question, but the solution provided is not working for me. And actually I want to do something a little different, so maybe there's a better way.
My hope is to send UDP unicast packets (a video stream) to a server. The server will forward these packets to another computer. Optionally, someone logged into the server will be able to watch the video stream using something like mplayer. I'm pretty sure I could stream the video both to the server and to the final destination from the video source, but I really want the server to be in control of that.
First Attempt:
Downloaded nmap to get the ncat tool. Thought I could use that to download and pipe bytes to mplayer and forward with another ncat process. I can pipe to mplayer and it works great, I just can't forward at the same time. If I was using Linux, apparently I could use tee and process substitution, but I'm looking for a Windows solution.
Second Attempt
I found a solution that kind of implied piping to multiple programs would be possible in PowerShell. But what I found is that binary piping wasn't going to work.
Third Attempt
I attempted to program a tee-like executable that would tee to two programs. This was about as close as I got, but the video streams seemed to get a bit corrupted. I think it was more of a performance issue than anything.
Fourth Attempt
Now I'm at the point where I've discovered socat which led me to the question I linked to. It seems odd, but I thought a solution would be for my server to forward packets to the destination AND forward packets to a different port on the server which would be used with netcat or socat to pipe into mplayer. I'd rather not use up another port on the server, but if that's what I have to do, then ok. But like I said, the solution from the other question didn't seem to work. Mplayer started to buffer, then stopped very shortly.
Ran each of these in a separate cmd window in this order. Notice I wasn't trying to forward to the destination computer yet, just trying to get mplayer to work on the server.
socat UDP4-LISTEN:5000 UDP-DATAGRAM:224.10.10.10:5001
socat UDP4-RECVFROM:5001,ip-add-membership=224.10.10.10:0.0.0.0,reuseaddr,fork UDP-DATAGRAM:192.168.16.33:5002
ncat -l -u -p 5002 | mplayer -vo direct3d -cache 1024 -
I seem to get about 924 bytes received according to MPlayer output.
I'm fresh out of ideas, though I will continue learning about socat. I assume I am not running socat correctly, but I'm not really sure where the issue is. If anyone can point out my mistake or offer an alternative solution I'd really appreciate it. Thanks.
Edit:
I did some more testing and if I change the second socat to do this:
socat UDP4-RECVFROM:5001,ip-add-membership=224.10.10.10:0.0.0.0,reuseaddr,fork - | mplayer -vo direct3d -cache 1024 -
I find that it will play the stream from the multicast address. But apparently forwarding to the unicast address on port 5002 is what's not working.
Ok, I'm not really sure why this is, but the netcat process that worked fine to receive the UDP packets from the sender and pipe to MPlayer does not work here. I'm not sure what the problem is.
So instead of:
ncat -l -u -p 5002 | mplayer -vo direct3d -cache 1024 -
I had to use:
socat UDP4-RECVFROM:5002,fork - | mplayer -vo direct3d -cache 1024 -
Though I do still have one small problem. If I close MPlayer, I get a ton of messages like:
2015/02/24 11:14:04 socat[5888] E write(1, 0x800432e8, 986): Broken
pipe
Also if I hit Ctrl-C from the command line it takes a few tries and then when MPlayer stops, my keyboard no longer works and I have to start a new cmd window.

Running mpg123 with FIFO control?

I need to run mpg123 with a single file, such that it will autostart and autoclose, like it normally does, however, I need to be able to override this default behavior with commands sent to a fifo file.
I had been running mpg123 filename.mp3 from a script, and simply waiting for it to finish before moving on. However, I'd like another script to be able to pause playback, control volume, or kill the process early, depending on the user's input.
mpg123 -R --fifo /srv/http/newsctl filename.mp3 seems to start mpg123 and create the pipe, but does not start playback.
How do I make this work?
Unfortunately mpg123 is unable to play a specified file when -R argument is used.
To start the playback you have to load a file using created fifo.
FIFO_MPG='/srv/http/newsctl'
mpg123 -R --fifo "$FIFO_MPG"
echo 'load filename.mp3' >> "$FIFO_MPG"
Also I suggest you to silence verbose output by using
echo 'silence' >> "$FIFO_MPG"
I hope it is not too late. Good luck! ;)

Resources