Skip to content

Instantly share code, notes, and snippets.

@mikaelz
Last active December 8, 2024 03:53
Show Gist options
  • Save mikaelz/12b394334e3226d67a37399d40751d8f to your computer and use it in GitHub Desktop.
Save mikaelz/12b394334e3226d67a37399d40751d8f to your computer and use it in GitHub Desktop.
#!/bin/bash
ffmpeg -rtsp_transport tcp \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/102 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/202 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/302 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/402 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/502 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/602 \
-filter_complex "
nullsrc=size=1920x1080 [base];
[0:v] setpts=PTS-STARTPTS, scale=w=960:h=360 [upperleft];
[1:v] setpts=PTS-STARTPTS, scale=w=960:h=360 [upperright];
[2:v] setpts=PTS-STARTPTS, scale=w=960:h=360 [middleleft];
[3:v] setpts=PTS-STARTPTS, scale=w=960:h=360 [middleright];
[4:v] setpts=PTS-STARTPTS, scale=w=960:h=360 [lowerleft];
[5:v] setpts=PTS-STARTPTS, scale=w=960:h=360 [lowerright];
[base][upperleft] overlay=x=0:y=0 [tmp1];
[tmp1][upperright] overlay=x=960:y=0 [tmp2];
[tmp2][middleleft] overlay=x=0:y=360 [tmp3];
[tmp3][middleright] overlay=x=960:y=360 [tmp4];
[tmp4][lowerleft] overlay=x=0:y=720 [tmp5];
[tmp5][lowerright] overlay=x=960:y=720
" \
-c:v rawvideo -f nut - | mpv -
#!/bin/bash
# 8 channel input, 4 rows and 2 columns layout
ffmpeg -rtsp_transport tcp \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/102 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/202 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/302 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/402 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/502 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/602 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/702 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3/Streaming/Channels/802 \
-filter_complex "
nullsrc=size=1920x1080 [base];
[0:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [upperleft];
[1:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [upperright];
[2:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [middleleft];
[3:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [middleright];
[4:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [lowerleft];
[5:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [lowerright];
[6:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [centerleft];
[7:v] setpts=PTS-STARTPTS, scale=w=960:h=270 [centerright];
[base][upperleft] overlay=x=0:y=0 [tmp1];
[tmp1][upperright] overlay=x=960:y=0 [tmp2];
[tmp2][middleleft] overlay=x=0:y=270 [tmp3];
[tmp3][middleright] overlay=x=960:y=270 [tmp4];
[tmp4][lowerleft] overlay=x=0:y=540 [tmp5];
[tmp5][lowerright] overlay=x=960:y=540 [tmp6];
[tmp6][centerleft] overlay=x=0:y=810 [tmp7];
[tmp7][centerright] overlay=x=960:y=810
" \
-c:v rawvideo -f nut - | mpv -
ffmpeg -rtsp_transport tcp \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/101 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/201 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/301 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/401 \
-filter_complex "
nullsrc=size=1920x1080 [base];
[0:v] setpts=PTS-STARTPTS, scale=960x540 [upperleft];
[1:v] setpts=PTS-STARTPTS, scale=960x540 [upperright];
[2:v] setpts=PTS-STARTPTS, scale=960x540 [lowerleft];
[3:v] setpts=PTS-STARTPTS, scale=960x540 [lowerright];
[base][upperleft] overlay=shortest=1 [tmp1];
[tmp1][upperright] overlay=shortest=1:x=960 [tmp2];
[tmp2][lowerleft] overlay=shortest=1:y=540 [tmp3];
[tmp3][lowerright] overlay=shortest=1:x=960:y=540
" \
-c:v libx264 -preset superfast -crf 18 -f matroska - | ffplay -
ffmpeg -rtsp_transport tcp \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/102 \ # use substream with lower resolution
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/202 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/302 \
-i rtsp://smartiptv:PASSWORD@192.168.2.3:6911/Streaming/Channels/402 \
-filter_complex "
nullsrc=size=1920x1080 [base];
[0:v] setpts=PTS-STARTPTS, scale=960x540 [upperleft];
[1:v] setpts=PTS-STARTPTS, scale=960x540 [upperright];
[2:v] setpts=PTS-STARTPTS, scale=960x540 [lowerleft];
[3:v] setpts=PTS-STARTPTS, scale=960x540 [lowerright];
[base][upperleft] overlay=shortest=1 [tmp1];
[tmp1][upperright] overlay=shortest=1:x=960 [tmp2];
[tmp2][lowerleft] overlay=shortest=1:y=540 [tmp3];
[tmp3][lowerright] overlay=shortest=1:x=960:y=540
" \
-c:v libx265 \ # check output and see what format the camera use, see `ffmpeg -encoders | grep V`
-preset superfast -crf 18 -f matroska - | ffplay -
@gabonator
Copy link

Dakujem! Presne toto som potreboval!

@csandrade87
Copy link

Thanks man! Works like a charm on with a Intelbras DVR!

@MarcoRavich
Copy link

Just a question: is possible to display-only RTSP streams without re-encoding (with hw decoding, if possible) ?

@mikaelz
Copy link
Author

mikaelz commented Apr 9, 2023

Tried with c:v copy -crf 18 -f matroska - | ffplay - but didn't work for me. But thanks for commenting because I investigated a bit and changed encoder to libx265 and it's much better also faster if I use the sub stream (102, 202, ...) from camera instead main stream (101,201,...).

@MarcoRavich
Copy link

MarcoRavich commented Apr 11, 2023

Tried with c:v copy -crf 18 -f matroska - | ffplay - but didn't work for me.

OK, but why don't use FFPLAY directly in place of piped FFMPEG instead ?
Check here: https://stackoverflow.com/questions/68824225/bad-ffmpeg-performace-compared-to-ffplay-and-vlc

About HW accelleration, check out these interesting posts:

...@cpcloud's ffgrid.sh may inspire too.
Another cool approach is cinegrid by @s7sost.

Someone suggests to use xstack filter for better performances:

Last but not least, it would be great to let FFMPEG get the screen resolution and adapt the mosaic (and source videos) to it:
https://superuser.com/questions/841235/how-do-i-use-ffmpeg-to-get-the-video-resolution

EDIT:
Just played a bit with ffplay and this seems to work flawlessly with local files...
@ffplay -autoexit -f lavfi "movie=<filename1>:seek_point=0.0, drawtext=verdana.ttf:text='TEST':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)-5, scale=960:-1, setpts=PTS-STARTPTS[v0];movie=<filename2>:seek_point=0.0, scale=960:-1, setpts=PTS-STARTPTS[v1];[v0][v1]hstack"

Hope that inspires !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment