v4l2loopback

提供: ArchWiki
2023年6月29日 (木) 19:52時点におけるAshMyzk (トーク | 投稿記録)による版 (カテゴリを修正)
ナビゲーションに移動 検索に移動

関連記事

プロジェクトのリポジトリより:

v4l2loopback - V4L2 ループバックデバイスを作成するためのカーネルモジュール
このモジュールにより、"仮想ビデオデバイス"を作成することができます。通常の (v4l2) アプリケーションは仮想ビデオデバイスを通常のビデオデバイスかのように読み取りますが、ビデオはキャプチャカードなどから読み取られずに他のアプリケーションによって生成されます。

インストール

v4l2loopback-dkms パッケージに加えて対象のカーネルのヘッダーファイルインストールしてください (Dynamic Kernel Module Support#インストール を参照)。例えば、デフォルトの linux カーネルの場合、ヘッダファイルは linux-headers です。他のカーネルにはそれぞれのヘッダファイルパッケージがあります

コマンドラインユーティリティは v4l2loopback-utilsv4l-utils によって提供されています。

この記事で紹介している使用例の多くは ffmpeg も使用します。

カーネルモジュールをロードする

v4l2loopback カーネルモジュールDynamic Kernel Module Support でロードすることができます:

# modprobe v4l2loopback

v4l2-ctl を使って全てのビデオデバイスを一覧表示することができます。v4l2loopback デバイスが新たに現れるはずです:

$ v4l2-ctl --list-devices

v4l2loopback は、デバイスの作成に関する様々なオプションを指定してロードすることができます:

# modprobe v4l2loopback video_nr=9 card_label=Video-Loopback exclusive_caps=1

このコマンドにより、/dev/video9 がループバックデバイスとして作成されます。exclusive_caps=1 は一部の Chromium/WebRTC ベースのアプリケーション (jitsi-meet-desktop-binAURzoomAUR など) で必要です。その他のオプションは公式のドキュメントで確認できます。

モジュールがすでにロードされている場合、上記のコマンドを実行してもおそらく何も起こりません。モジュールをまずアンロードし、その後にロードし直す必要があります:

# modprobe -r v4l2loopback

詳細は カーネルモジュール#手動でモジュールを扱う を参照してください。

ヒント: 公式のドキュメントで説明されているように、v4l2loopback-ctl を使うことでカーネルモジュールを再ロードすることなくデバイスを動的に管理することもできます。

devices 引数を使うことで、複数のループバックデバイスを作成することができます。それぞれのデバイスのオプションはコンマで区切って指定します。以下のコマンドは、exclusive_caps が有効化された 3 つのループバックデバイスを作成します。ループデバイスは、/dev/video8/dev/video9、そして利用可能な最初の /dev/videoX となります。

# modprobe v4l2loopback devices=3 video_nr=8,9 exclusive_caps=1,1,1 card_label="Loopback-1,Loopback-2,Loopback-3"

システムを再起動するとモジュールがアンロードされてしまいますが、起動時にカスタムのデバイスオプションを指定してモジュールを自動的にロードさせることができます。これは、ループバックデバイスが内部ウェブカメラとして使用されている場合に便利です。詳細は カーネルモジュール#モジュールの自動ロード公式のドキュメントを参照してください

v4l2loopback のビデオフィードを見る

ノート: v4l2loopback video devices only output video, if there is a stream piped into them like the examples in #Use cases do. Otherwise many applications won't even detect the video devices.

A loopback video device at /dev/video0 can be viewed for testing with ffplay (part of ffmpeg), mpv, or gst-launch (part of gstreamer):

$ ffplay /dev/video0
$ mpv av://v4l2:/dev/video0
$ gst-launch-1.0 -v v4l2src device=/dev/video0 ! glimagesink

As noted in #Loading the kernel module, when exclusive_caps=1 is used Chromium and applications like jitsi-meet-desktop-binAUR and zoomAUR should be able to use the loopback device as a virtual webcam, while a video stream is piped into them. In a browser the virtual webcam can be tested on webcamtests.com.

See also Webcam setup#Applications.

ヒント: If you pipe a video stream to the video device and your application doesn't detect it, try it with and without exclusive_caps=1.
ノート:
  • Various other issues with Firefox have been reported.
  • It has been reported, that skypeforlinux-stable-binAUR detects the virtual video device, but outputs a blank screen.

使用方法

ノート: Because v4l2loopback devices can be handled as v4l2 devices, numerous applications can interact with them. For more information about v4l2 devices see the Webcam setup article.

Generally, FFmpeg is very convenient in combination with v4l2loopback, because it can transcode various input streams to the widely supported yuv420p pixel format and pipe the output to a v4l2loopback device by using the ffmpeg arguments -vf format=yuv420p -f v4l2. See the examples below in this article.

FFmpeg can also use the v4l2loopback device as an input stream. See FFmpeg#Recording webcam.

GStreamer can also use v4l2loopback devices as sources and sinks.

Further examples can be found at the official v4l2loopback wiki.

ヒント: PipeWire is an upcoming new low-level multimedia framework, which can also do some things v4l2loopback is used for.

スクリーンキャスト

Below are examples how to implement screen sharing by streaming the screen to a v4l2 loopback device, which can be used as a virtual webcam.

FFmpeg を使って X11 をキャストする

ノート: This requires #Loading the kernel module with exclusive_caps=1 and assumes the created video device is /dev/video0.

FFmpeg can be used to select a region on a X11 display and then stream it to a v4l2 loopback device:

$ ffmpeg -f x11grab -select_region 1 -show_region 1 -framerate 25 -i $DISPLAY -vf format=yuv420p -f v4l2 /dev/video0

See the x11grab docs for a description of all the arguments.

wf-recorder を使って Wayland をキャストする

ノート:

Install wf-recorder (or wf-recorder-gitAUR). To start recording the screen with wf-recorder and stream it to a v4l2 loopback device use:

$ wf-recorder --muxer=v4l2 --codec=rawvideo --file=/dev/video0 -x yuv420p

If you encounter Unknown V4L2 pixel format equivalent for rgb0 run wf-recorder with --force-yuv or -t to force conversion of the data to yuv format, before sending it to the GPU. [1]

特定の領域のみをスクリーンキャストする
警告: This method does not involve a proper implementation of individual application sharing as the region being shared will not change after resizing any windows.

As explained above, wf-recorder is able to record only a portion of the screen by first selecting a region with slurp. To use this functionality for sharing a specific region/application window through a virtual video device, start recording the screen with the following modified command:

$ wf-recorder -g "$(slurp)" --muxer=v4l2 --codec=rawvideo --file=/dev/video0 -x yuv420p

gPhoto でフォトカメラをウェブカメラとして使う

ノート: This requires #Loading the kernel module with exclusive_caps=1 and assumes the created video device is /dev/video0.

If gPhoto is installed and working with your camera, you can use gphoto2 and FFmpeg to pipe the liveview stream of the camera to a v4l2 loopback device:

$ gphoto2 --stdout --capture-movie | ffmpeg -i - -vf format=yuv420p -f v4l2 /dev/video0

For troubleshooting refer to gPhoto#Troubleshooting. This should work with all cameras, which have the liveview functionality listed on the official gPhoto website.

ネットワークストリームをウェブカメラとして使う

ノート: This requires #Loading the kernel module with exclusive_caps=1 and assumes the created video device is /dev/video0.

FFmpeg can be used to capture a network stream and pipe it to a v4l2 loopback device:

$ ffmpeg -i http://ip_address:port/video -vf format=yuv420p -f v4l2 /dev/video0

Android デバイスをウェブカメラとして使う

On Android IP Webcam can be used to broadcast a network video stream. Then the command above can be used to pipe it into a v4l2 loopback device and use it like a webcam. IP Webcam defaults to port 8080.

Depending on the network connection, the network stream over WiFi might have a lot of latency and could be stuttering. If you have Android Debug Bridge configured with your computer and the Android device, it is possible to tunnel the stream over a USB cable using adb forward, which provides a more stable connection:

$ adb wait-for-usb-device && adb forward tcp:8080 tcp:8080 && ffmpeg -i http://127.0.0.1:8080/video -vf format=yuv420p -f v4l2 /dev/video0
ヒント: An other option is Iriun with iriunwebcam-binAUR.

トラブルシューティング

Firefox

Firefox がビデオストリームを読み込むことができず、AbortError: Starting video failed のようなメッセージが出力される場合、v4l2compat.so をプリロードしてみてください:

$ LD_PRELOAD=/usr/lib/v4l1compat.so firefox

ioctl(VIDIOC_G_FMT)

ffmpeg でビデオデバイスを使おうとしたときに ioctl(VIDIOC_G_FMT) エラーが発生する場合、カーネルモジュールを一旦アンロードし、再びロードし直してみてください。 [2]

参照