I used Python + FFmpeg + DLNA to complete a screen projection solution under Linux . The disadvantage of this scheme is that the delay is a bit large. |
Generally speaking, Android devices and Windows devices use the miracast protocol for screen projection, but this protocol requires the network card to support p2pwifi, and most network card drivers under Linux do not support p2pwifi.
So I used Python + FFmpeg + DLNA to complete a screen projection solution under Linux. The disadvantage of this scheme is that the delay is a bit large.
set up
Here's how to do it.
First install this DLNA library:
pip3 install dlna
Then use pactl to find “Monitor Source” (Chinese output) or “Monitor Source” (English output):
pactl list sinks
Sample output:
Sink #0 State: RUNNING Name: alsa_output.pci-0000_05_00.6.HiFi__hw_Generic_1__sink Description: Family 17h (Models 10h-1fh) HD Audio Controller Speaker + Headphones Driver: module-alsa-card.c Sample Specification: s16le 2ch 44100Hz Channel Map: front-left,front-right Owner Module: 9 Mute: no Volume: front-left: 53814 / 82% / -5.14 dB, front-right: 53814 / 82% / -5.14 dB balance 0.00 Base Volume: 65536 / 100% / 0.00 dB Monitor Source: alsa_output.pci-0000_05_00.6.HiFi__hw_Generic_1__sink.monitor Latency: 16676 usec, configured 16000 us...
Then create a CGI script screen.flv. first. Create the directory where the script will be placed:
mkdir screencast mkdir screencast/cgi-bin
Then create the script directly by cat:
cat <screencast/cgi-bin/screen.flv #!/bin/bash echo "Content-Type:video/x-flv" echo ffmpeg -f pulse -i <监视器信源> -f x11grab -i :0 -vcodec h264_nvenc pipe:.flv eof
Please replace <monitor source> in the file with the monitor source obtained above.
and set executable permissions for it:
chmod +x screencast/cgi-bin/screen.flv
Note: If you don't have an Nvidia graphics card, or want to use other hardware acceleration, please replace the encoding schemeh264_nvenc with the corresponding encoding scheme. Soft solution is not recommended because the delay is very high.
screencast
When you need to cast the screen, first start the local web server:
cd screencast python3 -m http.server --cgi 9999&
Then, find your DLNA device and copy the URL after location:
dlna device
Sample output:
=> Device 1: { "location": "http://192.168.3.118:1528/", "host": "192.168.3.118", "friendly_name": "Kodi", ...
Find the LAN IP address of your Linux computer:
ip addr
Sample output:
3: wlp2s0: mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 74:4c:a1:82:2e:3f brd ff:ff:ff:ff:ff:ff inet 192.168.3.117/24 brd 192.168.3.255 scope global dynamic noprefixroute wlp2s0 valid_lft 58283sec preferred_lft 58283sec inet6 240e:3b3:2ee3:9530:d005:e492:6243:9/128 scope global dynamic noprefixroute valid_lft 6738sec preferred_lft 3138sec inet6 240e:3b3:2ee3:9539:f289:6043:c56a:4e7b/64 scope global dynamic noprefixroute valid_lft 7189sec preferred_lft 3589sec inet6 240e:3b3:2ee3:9539:3714:eaf0:c549:b8c9/64 scope global dynamic mngtmpaddr noprefixroute valid_lft 7188sec preferred_lft 3588sec inet6 fe80::c746:2540:ab7b:20aa/64 scope link valid_lft forever preferred_lft forever inet6 fe80::3543:2637:e0fc:3630/64 scope link noprefixroute valid_lft forever preferred_lft forever
The command to start screencasting is as follows:
dlna play -d http://<局域网 IP>:9999/cgi-bin/screen.flv
Please replace the and <LAN IP> parameters accordingly. The command I replaced here is:
dlna play -d http://192.168.3.118:1528/ http://192.168.3.117:9999/cgi-bin/screen.flv
Then set up your TV to accept screencasting. Different TV devices have different screencasting methods. Please refer to the specific device instructions.
After a few moments, the video will appear on the TV. The screen projection effect is as follows:
About the Author:
calvinlin: An ordinary Shenzhen junior high school student.