Skip to content

Instantly share code, notes, and snippets.

@CSaratakij
Last active June 5, 2024 16:26
Show Gist options
  • Save CSaratakij/788261f1ebcf2aefa320255120f75efe to your computer and use it in GitHub Desktop.
Save CSaratakij/788261f1ebcf2aefa320255120f75efe to your computer and use it in GitHub Desktop.
Video Wallpaper (hw-accelerate) : Use 'mpv' as a video player backend with 'xwinwrap' to wrap our window to be use as a desktop background
You want to use a live wallpaper in x11?
Great!!...but It come as a cost.
I did experiment with .gif backend and non hardware accelerate backend, It sucks.
CPU usage was so high (40% up just for idle), unacceptable for my laptop.
Running with those backend for such a really long time will slowly turn your laptop into the hot potato.
But don't be upset, we can reduce those cost.
Using the video player with hardware video decoding support can reduce the cost.
But...with video player alone won't help your poor laptop to be heat.
If you play video with unsupport codec, the cost will be the same as using a .gif which result in high cpu usage.
The more cpu usage (during idle), the more heat it will get in the long run.
My acceptable cpu usage and cpu temperature on idle for my laptop is about 0%-5% with 50°C
(Hotter than that, I will feel the warm on my palm)
Note that cpu usage are approximate by htop when the computer idle, not the process itself
and the number here is my cpu i5-7200u with intel-hd-620.
I use 'mpv' as a video player (with VAAPI) and use 'xwinwrap' to wrap mpv window as a desktop background.
The reason to use xwinwrap is to make it compatible with the picom (compositor).
If you just tell mpv to draw to the root window when compositor is enable, you won't see anything.
(If you don't use compositor, you don't need xwinwrap)
Before doing anything, please make sure you can play your video with hardware video decoding.
It won't work without this feature.
The keys to lower the cost:
1) video player with hardware video decoding support.
2) video (live wallpaper) that encode with the same codec that hardware can decode.
3) avoid video scailing*
4) lower the video framerate**
5) disable compositor***
6) encode video with fastdecode****(mp4 encoding)
7) offloading to dedicated gpu*****(optimus laptop)
I provide "ffmpeg-command.sh" and "i3-livewallpaper.sh" scripts.
"ffmpeg-command.sh" is an example of how to scale a video and lower your video framerate.
Please, do not make this script executable. It wasn't written to be run as a scripts.
Look inside to see more information.
"i3-livewallpaper.sh" is an actual script I use after I finish preparing my video (live wallpaper).
Please change the video name to match yours.
*scailing video (size of video is not the same as the size of the monitor resolution) is not really a cost at all.
But it won't hurt to reduce the computation cost right? :D
**lower the video framerate massively reduce the cpu usage.
Please spend sometime trying to lower it until your video framerate is the lowest acceptable (visually)
***compositor can use slighly cpu usage, I manage to cut about 3% cpu usage with it disable.
****the fastdecode flag will slightly reduce the cpu usage in my mp4 decoding.
*****using prime render offload reduce the render cost of integrate gpu.
please ensure your dedicated card can decode the video codec before trying this, or else it will use cpu usage to decode.
(beware of more heat from dedicated gpu though)
Result:
------------
setup cpu usage
software decoding(30fps) 40-60%
hardware decoding(30fps) 22-25%
hardware decoding(18fps) 6-11%
hardware decoding(18fps, disable compositor) 5-8%
This is the lowest as I can get by this experiment. Unfortunately, I cannot maintain the temperature as I hope for.
With 53°C as the end result. The warm feeling on my palm is too much for just an idle laptop.
So at the end, I didn't use a live wallpaper....But I'm happy that atleast I give this a shot :D
(I use dynamic wallpaper at the end...)
ps.
I use 'intel-media-driver' for intel-hd-620, it use slightly less cpu usage than i965 though.
#
# WARNING!!!, don't make this script executable, Please read the comment below.
#
#----------------------------------------
# Step 1 : Avoid video scaling by resize your video to exact monitor resolution
#----------------------------------------
# To Shrink from full hd to 1366x768 (lossless)
$ ffmpeg -i MyMovie.mkv -vf scale=1366:-1 -c:v libx264 -crf 18 -preset veryslow -c:a copy MyMovie_720p.mkv
----------------------------------------
#----------------------------------------
# Step 2 : Set lowest acceptable fps for your video
#----------------------------------------
#Warning, you can't simply drop the video frame rate (The result of video will stutter).
#Instead, re-encode video with motion interpolation to smooth the frame (filling the in-between).
#----------------------------------------
# To compute a new fps and get closets visual without drop a frame
$ ffmpeg -i input.mp4 -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=18'" -tune fastdecode -profile:v main -preset slower -crf 18 smooth_bitrate_18fps.mp4
# Someone suggest to remove B frame calculation (to reduce computation) and Set a max bitrate (to avoid average bitrate to go further than theshold)
$ ffmpeg -i input.mp4 -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=24'" -tune fastdecode -profile:v main -preset veryslow -crf 18 -maxrate 8M -bf 0 smooth_bitrate_b_frame_disable_24fps.mp4
# This is currently my actual command
#----------------------------------------
# I'm Setting bufsize to make setting maxrate possible (experiment with higher or lower if you encounter the lost quality) (set too low will hinder the video quality)
$ ffmpeg -i input.mp4 -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=18'" -tune fastdecode -profile:v main -preset veryslow -crf 18 -maxrate 8M -bufsize 4M -bf 0 ouput.mp4
#----------------------------------------
#!/bin/sh
# Please change your video name to match yours.
xwinwrap -g 1366x768 -ni -s -nf -b -un -ov -fdt -argb -- mpv --mute=yes --no-audio --no-osc --no-osd-bar --quiet --screen=0 --geometry=1366x768+0+0 -wid WID --loop smooth_b_frame_disable_18_fps.mp4
@Tej12345
Copy link

Good job my friend, you saved a lot of time for us!
Thank you very much:)

@KebabLord
Copy link

excellent job mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment