H.264対応IPカメラのRTSPストリーム配信をiPyCamでシミュレーションしたい~バーチャルIPカメラ~

ffmpeg

環境

当方の環境は以下である。
Windows 11 + x86_64 + wsl2 + ubuntu24.04 + zsh

目的

最終的には、IPカメラをリアルタイムで画像処理したいにゅんちゅ。そのためにまずは、ストリームをキャプチャするプログラムを書きたい。しかし、手元にIPカメラの実物がないため、バーチャルIPカメラでテストできる環境を構築する。

ツール

olkhamさんの素晴らしい取り組みを活用させていただきます。
olkham/IPyCam – MIT license

インストール方法

READMEを参考に、pipenvで環境構築した。

zsh

% git clone git@github.com:olkham/IPyCam.git
% cd IPyCam
% git checkout v1.3.0
% pipenv --python 3.13.12 #任意のバージョン
% pipenv run bash setup.sh
===============================================
IPyCam Setup Script
===============================================

Checking for python3-venv...

[1/4] Creating virtual environment...
Virtual environment created successfully

[2/4] Activating virtual environment...

[3/4] Installing dependencies...
・・・省略・・・
Successfully installed pip-26.1.2
・・・省略・・・
Successfully installed numpy-2.5.1 opencv-python-5.0.0.93

[4/4] Installing IPyCam package...
・・・省略・・・
Successfully built ipycam
Installing collected packages: ipycam
Successfully installed ipycam-1.3.0

[5/6] Checking for go2rtc...
go2rtc is not installed

Would you like to download go2rtc? (Y/N): Y

Downloading go2rtc v1.9.9 for go2rtc_linux_amd64...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 5802k  100 5802k    0     0  2619k      0  0:00:02  0:00:02 --:--:-- 4079k
Extracting go2rtc...
go2rtc has been installed

[6/6] Checking for ffmpeg...
ffmpeg is not installed

Would you like to install ffmpeg via package manager? (Y/N): Y

Installing ffmpeg...
[sudo] password for hoge:
・・・省略・・・
ffmpeg has been installed

===============================================
Setup completed successfully!
===============================================

To activate the environment, run:
  source .venv/bin/activate

To run the camera, use:
  python -m ipycam

%

シミュレーション方法

早速、RTSPサーバーを構築し、1280×720の30fpsカラー映像をH.264でエンコードして、RTSP over TCPでストリーム配信してみる。
examples/README.mdが参考になった。

zsh

% pipenv shell
% source .venv/bin/activate
%  ./go2rtc --config ipycam/go2rtc.yaml &
[1] (pid)
18:34:23.769 INF go2rtc platform=linux/amd64 revision=fa580c5 version=1.9.9
18:34:23.770 INF config path=/home/hoge/IPyCam/ipycam/go2rtc.yaml
18:34:23.770 INF [rtsp] listen addr=:8554
18:34:23.771 INF [api] listen addr=:1984
18:34:23.772 INF [webrtc] listen addr=:8555
18:34:23.774 INF [rtmp] listen addr=:1935

% python examples/generated_content.py

============================================================
  Streaming generated content
============================================================
  Web UI:      http://172.26.230.129:8080/
  RTSP Stream: rtsp://172.26.230.129:8554/video_main
============================================================

Press Ctrl+C to stop

Stats: 142 frames, 15.7 fps
Stats: 292 frames, 14.9 fps
Stats: 442 frames, 14.9 fps
・・・

確認方法

ffmpegでキャプチャし、3フレームをpngに保存してみる。

zsh

% ffmpeg -rtsp_transport tcp -i rtsp://172.26.230.129:8554/video_main -vframes 3 -pix_fmt rgb24 frame_%04d.png

左上の文字はバーチャルIPカメラを起動してからのフレームの通し番号である。グラデーションが変化する映像が流れている。

frame_0001.png

続いて、90フレーム=3秒のmp4として保存してみる。

zsh

% ffmpeg -rtsp_transport tcp -i rtsp://172.26.230.129:8554/video_main -vframes 90 -c:v copy output.mp4

これでストリームをキャプチャするプログラムのテストができるにゅんちゅ!