Stream Windows Desktop to Kodi
#1
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)?
Reply
#2
i would definitely be interested in seeing this and adapting to linux if possible
love the creativity, kudos
Reply
#3
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?).
Reply
#4
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
Reply
#5
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.
Reply
#6
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
Reply
#7
I've uploaded it to github, see: https://github.com/chschnell/desktop2kodi.

Currently the README mostly describes the ffmpeg side of things as it has by far the greatest complexity here and thus needs some explaining. You'll find a link there about Intel GPU-accelerated video encoding, maybe you can manage to get this running on your system with the information provided there.

The Python script still lacks flexibility to configure the ffmpeg grabbers and encoders, you'll have to modify the code for ffmpeg customization, I will need a day or two to sort out a concept and implement it.

It's still all in a very, very early state.
Reply
#8
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
Reply
#9
The script is now well configurable, all the ffmpeg-related configuration now resides in an INI file called ffmpeg.ini. All you need to to is create your own local desktop2kodi.ini file (from a template in the repository), and that is the only file you will need to edit.

See https://github.com/chschnell/desktop2kodi#installation for installation instructions.

I hope you can get it to word.
Reply
#10
right on
i was playing around with ffmpeg specifically just capture methods on my box, heres my rough notes for commands, it doesnt stream to kodi because i was just testing capture to a file so i knew it worked ahead of time in case kodi didnt like it

x11 grab with a hardware loopback audio device, basically adds a dummy audio card to linux i can then choose to send specific audio to:

Code:
ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0+0,0 -f alsa -ac 2 -i hw:Loopback,1,0 -c:a ac3 -b:a 640k -t 00:00:30 -c:v libx264 -b:v 30M -y output.mkv

another variation:

Code:
ffmpeg -f x11grab -thread_queue_size 4096 -r 24 -s 1920x1080 -i :0.0+0,0 -f alsa -thread_queue_size 4096 -ac 2 -i hw:Loopback,1,0 -vcodec libx264 -preset ultrafast -pix_fmt yuv420p -threads 4  -acodec ac3 -b:a 640k -b:v 12M -t 00:00:30 -y output.mkv

with kmsgrab same audio loopback:

Code:
ffmpeg -r 24 -f alsa -thread_queue_size 4096 -ac 2 -i hw:Loopback,1,0 -framerate 24 -device /dev/dri/card0 -r 24 -f kmsgrab -i - -vf 'hwmap=derive_device=vaapi,hwdownload,format=bgr0' -s 1920x1080 -c:v libx264 -preset ultrafast -b:v 12M -c:a ac3 -b:a 640k -t 00:05:00 -y output.mkv


to load the loopback and give ffmpeg permission to use it:

Code:
sudo modprobe snd-aloop pcm_substreams=1
sudo setcap cap_sys_admin+ep /usr/bin/ffmpeg
Reply
#11
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.
Reply

Logout Mark Read Team Forum Stats Members Help
Stream Windows Desktop to Kodi0