Posts: 6
Joined: Dec 2020
Reputation:
2
I've put myself together a little Python script for my Windows PC which launches ffmpeg to create a live audio/video stream off my desktop, controls my Kodi device through its JSON RPC interface remotely and mutes my PC's local audio output while streaming.
After fiddeling around with ffmpeg for a while I now have a HEVC (H.265) 1920x1080@60fps video signal with a 32 Bit AAC audio signal which is broadcast to my LAN using RTP for transport, and when looking at the screen connected to my Kodi device (Raspberry Pi 4, but any Kodi device capable of decoding H.265/AAC should do) I can't really tell the difference to my PC monitor (I'm using NIVDIA's GPU-accelerated video encoder "hevc_nvenc", but it also works with ffmpeg's software encoder). There is a constant delay of about 4 seconds between PC an Kodi, but that doesn't matter once I focus my attention to Kodi (as long as the PC's speakers are muted).
I could generalize the Python script a little and upload it to github, I'm just wondering: is anybody besides me interested in this? Are there any free and open solutions to this (I mean without involving special hardare or any software licences)?
Posts: 4,001
Joined: Jan 2023
Reputation:
407
i would definitely be interested in seeing this and adapting to linux if possible
love the creativity, kudos
Posts: 6
Joined: Dec 2020
Reputation:
2
Great! I'm not sure though if I will be able to port the speaker-mute feature to linux, it's really tied to Windows (in the worst case you will have to mute your speakers manually, if that applies to your setup).
But all the rest should work if I can find a fast video signal grabber and any audio signal grabber for the linux desktops (X11 and Wayland, which are you using?).
Posts: 4,001
Joined: Jan 2023
Reputation:
407
2023-01-24, 20:33
(This post was last modified: 2023-01-24, 20:38 by izprtxqkft. Edited 1 time in total.)
no worries im a coder, whatever youve got there should be workable, python is already cross platform as is ffmpeg, i have intel so itll be a different hardware accelerator but nothing too strenuous to work out
EDIT:
ill probably port over to use kmsgrab
Posts: 6
Joined: Dec 2020
Reputation:
2
Perfect. If you want to play around, here's the gist of it:
First, launch ffmpeg, here's an example windows command line:
ffmpeg -hide_banner -loglevel warning \
-f dshow -thread_queue_size 64 -i audio=virtual-audio-capturer \
-f gdigrab -thread_queue_size 128 -probesize 32M -framerate 60 -i desktop \
-c:a aac \
-c:v libx265 -pix_fmt yuv420p10 -x265-params log-level=warning \
-f rtp_mpegts rtp://10.255.255.255:1234/
You will have to replace the 2nd and 3rd lines, that is the DirectShow audio grabber "virtual-audio-capturer" and the GDI video grabber "gdigrab". Currently, I don't know anything about linux audio grabbers in ffmpeg, but you already found kmsgrab for the video grabber, maybe you can find an audio grabberm, too. Since I don't know enough about Intel GPUs in ffmpeg, in the 5th line I chose the X.265 software encoder "libx265" with 10 bit colours (works better on the Raspberry Pi 4 than 8 bit, try "-pix_fmt yuv420p" for 8 bit colour). Try to pick a pix_fmt that has hardware decoding support on your Kodi device. The broadcast address for my LAN is 10.255.255.255, replace that with yours if it differs.
Second, create yourself a file called "Desktop.strm" which you can access through Kodi, this file should contain a single line (again, replace with your LAN's broadcast address):
rtp://10.255.255.255:1234/
Finally, open the .strm file through Kodi about 5 seconds (or more) after launching ffmpeg, and you should see your desktop.
I would appreciate your feedback regarding your ffmpeg command line very much.
Posts: 4,001
Joined: Jan 2023
Reputation:
407
nice, ive been playing with alterations it works quite well but kodi is being a little b about the stream so i tested from mpv, pretty quick and of course the buffered delay is to be expected
ill be sure to follow up with a proper matching command line to yours once i work it all in
Posts: 4,001
Joined: Jan 2023
Reputation:
407
right on, i hope to be able to dedicate some time to dialing this in for my system this weekend, too much going on at the moment
Posts: 6
Joined: Dec 2020
Reputation:
2
Fantastic, that is really helpful. Here I refactored your first command line into the desktop2kodi.ini format:
[desktop2kodi]
audio_input = alsa
video_input = x11grab
audio_encoder = ac3
video_encoder = libx264_30M
kodi_addr =
[alsa]
type = audio_input
description = ALSA audio grabber
platform = linux
unmutable = false
args = -f alsa -ac 2 -i hw:Loopback,1,0
[x11grab]
type = video_input
description = X11 frame grabber
platform = linux
args = -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0+0,0
[ac3]
type = audio_encoder
description = Audio Codec 3 (AC3) audio encoder
args = -c:a aac
[libx264_30M]
type = video_encoder
description = CPU-based H.264 video encoder (yuv420p, 30M buffer)
args = -c:v libx265 -x265-params log-level=warning -pix_fmt yuv420p
All I did was to separate each of the four ffmpeg "elements" (2 grabber and 2 encoder) into 4 separate INI file sections. It's maybe a bit redundant but absolutely flexible, the idea here is that these sections are reusable for others once they are well adjusted (such sections should be merged into my repository eventually).
All you need to edit is the address of your kodi device in the first section in field kodi_addr, and it should stream straight to your kodi device. This field has one of these forms:
HOSTNAME
HOSTNAME:PORT
USERNAME:PASSWORD@HOSTNAME
USERNAME:PASSWORD@HOSTNAME:PORT
HOSTNAME can be also be just the IP address of your device. Default for PORT is Kodi's default of 8080.