How to View RTSP by Raspberry Pi 3 Using ffplay (FFMPEG)

The following article is outdated. For the Raspberry Pi OS only, you can use VLC to watch RTSP as of 3 November 2023.

Original Article follows;

For Raspberry Pis, there are many outdated or sometimes harmful guides to watching video streams (RTSP) generated by webcams. The biggest issue obstructing the approach is that Debian removed RTSP capability from the VLC media player. People who want to simply watch RTSP on the Raspberry Pi are straying.

The goal of this article is to run ffplay on the Raspberry Pi OS automatically.

Disclaimer

The first purpose of this article is to record what I did. However, I am happy if this will be helpful to someone, no matter how much or little. I would like to state this article is based on GPLv3. No warranty at all.

Prior Information

  • The information in this article is tested on Raspberry Pi 3 Model B (RPi3) and Raspberry Pi 3 Model B+ (RPi3+) both of them have 1GB RAM. Raspberry Pi 4 Model B is not tested but may work as well.
  • URL to webcam is in the form of “rtsp://user_id:password@ipaddress/designated_string”
  • Used OS is Raspberry Pi OS bullseye. The next generation, bookworm, will be released soon. This article will or will not be updated after bookworm release.

Raspberry Pi OS installation

Details of the OS installation are omitted in this article because there are many guides, including official and unofficial ones. It is lucky if you find one that suits to you. The installation is not difficult, so you can manage to install it even if the guide you found is not friendly.

The recommendable points are as follows:

  • Install Raspberry Pi OS (64bit) full version, not the LITE version.
  • Enable auto-login (just according to the default).
  • Disable screen blanking.
  • Enable the ssh server (sshd).

The following section assumes the WiFi setup is finished and that all packages have been updated.

Recommended Option: Zram

This is not a must, but I strongly recommend installing ZRAM. Many distros enable ZRAM on their default installation today, but Raspberry Pi OS bullseye doesn’t.

Installation is not difficult. Just install zram-tools with an apt command like,

sudo apt install zram-tools

then swap on the RAM with compression starts functioning. ZRAM has extremely high priority, so the bullseye default, swap-on-file, will never work again. Because the swap file remains just a huge, redundant file, you may want to remove it. It is removed as follows:

sudo systemctl stop dphys-swapfile
sudo systemctl disable dphys-swapfile
sudo rm -v /var/swap

Test ffplay

During the OS installation, you have a mouse, a keyboard, and a monitor connected to the RPi3(+). Keep them connected until the set-up finishes.

The program to view RTSP, ffplay, is included in the FFMPEG package that comes with the Raspberry Pi OS. Open a terminal and execute the next command as a user.

ffplay -fs -loglevel quiet rtsp://user_id:password@ip_address/designated_string

where use_id, password, ip_address, and designated_string are those of the corresponding webcam.

If you get a full-screen video stream, you are one step closer. If you can’t, I can just say “It depends.”

Type “q” to quit the video.

Next, create a shell script file as a user with your favorite editor. You can name it whatever you like. For simplicity, it is named “run_ffplay.sh” here. Inside, it is like,

#!/bin/sh
sleep 20
/usr/bin/ffplay -fs -loglevel quiet rtsp://user_id:password@ip_address/designated_string >/dev/null 2>&1 &

This script sleeps 20 seconds before the execution of ffplay. I don’t know if 20 seconds is optimum. Please trim it if you like.

You can place the script file wherever you want, but your home directory is safe. Then give it execute permission.

chmod a+x run_ffplay.sh

Just run the script to see if you get a full-screen video again.

./run_ffplay.sh

Again, type “q” to quit.

Autostart

You need to create an autostart configuration file in the directory "~/.config/autostart/". By the default of bullseye installation, "~/.config" has been created then you will need,

mkdir ~/.config/autostart

Then create a file named “ffplay.desktop” in the directory with your editor. The contents will like,

[Desktop Entry]
Type=Application
Name=ffplay
Exec=/THE/PATH/TO/run_ffplay.sh

I know I’m being nosy, but /THE/PATH/TO should be the path to run_ffplay.sh. All done; just cross-finger and sudo reboot; exit1 ; exit may be redundant..

If success, you can remove the mouse and the keyboard.

Shutdown

Unfortunately, there is no smart shutdown method provided without a keyboard and a mouse. Login to the box via ssh from another PC, then execute the command sudo poweroff2Or simply poweroff may work.. Installing an electrical switch on the GPIO and configuring it will be fun. LIRC will be a smarter alternative.

Conclusion

Since Debian removed RTSP capability from the VLC media player, many people are struggling. You can use ffplay of the FFMPEG package as an alternative.

Raspberry Pi 3 でRTSPを見る(結論)

使い道のないRaspberry Pi 3 Model B (RPi3) やRaspberry Pi 3 Model B+ (RPi3+)で,RTSPを視聴する方法の結論としては,

ffplay (FFMPEG)で見る

で最終決定しました1その後,Raspberry Pi OS配布のVLCでRTSP視聴ができるように復旧しました..唯一懸念されていた時間遅れの蓄積は,その後半日ほど連続運転したところ,不思議と解消されました2たぶん,下記のスクリプトのように,コンソールにlogを出さないようにしたためかもしれません.それでも多少は遅れます.

FFMPEGが使えるならばRaspberry Pi OSでも行けるはずですが,いまさら面倒なので3と言いつつ後日試しました.ffplayだけのためにインストールするなら,Raspberry Pi OSが一番楽で,起動も早いのでお勧めです.,RPi3ではManjaro ARM xfce, RPi3+ではManjaro ARM KDE Plasmaでこのまま行きます.

自動起動については,

#!/bin/sh
sleep 30
DISPLAY=:0 /usr/bin/ffplay rtsp://ユーザー名:パスワード@ホストのFQDN/指定された文字列 -loglevel quiet -fs > /dev/null 2>&1 &

というスクリプトを実行可能にして適当な場所に置いて設定からAutostartに指定したら,xfceもKDE Plasmaもうまくいきました.sleep 30については要調整です.フルスクリーンオプション-fsはお好みで.

なお,xfceでは設定のScreen saverをOFFが機能しますが,KDE Plasmaはそのスイッチが効かない4一年前にそうだったのですがその後確認はしていません😅ようなので,ワークアラウンドとして,.xprofile

DISPLAY=:0 xset -dpms
DISPLAY=:0 xset s off

を書いてます.

macOSとiPhoneの同期が終わらない

どうもiPhone 14のiOSを17.0.xに上げてからだと思いますが,Mac mini (Intel, macOS 13.6)と同期しようとしても,最後の「項目がコピーされるのを待っています」が終了しません.iOSのほうはとっくに同期の処理が終わっているようで,バッテリーの表示のところをプルダウンしてもぐるぐるは見えませんし,写真アプリでもMac側にしかない写真が送られないままで「同期中」のサインもありません.

ながらくMacとiPhone (iPod touc, iPad)を使ってきて,同期がうまく行かないことはこれまでもありました.非常に不便でなかなかAppleは直してくれませんが,あんまり気にするとストレスになるので気にしないことにします.

追記: 動きました

何回か試したら同期処理が完了しました.しばらく様子見ます.

Manjaro ARMでもVLCでRTSPはNGに

ここだけの話にしておいてください.2023年10月3日(火)現在ですが,Manjaro ARMの配布しているVLC最新版(3.0.18-16)は,RTSPの視聴が可能です.Raspberry Pi 4 Model B (RPi4)で確認しました.

で,めでたしめでたしと行きたかったのですが,他の2台のManjaro ARMを最新版にしたところ,VLCによるRTSPの視聴はできなくなりました.

どうも,視聴できる方がどこかおかしいようです.パッケージ管理のデータベース上は最新版にアップデートされているはずが実際は古いままだとか.

FFMPEGも微妙

かつて,VLCでなくコマンドラインのmplayerPython3のライブラリーを試したことがありましたが,いずれも遅延がどんどん大きくなって,実時間の監視用には使えないことがわかりました.Raspberry Pi 3 Model B (RPi3) とRaspberry Pi 3 Model B+ (RPi3+) でのテストでした.

今回,FFMPEGを試してないことを思い出し同じくRPi3, RPi3+でFFMPEGのコマンドラインプレーヤーffplayを試してみましたが,mplayer, Python3のライブラリーほどではないものの微妙に遅延していきます.

RPi3でffplayを動かしていると,一時間に数十秒遅れて,数時間で数分遅れという状況になります1ちなみにCore i7のデスクトップ機ではffplayでも遅延が増大することはありません..一時間ごとに再起動すれば良いかもしれませんが,あんまりスマートではありません.

いろいろ試すとVLCがよくできているのが解るばかりで弱ります.

いずれもあくまで当社調べです.

「VLCでrtspを見る」は依然難航中

なんだか相変わらず断片的な書きなぐりなので,何をどうしたくて今どういう問題に直面しているのかというのを一旦まとめます.

最終目標としては,安いモニターカメラが出力するRTSP (Real Time Streaming Protocol)をRaspberry Pi 3 Model B (以下RPi3.Raspberry Pi 3 Model B+はRPi3+)にて視聴するというプロジェクトです.

昨年の段階ではRPi3およびRPi3+にManjaro ARMをインストールして,Manjaroが配布する(もとはArch Linux)VLCで視聴できていました.

しかし,その後一年間モニターカメラの運用を停止していた間にManjaroのVLCもDebianやUbuntu等と同様にRTSPのサポートを止めてしまったようで,RPi3(+)で視聴する方法がなくなってしまいました.

それで現在RPi3(+)でRTSPサポートの付いたVLCを動かす方法を色々探しています.

VLCについて明らかなのは,「本家のVLCはRTSPの視聴が可能」ということです.直接本家がバイナリーを配布しているmacOS版は問題なくモニターカメラからのRTSPを音声込みで再生できています.Windows版については確認していませんが,たぶん同様に問題ないと思います.

一方,Linuxについては,各distroのbuildを尊重しているようです.

SlackwareはdistroにはVLCは含まれていません.SlackBuildsにスクリプトがあるので,sbopkgでbuildできるはずですが,必要なパッケージが35もあってめげてしまいます.それでも気を取り直してまずVLC本体からbuildして,エラーが出るパッケージ(=必須のパッケージ)をbuild & installしていこうと方針を立て,

sbopkg -b vlc

とすると,ソースの配布サイトのアドレスがresolvできないというどうしようもないエラーが出てしまいます.

なお,前述のVLCのダウンロードサイトには,Slackwereのバイナリーのリンクもあります.これはSlackwareに貢献著しいAlienさんがbuildしたもので,試したところ,32bit版も64bit版も問題なくRTSPを再生できます.もし,Alienさんに限らずだれかがARM用もbuildして配布していれば解決なんですが😅

仮想マシンのSlackware 15.0 (32bit版)でRTSPを再生するAlienさんbuildのVLC