# Use FFmpeg to convert RSTP stream to MP4

**URL:** <https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883>\
**Category:** General\
**Created:** [14 September 2020 15:49 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883 "2020-09-14T15:49:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [14 September 2020 15:49 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/1 "2020-09-14T15:49:46Z")

</div>

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](https://www.raspberrypi.org/forums/viewtopic.php?t=170094). I hope my issues above are not the result of this installation method.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [14 September 2020 15:56 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/2 "2020-09-14T15:56:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [14 September 2020 16:54 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/3 "2020-09-14T16:54:54Z")

</div>

Hi Kevin,

The full command is this:

```auto
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:

```auto
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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/6109871b4f40bb82d134df2052a6c18b3a95076b.jpeg)

And the codec information from VLC:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91eef20e0ac5b84f07a6996dd6043059f3f6d622.png)

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

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [14 September 2020 17:00 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/4 "2020-09-14T17:00:24Z")

</div>

> [@nygma2004](#):
>
> The full command is this:
> 
> ```auto
> ffmpeg -i rtsp://xxx:xxx@192.168.1.57:554/h264Preview_01_main -r 10 -t 60 -vcodec copy -acodec copy -y
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [14 September 2020 17:26 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/5 "2020-09-14T17:26:48Z")

</div>

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

> [@Display camera ip in dashboard and store video in raspberry](https://discourse.nodered.org/t/display-camera-ip-in-dashboard-and-store-video-in-raspberry/6349/62):
>
> I'm currently following this instruction for one of my RPi3B, not finished yet but I believe you have to build from source like it's described in this guide

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [14 September 2020 19:08 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/6 "2020-09-14T19:08:24Z")

</div>

> [@kevinGodell](#):
>
> > [@nygma2004](#):
> >
> > The full command is this:
> > 
> > ```auto
> > ffmpeg -i rtsp://xxx:xxx@192.168.1.57:554/h264Preview_01_main -r 10 -t 60 -vcodec copy -acodec copy -y
> > 
> > ```
> 
> Still seems like part of that command is cut off. Where is the output params? Are you writing to file?

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.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [14 September 2020 19:25 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/7 "2020-09-14T19:25:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 September 2020 19:33 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/8 "2020-09-14T19:33:23Z")

</div>

> [@kevinGodell](#):
>
> but my super cheap generic chinese $20 ip cams perform way better than the $50 reolink that I got from amazn

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)

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [14 September 2020 19:43 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/9 "2020-09-14T19:43:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [14 September 2020 21:39 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/10 "2020-09-14T21:39:16Z")

</div>

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

 ![](https://ae01.alicdn.com/kf/Hb6c89f95e82543fd9041c492551e0bf9R.png)

One cam that just caught my eye is [here](https://www.aliexpress.com/item/33060204842.html?spm=a2g0o.detail.1000014.59.25e4792alj53A8&gps-id=pcDetailBottomMoreOtherSeller&scm=1007.14452.184418.0&scm_id=1007.14452.184418.0&scm-url=1007.14452.184418.0&pvid=7b5c8582-b62c-4e00-9f56-2f92eaeede52&_t=gps-id:pcDetailBottomMoreOtherSeller,scm-url:1007.14452.184418.0,pvid:7b5c8582-b62c-4e00-9f56-2f92eaeede52,tpp_buckets:668%230%23131923%2366_668%23808%234094%23387_668%23888%233325%2315_4452%230%23184418%230_4452%233474%2315675%23297_4452%233098%239599%23741_4452%233564%2316062%23831_668%232846%238112%23507_668%232717%237564%23692_668%231000022185%231000066059%230_668%233422%2315392%2328_4976%230%23188500%230_4976%232711%237538%23945_4976%233104%239652%231_4976%233141%239887%233&fromDetail). 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.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [15 September 2020 05:47 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/11 "2020-09-15T05:47:45Z")

</div>

> [@kevinGodell](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [15 September 2020 10:18 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/12 "2020-09-15T10:18:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [15 September 2020 20:32 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/13 "2020-09-15T20:32:27Z")

</div>

> [@nygma2004](#):
>
> 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:

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.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [16 September 2020 06:01 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/14 "2020-09-16T06:01:20Z")

</div>

> [@kevinGodell](#):
>
> I just realized you might need to force tcp connection.  
> In your command, try adding `-rtsp_transport tcp` before the `-i`

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

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [16 September 2020 06:03 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/15 "2020-09-16T06:03:36Z")

</div>

> [@ristomatti](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [16 September 2020 06:42 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/16 "2020-09-16T06:42:19Z")

</div>

> [@nygma2004](#):
>
> 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?

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 😄.

---

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [16 September 2020 07:49 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/17 "2020-09-16T07:49:53Z")

</div>

> [@Steve-Mcl](#):
>
> throw a link or two to these cheap cameras you speak of

(Being a specialist in off topic 😆) 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](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](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 🤩 (see video).

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [16 September 2020 09:00 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/18 "2020-09-16T09:00:19Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [16 September 2020 09:47 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/19 "2020-09-16T09:47:21Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![pgalbavy](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@pgalbavy](https://discourse.nodered.org/u/pgalbavy)\
**Post date:** [16 September 2020 11:16 UTC](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883/20 "2020-09-16T11:16:32Z")

</div>

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.

```auto
#!/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

```

[Next page](https://discourse.nodered.org/t/use-ffmpeg-to-convert-rstp-stream-to-mp4/32883.md?page=2)
