Page 1 of 1
Streaming - Works from CMD but not from Python
Posted: 13 Apr 2022 18:24
by MisfitGeek
Hello All,
Hoping someone can help me solve this puzzle.
I'm trying to stream from a Python script on Windows.
It works using this command line:
"C:\Program Files\VideoLAN\VLC\vlc.exe" sample-mp4-file.mp4 --sout="#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:duplicate{dst=http{mux=ffmpeg{mux=flv},dst=:8080/}", :no-sout-all :sout-keep
However, this code using the same sout doesn't seem to work.
import vlc
inst = vlc.Instance("""--sout="#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:duplicate{dst=http{mux=ffmpeg{mux=flv},dst=:8080/}", :no-sout-all :sout-keep""")
med = inst.media_new("sample-mp4-file.mp4")
p = med.player_new_from_media()
p.play()
while True:
pass
Does anyone have any suggestions
TIA
Re: Streaming - Works from CMD but not from Python
Posted: 14 Apr 2022 15:46
by MisfitGeek
This code works - don't ask me why
import vlc
inst = vlc.Instance()
param=[
"sample-mp4-file.mp4"
,"sout=#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:duplicate{dst=http{mux=ffmpeg{mux=flv},dst=:8080/}"
]
Media = inst.media_new(*param)
player = Media.player_new_from_media()
player.play()
while True:
pass
Re: Streaming - Works from CMD but not from Python
Posted: 29 Apr 2022 09:13
by unidan
Hi, in the first code, because of the """ string marker, you gave only one big option which was
Code: Select all
--sout="#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:duplicate{dst=http{mux=ffmpeg{mux=flv},dst=:8080/}", :no-sout-all :sout-keep
Python libvlc will split it at spaces, but then the first argument will become
Code: Select all
--sout="#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:duplicate{dst=http{mux=ffmpeg{mux=flv},dst=:8080/}",
Which might have raised an error at the sout creation.
Using media_new is correct here and you can keep that!
Re: Streaming - Works from CMD but not from Python
Posted: 05 May 2022 00:02
by peppy.player
Hi,
I've prepared the following code using the example from this thread:
Code: Select all
import vlc
import time
inst = vlc.Instance()
url = "https://rockthecradle.stream.publicradio.org/rockthecradle-tunein.mp3"
param=[url, "--sout=#transcode{acodec=flac}:std{access=http,mux=flac,dst=10.0.0.6:8080}"]
m = inst.media_new(*param)
player = m.player_new_from_media()
player.play()
while True:
time.sleep(0.2)
When I run it I can hear the stream played locally but I cannot connect any client to that stream (e.g. Firefox browser):
http://10.0.0.6:8080
If I do the same from command line:
Code: Select all
vlc --sout=#transcode{acodec=flac}:std{access=http,mux=flac,dst=10.0.0.6:8080} "https://rockthecradle.stream.publicradio.org/rockthecradle-tunein.mp3"
I can connect clients (Firefox) to that stream.
I'm just wondering how my example is different from the suggested solution?
Thanks!
Re: Streaming - Works from CMD but not from Python
Posted: 05 May 2022 09:40
by Rémi Denis-Courmont
The LibVLC media player API does not support the streaming output (except through the renderer selection).
Re: Streaming - Works from CMD but not from Python
Posted: 05 May 2022 17:07
by peppy.player
OK, how does the player stream without selecting renderer when I start it from command line?
Code: Select all
vlc --sout=#transcode{acodec=flac}:std{access=http,mux=flac,dst=10.0.0.6:8080} "https://rockthecradle.stream.publicradio.org/rockthecradle-tunein.mp3"
Does it use some default renderer?
Thanks!
Re: Streaming - Works from CMD but not from Python
Posted: 05 May 2022 17:22
by Rémi Denis-Courmont
VLC does not use the LibVLC media player API.
Re: Streaming - Works from CMD but not from Python
Posted: 05 May 2022 18:02
by peppy.player
Hmm, the second post in this thread is saying that it works. Though my example which follows that code doesn't actually work. Then it looks like the Python binding for libVLC doesn't support streaming output. Though I was able to use it about 4-5 years ago:
https://github.com/oaubert/python-vlc/issues/105
So more likely something was changed in the code and the only way to make streaming working now is to start 'vlc' as external process. That makes it more difficult to control the player in this case and defeats the purpose of using Python binding at all.
Thank you anyway.
Re: Streaming - Works from CMD but not from Python
Posted: 05 May 2022 19:35
by Rémi Denis-Courmont
The first post uses instance options while the second post uses media/item options.
Either way, there is explicitly no compatibility promise when using options, precisely because they are not compatible across versions.
Re: Streaming - Works from CMD but not from Python
Posted: 06 May 2022 15:16
by unidan
Hmm, the second post in this thread is saying that it works
Actually Rémi's right, --sout won't work on any recent libvlc version (sort of since renderer API in libvlc).
A media option will work though, by passing the option to `media_new`, because it's no different than what VLC does, but as always it's not "public supported API" and might break some time. But you can still pass :sout-keep (or :no-sout-keep to destroy the sout) and still pass sout option in a more flexible way than when passing the option to the instance.
Re: Streaming - Works from CMD but not from Python
Posted: 06 May 2022 21:41
by peppy.player
Actually Rémi's right, --sout won't work on any recent libvlc version (sort of since renderer API in libvlc).
A media option will work though, by passing the option to `media_new`, because it's no different than what VLC does, but as always it's not "public supported API" and might break some time. But you can still pass :sout-keep (or :no-sout-keep to destroy the sout) and still pass sout option in a more flexible way than when passing the option to the instance.
OK, I understand that --sout options will not work in Instance class but should work in Media class. I'm just wondering why my example which is using Media doesn't work? Is it just because now is exactly that time which you mentioned - "and might break some time"?
Thanks!
Re: Streaming - Works from CMD but not from Python
Posted: 06 May 2022 22:07
by peppy.player
Never mind, I found the problem - it should be 'sout' not '--sout'. So, this code works:
Code: Select all
import vlc
import time
inst = vlc.Instance()
url = "https://rockthecradle.stream.publicradio.org/rockthecradle-tunein.mp3"
param=[url, "sout=#transcode{acodec=flac}:std{access=http,mux=flac,dst=:8080}"]
m = inst.media_new(*param)
player = m.player_new_from_media()
player.play()
while True:
time.sleep(0.2)
I can connect to the stream from browser.
Thanks!
Re: Streaming - Works from CMD but not from Python
Posted: 16 May 2022 09:09
by unidan
What makes it working is that you're using `media_new` to pass the option instead of `vlc.Instance`