Use FFmpeg to convert RSTP stream to MP4

Hi All,

There has been some discussion on the forum how to use FFmpeg to convert the RSTP IP Camera feed to MP4.
Recently, I tried doing the same with my Reolink cameras and I used the same command that has worked fine with my previous IP camera:
ffmpeg -i rtsp://xxx:xxx@192.168.1.57:554/h264Preview_01_main -r 10 -t 60 -vcodec copy -acodec copy -y
But what I found is that the top half of the image is good, but the bottom half is just garbage.
I am not sure if something is missing from my setup, but I don't know how to tell.

@BartButenaers, @kevinGodell I am adding you specifically, since I know you are interested in this topic. I hope you don't mind.

And one more info for those visiting this discussion later. Initially I had issues installing ffmpeg on my rapsberry pi. I have raspbian jessie and it is (was) not possible to install ffmpeg using apt-get install as it appears that is missing from this distro. This is why I used avconv instead which appears to be a copy of ffmpeg. But I did manage to install ffmpeg using Post 7 from user Strontium on this discussion: https://www.raspberrypi.org/forums/viewtopic.php?t=170094. I hope my issues above are not the result of this installation method.

Can you include the full ffmpeg command? What version of ffmpeg? Is the green when outputting jpegs or streaming mp4?

If you are playing the mp4 content and the screen is partially green, just accept it because it is an overpriced cam that does the same thing to me most of the time. I hate to publicly bash that brand, but my super cheap generic chinese $20 ip cams perform way better than the $50 reolink that I got from amazn.

If it is a jpeg issue, then you probably need to pass the jpeg buffer through pipe2jpeg to collect all the pieces befores outputting a complete jpeg.

2 Likes

Hi Kevin,

The full command is this:

ffmpeg -i rtsp://xxx:xxx@192.168.1.57:554/h264Preview_01_main -r 10 -t 60 -vcodec copy -acodec copy -y

My ffmpeg says the following:

ffmpeg version 2.6.9 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.9.2 (Debian 4.9.2-10+deb8u1)
  configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector-strong -                                           Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro' --enable-shared                                            --enable-libmp3lame --enable-gpl --enable-nonfree --enable-libvorbis --enable-p                                           threads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --e                                           nable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-                                           amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --                                           enable-libvpx --enable-libschroedinger --disable-encoder=libschroedinger --enabl                                           e-version3 --enable-libopenjpeg --enable-librtmp --enable-avfilter --enable-libf                                           reetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-libvo-amrwbenc --                                           enable-libaacplus --libdir=/usr/lib/arm-linux-gnueabihf --disable-vda --enable-l                                           ibbluray --enable-libcdio --enable-gnutls --enable-frei0r --enable-openssl --ena                                           ble-libass --enable-libopus --enable-fontconfig --enable-libpulse --disable-mips                                           32r2 --disable-mipsdspr1 --disable-mipsdspr2 --enable-libvidstab --enable-libzvb                                           i --enable-avresample --disable-htmlpages --disable-podpages --enable-libutvideo                                            --enable-libfdk-aac --enable-libx265 --enable-libiec61883 --enable-libdc1394 --                                           disable-altivec --shlibdir=/usr/lib/arm-linux-gnueabihf
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfi                                           le}...

Use -h to get full help or, even better, run 'man ffmpeg'

And this is how the video looks like in VLC. And the garbage part changes from time to time:

And the codec information from VLC:

But I agree, my previous off-brand camera van performing better in RSTP stream conversion.

Still seems like part of that command is cut off. Where is the output params? Are you writing to file?

I had to build ffmpeg on my RaspberryPi 3B, it took a while but it works now very well. All this is discussed in this topic, maybe helps. Also seems you have installed and use a very old version of ffmpeg

Oh sorry, yes the last parameter is the local filename which is added by the exec node from the msg.payload. So that goes to the end.

I think I understand now. Do you constantly re-connect to the cam since the ffmpeg process only runs for 60 seconds and terminated? That particular ip cam craps out if it gets constant new connections. Atleast mine does.

Hi @kevinGodell, sorry for the off topic comment but would you be kind enough to throw a link or two to these cheap cameras you speak of. Cheers. (might be useful to others too)

When I get to my computer, I will log into AliExpress and see if any of my previous retailers still exist there.

1 Like

Unfortunately, the specific items I purchased 3 years ago are no longer listed. The good news is that similar products are even cheaper now. I paid about 26 usd at the time, and they are now about 20.

The cams that worked best for me all seemed to built upon the same internal components and firmware with an interface that looked like this

One cam that just caught my eye is here. Features that I look for are onvif standards, ip66 for outside, poe support for power from a poe switch, and usually 1080p h264 is fine for me.

2 Likes

No this was one time only. I was just using this flow to save a 1 minute clip when somebody rings the bell at the gate. I was not using this to continuously record the video. So hopefully not and issue with the constant reconnection.

I just realized you might need to force tcp connection.
In your command, try adding -rtsp_transport tcp before the -i

1 Like

I also have the misfortune of owning two Reolink RLC-420 5MP cameras and they (among a few other models) have issues with their RTSP stream and mostly only work on the Reolink app. It likely does not help in this situation but I just recently learned the cameras also support RTMP streams and can do that without the issues you described.

On my Android phone and tablet for example I use tinyCam PRO which has handled every other camera I got (5 of different brand and model) but the Reolink cameras I have to view in snapshot mode as the app does not support RTMP and the RTSP stream smears after few seconds of streaming.

Kevin, thank a lot. This transport option has fixed the issue. The mp4 file is now clear and I tested it on my RLC-522, RLC-410 and E1 Zoom and all work fine. How did you figure this out?

For those looking at this discussion in the future, this is the full command that works for my Reolink cameras. The below command will create a 1 minute long (-t 60) video of the live stream:

ffmpeg -rtsp_transport tcp -i rtsp://username:password@192.168.1.56:554/h264Preview_01_main -r 10 -t 60 -vcodec copy -acodec copy -y /home/pi/video.mp4

4 Likes

Check my post above, it is working now. Btw the feed is clear for example in VLC. So I was confident that there will be a solution eventually.

1 Like

It might not be a coincidence then that the only software outside of Reolink's own app where I've got the RTSP stream working has been Shinobi (NVR software) in which Kevin has been involved. It did support RTMP also but for some reason RTSP worked better on the Raspberry docker image I've been testing it with.

This might actually solve the final piece in my quest to setup a light weight Node-RED based NVR solution using object detection (YOLOV4 inference running on a Jetson Nano). Awesome!

@kevinGodell we've been so lucky to have you appear on the forum out of the blue! There's been so many threads with people being more or less clueless with ffmpeg :smile:.

3 Likes

(Being a specialist in off topic :laughing:) here are the links of the PTZ (pan tilt zoom) camera I have which are of the same type (Chinese manufacturer xiongmai technology) as @kevinGodell

My actual ptz cam : https://www.aliexpress.com/item/4000096231458.html?spm=a2g0s.9042311.0.0.27424c4d865FtM
My wish of purchase : https://www.aliexpress.com/item/4000260050022.html?spm=a2g0o.cart.0.0.4c503c00brYTBF&mp=1 you can choose with fix focus or 4x optical zoom and auto tracking :star_struck: (see video).

All use icsee app (my favorite) include human detection. My actual cam is fully controllable with Node red.

4 Likes

So simple setup of the first camera, really easy, thats very clever!

The second camera is so amazingly impressive!!! How can they pack all those features into that little device, unbelievable. I really like the auto-tracking feature, the picture quality, the very fast ptz, object detection, two way audio, wow, I now got appetite to upgrade everything I have, what a solution with YOLO object analyze and detection running in my Jetson Nano that could be!!!

1 Like

I agree this is very cool. This is probably going to be the next norm, object detection. A custom model training would be great, so my camera would be able to tell if it is the UPS, DPD, or other delivery trucks are at my front door. Or the postman who comes in motorbike :slight_smile:

Nice to see the issue is resolved, but in case it's of use to anyone searching, here is my bodged script (worked for years, so never cleaned it up), with stream key removed, of how I both save a feed locally and stream to YouTube live - this is with old Geovision BL2410 cam, but just got a new Annke (Hikvision's cheap brand) 4k camera which will be replacing it - probably through Shinobi with HLS to YT instead.

Worth noting the Geovision provides no audio stream, which was my early problems and this injects a fake aac stream. May be required on some cams.

The exiftool part moved the files into a dated sub-directory every time the stream restarts (or is killed by a daily crontab job) - again primitive, but works.

#!/bin/sh

RTSPNAME=camera1
RTSPIP=192.168.111.111
RTSPPORT=8554

CONVTOOL=ffmpeg
DIR=$HOME/cctv
URL=rtmp://a.rtmp.youtube.com/live2
STREAM=XXXX
#LOGLEVEL="-loglevel fatal -hide_banner"
LOGLEVEL="-hide_banner"

SENDTOYOUTUBE="-strict experimental -bufsize 512k -ab 32k -codec:v copy -codec:a aac -f flv $URL/$STREAM -flvflags no_duration_filesize"
SAVETOFILES="-c copy -map 0:v -f segment -segment_time 300 $RTSPNAME-%03d.mp4"

mkdir $DIR/$RTSPNAME >/dev/null 2>&1
cd $DIR/$RTSPNAME
while true
do
        exiftool -m -d "%Y-%m-%d/$RTSPNAME-%Y-%m-%d-%H:%M:%S.%%e" '-filename<filemodifydate' $RTSPNAME-*.mp4
        ${CONVTOOL} ${LOGLEVEL} -rtsp_transport tcp -i rtsp://$RTSPIP:$RTSPPORT/CH001.sdp $SENDTOYOUTUBE $SAVETOFILES 2>&1 | ts '%F %T' >> /tmp/${CONVTOOL}-$RTSPNAME.log
        sleep 5
done
2 Likes