jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
543 stars 68 forks source link

Loading external files/lavfi-complex playback #167

Closed willwrong closed 3 years ago

willwrong commented 3 years ago

Hi all,

I'm attempting to load a video with an alpha'd overlay using --external-files and then --lavfi-complex, like the following MPV command:

mpv --fs --external-file="overlay.mov" "video.mov" --lavfi-complex="[vid1][vid2]overlay[vo]"

I found an earlier Issue (https://github.com/jaseg/python-mpv/issues/34) that somewhat addresses this but I can't seem to find any more information on the exact syntax to implement this with the API. Any help is most appreciated!

neinseg commented 3 years ago

Hi there,

you're right, this is currently a bit complicated. Here's how you do it:

import mpv

player= mpv.MPV()
player.loadfile('tests/test.webm', external_files='big-buck-bunny_trailer.webm',
    lavfi_complex='"[vid2]scale=1280:720[scaled];[vid1][scaled]blend=shortest=1:all_mode=overlay:all_opacity=0.7[vo]"')
player.wait_for_playback()

Things to note:

willwrong commented 3 years ago

Thanks for the advice! Will test and implement.

willwrong commented 3 years ago

That's working well! Thank you. Another quick one: is there any way you know of to control the [vid1] stream independently of the overlay? I'm searching for a way to have an overlaid video play uninterrupted while the video underneath changes, or even vice-versa. I'm currently using the loadfile command to cue the video switch, but obviously that causes a hard cut in the overlay. Any ideas are welcome, and thanks again.

neinseg commented 3 years ago

I have had a look around and it seems that mpv is re-loading all external_files every time it loads a new file through either loadfile or from a playlist. The only idea I have on how you could get this working with lavfi-complex would be to use https://mpv.io/manual/master/#options-track-auto-selection and try to just never actually quit playing, and just switch out the inputs to the lavfi-complex.

Depending on what you want to achieve there may be several simpler options though: First, you can directly feed mpv image overlays from python, see here. Second, python-mpv has OpenGL support, see here. Using that you could just have two separate mpv.MPV() objects play the two videos into OpenGL buffers that you can then overlay using OpenGL. You can also sync up these two separate instances from python by seeking.

willwrong commented 3 years ago

Thanks again for the advice -- I'll experiment with these!