How to play youtube videos in python-vlc? - python-3.x

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()

Related

Python stream audio from YouTube livestream

I'm interested in creating a python 3.9 program that streams audio from a YouTube livestream but unfortunately I can't get the video.getbestaudio() function to work and the function only returns the value null. Thus causing the null error on the next line.
Just in case you need to know I'm using the Pafy library to get the audio stream and the python-vlc library to play the audio. The script is also fully functional if I use a YouTube video that is not a livestream or use the video.getbest() function but this also creates a window displaying the video stream which is not what I want.
I was wondering how I could work around the error and create a functioning python script. I am open to use other methods if they properly work. Thanks for any help in advance!
Here is the error:
Traceback (most recent call last):
File "C:\Users\mjten\Desktop\Programing\Python\lofi.py", line 8, in <module>
playurl = best.url
AttributeError: 'NoneType' object has no attribute 'url'
Here is my code:
import pafy
import vlc
url = 'https://www.youtube.com/watch?v=5qap5aO4i9A'
video = pafy.new(url)
best = video.getbestaudio()
playurl = best.url
player = vlc.MediaPlayer(playurl)
player.play()
while True: pass
P.S Sorry for the bad code I'm just trying to figure out a working example in the meantime.
pip install pafy
pip install youtube_dl
import pafy
def stream(url):
video = pafy.new(url, ydl_opts={'nocheckcertificate': True})
print(video)
best = video.getbestaudio()
stream_urls = best.url
print(stream_urls)
stream('https://www.youtube.com/watch?v=5qap5aO4i9A')

Audio gets stuck on stream.write in Pyaudio

I am trying to play audio through the play function of pydub play(audio_segment) but the function was getting constantly stuck on this function without actually playing the audio.
It seems like pydub is getting stuck on the def _play_with_pyaudio(seg): in the stream.open function of pyaudio on the line stream = Stream(self, *args, **kwargs).
If I pause between the creation of pyaudio and the opening of the audio stream, it plays through the audio stream just fine.
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(seg.sample_width),
channels=seg.channels,
rate=seg.frame_rate,
output=True)
I am not getting an error, neither in code or in ALSA debug messages. How do I resolve this, I made sure to reinstall pyaudio, is it a problem in the library or in pyaudio? It seems to me that the problem is somewhere in pyaudio.
I am currently on pyaudio-0.2.11 and pydub 0.23.1. I am working in my personal Lubuntu virtual environment.

How is the best way to play an MP3 file with a start time and end time in Python 3

I am working on a project that requires sound snippets to be played from MP3 files in a playlist. The files are full songs.
I have tried pygame mixer and I can pass the start time of the file, but I cannot pass the end time that I want the music to stop, or be able to fade-in and fade out the current snippet.
I have looked at the vlc and ffmpeg libraries, but I do not see the functionality I am looking for.
I'm hoping someone may be aware of a library out there that may be able to do what I am trying to accomplish.
I finally figured out how to do exactly what I wanted to do!
In the spirit of helping others I am posting an answer to my own question.
My development environment:
Mac OS Mojave 10.14.6
Python 3.7.4
PyAudio 0.2.11
PyDub 0.23.1
Here it is in it's most rudimentary form:
import pyaudio
from pydub import AudioSegment
# Assign a mp3 source file to the PyDub Audiosegment
mp3 = AudioSegment.from_mp3("path_to_your_mp3_file")
# Specify starting and ending offsets from the beginning of the stream
# then apply a fadein and fadeout. All values are in millisecond (seconds * 1000).
mp3 = mp3[int(43000):int(58000)].fade_in(2000).fade_out(2000)
# In the above example the music will start 43 seconds into the track with a 2 second
# fade-in, and only play for 15 seconds with a 2 second fade-out. If you don't need
# these features, just comment out the line and the full mp3 will play.
# Assign the PyAudio player
player = pyaudio.PyAudio()
# Create the stream from the chosen mp3 file
stream = player.open(format = player.get_format_from_width(mp3.sample_width),
channels = mp3.channels,
rate = mp3.frame_rate,
output = True)
data = mp3.raw_data
while data:
stream.write(data)
data=0
stream.close()
player.terminate()
It isn't in the example above, but there is a way to process the stream and increase/decrease/mute the volume of the music.
One other thing that could be done is to set up a thread to pause the processing (writing) of the stream, which would emulate a pause button in a player.

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).

Vlc python library

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.

Resources