ffmpeg/ffserverで保存した動画ファイルをRTSP配信を試す
※ 当ページには【広告/PR】を含む場合があります。
2021/07/20
ffmpegのインストール
$ sudo apt install ffmpeg
/dev/video0
$ ffmpeg -f video4linux2 -list_formats all -i /dev/video0
ffmpeg version 4.1.6-1~deb10u1+rpt2 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 8 (Raspbian 8.3.0-6+rpi1)
configuration: --prefix=/usr --extra-version='1~deb10u1+rpt2' --toolchain=hardened --incdir=/usr/include/arm-linux-gnueabihf --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-omx-rpi --enable-mmal --enable-neon --enable-rpi --enable-vout-drm --enable-v4l2-request --enable-libudev --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared --libdir=/usr/lib/arm-linux-gnueabihf --cpu=arm1176jzf-s --arch=arm
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
[video4linux2,v4l2 @ 0x510eb0] Raw : yuv420p : Planar YUV 4:2:0 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : yuyv422 : YUYV 4:2:2 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : rgb24 : 24-bit RGB 8-8-8 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Compressed: mjpeg : JFIF JPEG : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Compressed: h264 : H.264 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Compressed: mjpeg : Motion-JPEG : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : Unsupported : YVYU 4:2:2 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : Unsupported : VYUY 4:2:2 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : uyvy422 : UYVY 4:2:2 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : nv12 : Y/CbCr 4:2:0 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : bgr24 : 24-bit BGR 8-8-8 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : yuv420p : Planar YVU 4:2:0 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : Unsupported : Y/CrCb 4:2:0 : {32-3280, 2}x{32-2464, 2}
[video4linux2,v4l2 @ 0x510eb0] Raw : Unsupported : 32-bit XBGR 8-8-8-8 : {32-3280, 2}x{32-2464, 2}
/dev/video0: Immediate exit requested
配信用のテスト動画を撮る
デスクトップ画面をキャプチャする
DISPLAY
$ echo $DISPLAY
:0.0
-i :0.0
-f x11grab
-i desktop
-f gdigrab
$ ffmpeg -s 1920x1080 \
-r 30 \
-f x11grab \
-i :0.0 \
-c:v libx264 \
-pix_fmt yuv420p desktop.mp4
libx264
-pix_fmt yuv420p
desktop.mp4
ffmpeg --help
Linuxカメラから動画を撮る
$ ffmpeg \
-f v4l2 \
-s 640x480 \
-thread_queue_size 8192 \
-i /dev/video0 \
-c:v h264_omx \
-b:v 768k \
output.mp4
ffserverを立てる
ffserverの注意点
ffserver用のDockerコンテナを作成する
FROM alpine:3.8
RUN apk add --no-cache ffmpeg
COPY ffserver.conf /etc/
EXPOSE 554 8090
ENTRYPOINT ["ffserver"]
version: '3'
services:
ffserver:
build: .
container_name: my_ffserver
image: ffserver:alpine3.8
ports:
- "554:554"
- "8090:8090"
volumes:
- ./ffserver.conf:/etc/ffserver.conf
- ./data:/data
restart: always
ffserver.conf
HTTPPort 8090
HTTPBindAddress 0.0.0.0
RTSPPort 554
RTSPBindAddress 0.0.0.0
MaxClients 100
MaxBandwidth 100000
CustomLog -
<Feed feed.ffm>
File /data/feed.ffm
</Feed>
<Stream video.mp4>
Format rtp
Feed feed.ffm
VideoCodec libx264
VideoFrameRate 24
VideoBitRate 100
VideoSize 640x480
AVPresetVideo default
AVPresetVideo baseline
AVOptionVideo flags +global_header
NoAudio
ACL allow 127.0.0.1
ACL allow 192.168.0.0 192.168.255.255
</Stream>
<Stream status.html>
Format status
ACL allow 127.0.0.1
ACL allow 192.168.0.0 192.168.255.255
</Stream>
data
video.mp4
$ tree
.
├── Dockerfile
├── data
│ └── video.mp4
├── docker-compose.yml
└── ffserver.conf
$ docker-compose build
$ docker-compose up -d
$ docker-compose down
テスト用の動画をコンテナ内から配信してみる
data/video.mp4
$ ffmpeg -re -i output.mp4 http://localhost:8090/feed.ffm
docker-compose exec <サービス名>
$ docker-compose exec ffserver ffmpeg -re -i data/video.mp4 http://localhost:8090/feed.ffm
$ ffplay -i rtsp://localhost:554/video.mp4
rtsp://localhost:554/video.mp4
まとめ
参考サイト
記事を書いた人
ナンデモ系エンジニア
電子工作を身近に知っていただけるように、材料調達からDIYのハウツーまで気になったところをできるだけ細かく記事にしてブログ配信してます。
カテゴリー