Vlc python library - python-3.x

i am trying to make a media player that generates random subtitles based on nltk library. I am using the vlc python for it, for now i do not care about interface. But i have problem understanding how to put subtitles, even as a srt file.
The function is SubtitleTrack() inside vlc.py. Somewhere else i saw that i must use the add_slave() function.
My code until now is:
import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('Test.avi')
Sub = player.add_slave(player,'Test.srt', True)
player.set_media(Media)
player.play()
The version of libvlc is 2.2.6

SubtitleTrack() is a class.
What you want to do is add the subtitles after you hit play. How you do this, from my understanding depends on the version.
import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('Test.avi')
player.set_media(Media)
player.play()
player.video_set_subtitle_file('Test.srt')
I don't have the add_slave() function on my local version of vlc, but I would assume it's safe to say functionality stayed the same for compatibility reasons.

If you are using vlc 2.2, you should use
player.video_set_subtitle_file('Test.srt')
but this method has been deprecated in the 3.0 release and you should use add_slave.
You can have a look at https://en.wikipedia.org/wiki/URL#Syntax it will help you with the URL concept, and https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/ for the application to Windows paths.

Related

QT paly audio by QAudioDevidce can't connect to PulseAudioService

Like title, i'm trying to use Qt and FFmpeg to play audio. My code like this:
QAudioOutput *audio_output;
QIODevice *stream_out;
QAudioFormat audio_fmt;
audio_fmt.setSampleRate(44100);
audio_fmt.setChannelCount(2);
audio_fmt.setSampleSize(16);
audio_fmt.setCodec("audio/pcm");
audio_fmt.setByteOrder(QAudioFormat::LittleEndian);
audio_fmt.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice();
if(!info.isFormatSupported(audio_fmt))
{
audio_fmt = info.nearestFormat(audio_fmt);
}
audio_output = new QAudioOutput(audio_fmt);
When i use QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice()
i get PulseAudioService: pa_context_connect() failed error.
So how can i fix it?
By the way, i'm using Ubuntu 16.04 and Qt 5.14.2, and i have add 'mutilmedia' to Qt pro file
I checked my Qt file ,and i have audio dir in plugins, it's not lib problem. Also, i read this post ,but i don't know how to fix it, anybody have idea? Thank you guys,and my English is bad, wish you can understand what do i say.

Convert PIL.Image to/from Gtk.Picture/Gtk.Image in gtk4

I do RGBA image manipulation in PIL(low) and would like to display the resulting image in a gtk4 application. For that, I'd need a seamless PIL.Image <--> Gtk.Picture/Gtk.Image converter. I'd like to avoid using temporary files at all cost. I found helpful hints here and here but was wondering if there was anything more convenient in gtk4?
As of GTK 4.4, this seems to work best:
buffer = GLib.Bytes.new(pil_image.tobytes())
gdata = GdkPixbuf.Pixbuf.new_from_bytes(buffer, GdkPixbuf.Colorspace.RGB, True, 8, pil_image.width, pil_image.height, len(pil_image.getbands())*pil_image.width)
gtk_image = Gtk.Image.new_from_pixbuf(gdata)
Once GTK 4.6+ becomes ubiquitous, GdkPixbuf should be replaced with GdkTexture, via Gdk.Texture.new_from_bytes().

How to play youtube videos in python-vlc?

import vlc
p = vlc.MediaPlayer("https://www.youtube.com/watch?v=7ailmFB38Rk")
p.play()
gives me this error
[00007f97a80030c0] http stream error: local stream 1 error: Cancellation (0x8)
I was told this is causes if the link is invalid or broken, both of these are not the cases because using regular vlc to play the video works perfectly
Also, if it is somehow not possible to play the video, I only need to play the audio so that will also be of help.
use pafy
# importing vlc module
import vlc
# importing pafy module
import pafy
# url of the video
url = "https://www.youtube.com/watch?v = vG2PNdI8axo"
# creating pafy object of the video
video = pafy.new(url)
# getting best stream
best = video.getbest()
# creating vlc media player object
media = vlc.MediaPlayer(best.url)
# start playing video
media.play()

How to get artist and title information using python-vlc?

I'm trying to record and get song information (title and artist) from web radios using python-vlc lib.
The recording functionality is working well but the media parse to get the song information doesn't work!
This is my code:
inst = vlc.Instance() # Create a VLC instance
p = inst.media_player_new() # Create a player instance
cmd1 = "sout=file/ts:%s" % outfile
media = inst.media_new("http://playerservices.streamtheworld.com/api/livestream-redirect/JBFMAAC1.aac", cmd1)
media.get_mrl()
p.set_media(media)
p.play()
media.parse()
for i in range(13):
print("{} - {}".format(i, media.get_meta(i)))
It's always returning "MediaParsedStatus.skipped" status. And all song information returns "None". I tested the same radio in VLC App and there it works fine.
Anyone can help me?
thanks in advance
Since this is a stream, libvlc will not parse it by default (only local files).
You need to use a flag to tell libvlc to parse the media even if it is a network stream.
Use libvlc_media_parse_with_options with MediaParseFlag set to network (1).

Play wav file python 3

I want to play a .wav file in Python 3.4. Additonally, I want python to play the file rather than python open the file to play in VLC, media player etc..
As a follow up question, is there any way for me to combine the .wav file and the .py file into a standalone exe.
Ignore the second part of the question if it is stupid, I don't really know anything about compiling python.
Also, I know there have been other questions about .wav files, but I have not found one that works in python 3.4 in the way I described.
Using pyaudio you may get incorrect playback due to speed, consider instead:
sudo apt-get install python-pygame
Windows:
choco install python-pygame?
def playSound(filename):
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
import pygame
pygame.init()
playSound('hellyeah.wav')
I fixed the problem by using the module pyaudio, and the module wave to read the file.
I will type example code to play a simple wave file.
import wave, sys, pyaudio
wf = wave.open('Sound1.wav')
p = pyaudio.PyAudio()
chunk = 1024
stream = p.open(format =
p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
data = wf.readframes(chunk)
while data != '':
stream.write(data)
data = wf.readframes(chunk)
If you happen to be using linux a simple solution is to call aplay.
import os
wav_file = "./Hello.wav"
os.system(f'aplay {wav_file}')

Resources