вторник, 23 ноября 2010 г.

extracting flv from a rtmp stream

There are several tools to create flv files from a rtmp stream in linux, two of them being rtmpdump and its fork flvstreamer.
I tried to retrieve a stream where i constantly got ping messages in the process and it just stopped, so i looked for alternative ways.
The one i found was this huge script, which did crash on me: http://ubuntuforums.org/showthread.php?t=1159309
I was unwilling to search for the error or analyze the stream myself, so i came up with the following solution:

  1. capture stream with tcpdump
  2. extract streaming data with tcpflow
  3. pipe data through netcat
  4. connect rtmpdump to netcat
  5. ???
  6. profit!
sudo tcpdump -i ~device~ -p -s 0 -w ~dumpfile~ -v tcp src port 1935
tcpflow ~dumpfile~
# or use wireshrack like that http://www.youtube.com/watch?v=hNe70CbTlng

cat ~streamfile~ |netcat -l 1935&
rtmpdump -r "rtmp://localhost/streamer/stream.flv" -o 


Works quite reliable for me and is not as much code as the script which does not work.
If you end up downloading huge masses of files you can check them using flvtool2 with this script:

#!/bin/sh
if [ "$1" = "" ]
then
 echo "no filename given"
 exit
fi
if [ $(stat -c%s "$1") -eq 0 ]
then
 echo false
 exit
fi
if [ $(stat -c%s "$1") -eq `flvtool2 -P $1 /dev/null|grep filesize|sed 's/[^0-9]//g'` ]
then
 echo true
else
 echo false
fi


It returns "true" or "false", depending if projected filesize and actual filesize are equal, which means an incomplete file returns "false".

source: http://my.opera.com/dduenker/blog/show.dml/21695332