Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor RTMP stream, record if available

I'm looking for a way to monitor activity on a stream so that I can determine if there is anything come through the stream or not. If there is, I'll start recording it using rtmpdump.

I imagine this working by running a cron task that checks a stream every 60 seconds. If it determines that a stream is coming through, then call upon rtmpdump to start recording it. If not, then do nothing and check again in 60 seconds.

Since rtmpdump just kind of errors out when there is no stream data, it doesn't seem like it would be a good idea to attempt to use it to monitor a stream, but perhaps I'm wrong.

It would be easy if I was doing this on a case-by-case basis manually, but I'm trying to automate the task of recording streams automatically if they are available.

Has anyone come across a way to do this? Perhaps some other tools I can use in command line (linux)? If it helps, I'm writing code for this using Ruby/Rails

like image 206
Brian Avatar asked Mar 16 '12 14:03

Brian


1 Answers

You can send the info of the stream to stdout with this command:

rtmpdump -v -m6 -B6 -r "rtmp://#{URL}" -o /tmp/test.flv &> /tmp/rtmpdump.info; cat /tmp/rtmpdump.info

This will try to record just 6 seconds of video and show what the result has been. If the stream is running it will output something like:

INFO: Connected...
Starting Live Stream
For duration: 6.000 sec
INFO: Metadata:
INFO: trackinfo:
INFO:   timescale             50000.00
INFO:   length                19686000.00
INFO:   language              eng
INFO: sampledescription:
INFO:   sampletype            avc1
INFO:   timescale             48000.00
INFO:   length                18900992.00
INFO:   language              eng
INFO: sampledescription:
INFO:   sampletype            mp4a
INFO:   audiochannels         2.00
INFO:   audiosamplerate       48000.00
INFO:   videoframerate        25.00
INFO:   aacaot                2.00
INFO:   avclevel              31.00
INFO:   avcprofile            77.00
INFO:   audiocodecid          mp4a
INFO:   videocodecid          avc1
INFO:   width                 1047.00
INFO:   height                576.00
INFO:   frameWidth            720.00
INFO:   frameHeight           576.00
INFO:   displayWidth          1047.00
INFO:   displayHeight         576.00
INFO:   framerate             25.00
INFO:   moovposition          32.00
INFO:   duration              393.77
2033.613 kB / 6.02 sec (1.5%)
Download may be incomplete (downloaded about 1.50%), try resuming

If the stream is not running/fails it will output something like:

RTMPDump v2.4
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
Connecting ...
INFO: Connected...
ERROR: Closing connection: NetStream.Play.Failed

Here's a working example with a public rtmp stream taken from VLCStreamTester:

require 'open3'

rtmp_url    = "stream.streetclip.tv:1935/live/high-stream"
command     = "rtmpdump -v -m6 -B6 -r \"rtmp://#{rtmp_url}\" -o /tmp/test.flv &> /tmp/rtmpdump.info; cat /tmp/rtmpdump.info"

Open3.popen3(command) do |i, o, e, t|
  puts o.read
end

From here with ruby you can easily check whether one stream is active or not (reading the output for keywords), and then decide to record or not with the usual rtmpdump command. After this you can build more logic and loop through an array of URLs instead of a single one, getting the automation you need.

like image 51
sdude Avatar answered Nov 02 '22 12:11

sdude