python-vlc find current playing audio - python

I want know to which audio is currently playing in my playlist.
eg: it's path, title or index position
import vlc
instance = vlc.Instance()
player = instance.media_list_player_new()
player.set_playback_mode(vlc.PlaybackMode.loop)
songs = ['music1.mp3', 'music2.mp3', 'music.mp3']
media_list = instance.media_list_new(songs)
player.set_media_list(media_list)
player.play_item_at_index(1)
I tried bunch of methods unfortunately none of them works.

I found the solution for my problem.
Actually the instance is inherited from the get_media_player && get_media using this you can find current playing song base path.
import vlc
instance = vlc.Instance()
player = instance.media_list_player_new()
player.set_playback_mode(vlc.PlaybackMode.loop)
songs = ['music1.mp3', 'music2.mp3', 'music.mp3']
media_list = instance.media_list_new(songs)
player.set_media_list(media_list)
player.play_item_at_index(1)
player.get_media_player().get_media().get_mrl() # will return path of current playing mp3 or mp4

Related

VLC Python Binding only playing the first mp4 file in the folder

I was trying to Play multiple small mp4 files that is in a folder. And i was using this example as my reference:
https://github.com/oaubert/python-vlc/blob/master/generated/3.0/examples/play_buffer.py
But when i ran the py file giving that particular folder it read all of the files but closes after playing the first file. Can Someone Help me with this.
And i know i can just use a for loop to play all the files in a folder but i don't want any stutter between transitioning those files(Or in simple words i want seamless transition between those files). So any help would be appreciated.
i don't want any stutter between transitioning those files
What you are looking for is called "gapless playback". Sadly, this is not currently supported by LibVLC.
Regardless, to play multiple individual files, you need to either feed them one after the other to the mediaplayer (by subscribing to the EndReached event, for example) or use a MediaList object.
You have picked a complicated and arguably inappropriate code example for your scenario.
Try something like this (without ctypes):
import vlc
import glob
import time
base_folder = './'
# vlc State 0: Nowt, 1 Opening, 2 Buffering, 3 Playing, 4 Paused, 5 Stopped, 6 Ended, 7 Error
playing = set([1,2,3,4])
def add_media(inst, media_list, playlist):
for song in playlist:
print('Loading: - {0}'.format(song))
media = inst.media_new(song)
media_list.add_media(media)
playlist = glob.glob(base_folder + "/" + "*.mp3")
playlist = sorted(playlist)
media_player = vlc.MediaListPlayer()
inst = vlc.Instance('--no-xlib --quiet ')
media_list = vlc.MediaList()
add_media(inst, media_list, playlist)
media_player.set_media_list(media_list)
media_player.play()
time.sleep(0.1)
current = ""
idx = 1
player = media_player.get_media_player()
while True:
state = player.get_state()
if state.value == vlc.State.Ended or state == vlc.State.Error:
if idx == len(playlist)+1:
break
title = player.get_media().get_mrl()
if title != current:
print("\nPlaying - {0}\t{1} of {2}".format(str(title), idx, len(playlist)))
current = title
idx += 1
time.sleep(0.1)
print("\nPlaylist Finished")
Here, we use a MediaListPlayer() rather than a media.player_new_from_media() or a media_player_new() because we are playing a set of media, not a single instance.
You can improve on this by controlling it via events using the event_manager() and attaching appropriate events for the player to listen for.

Youtube DL returns none for artist and track

I'm trying to extract info like the artist and track from YouTube videos, but the youtube-dl library is returning none for song and artist.
I've read that YouTube updated recently and youtube-dl just hasn't caught up with the update.
Is there any other way I could do this?
# takes in a video url and extracts all the info about it
youtube_url = f"https://www.youtube.com/watch?v={video_id}"
# we dont want all the logs and unnecessay stuff and we dont want the library to download the video, so...
video = youtube_dl.YoutubeDL({'quiet': True}).extract_info(
youtube_url, download=False
)
# now that we have the info we need, lets assign it to the variables artist, track
artist = video['artist']
track = video['track']
print("song:", track)
print("artist:", artist)
return artist, track
The output is:
song: None
artist: None

How to add additional audio track to video player with libvlc

I'm looking for playing a video with vlc player via python3.8. I'm able to play a movie (mp4) but I would like to add additional audio tracks. I read that the 'add slave' method is the (new) way but I'm not able to use it properly: I'm not able to add subtitles nor audio track.
To summerize: what I want to achieve with Python is roughly the following:
https://wiki.videolan.org/VLC_HowTo/Play_an_external_audio_track_for_a_video
my current (non working) snippet:
import vlc
base_path = r"Z:/test/libvlc/"
video_file = base_path + "original.mp4"
audio_file = base_path + "2xlcDLHY7k0-instru+vocal_stereo.wav"
sub_file = base_path + "word.ass"
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(video_file)
AdditionalTrack = player.add_slave(player, audio_file, True, i_type="audio")
Sub = player.add_slave(player,sub_file, True)
player.set_media(Media)
while True:
player.play()
I found doc for 'add_slave' func here:
https://www.olivieraubert.net/vlc/python-ctypes/doc/
but I'm not able to use it properly
libvlc_media_slaves_add(p_md, i_type, i_priority, psz_uri)
Add a slave to the current media. A slave is an external input source that may contains an additional subtitle track (like a .srt) or an additional audio track (like a .ac3).
Parameters: p_md - media descriptor object. i_type - subtitle or audio. i_priority - from 0 (low priority) to 4 (high priority). psz_uri - Uri of the slave (should contain a valid scheme).
If anyone know how to add subtitles or additional audio track,
I would be grateful to him if he can advise me how to,
thanks a lot !
use this code instead.
AdditionalTrack = player.add_slave(player, audio_file, True, i_type=vlc.MediaSlaveType(1))
i-type parameter is not a string type.

How to incorporate SSML into Python

I need to use SSML to play an audio file with the tag in my Alexa Skill (as per Amazon's instructions).
Problem is, I don't know how to use SSML with Python. I know I can use it with Java but I want to build my skills with Python. I've looked all over, but haven't found any working examples of SSML in a Python script/program - does anyone know?
This was asked two years ago but maybe someone will benefit from the below.
I've just checked and if you use Alexa Skills Kit SDK for Python you can simply add SSML to your response, for example:
#sb.request_handler(can_handle_func=is_request_type("LaunchRequest"))
def launch_request_handler(handler_input):
speech_text = "Wait for it 3 seconds<break time="3s"/> Buuuu!"
return handler_input.response_builder.speak(speech_text).response
Hope this helps.
SSML audio resides in the response.outputSpeech.ssml attribute. Here is an
example obj with other required parameters removed:
{
"response": {
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>
Welcome to Car-Fu.
<audio src="https://carfu.com/audio/carfu-welcome.mp3" />
You can order a ride, or request a fare estimate. Which will it be?
</speak>"
}
}
Further reference:
JSON Interface Reference for Custom Skills
Speech Synthesis Markup Language (SSML) Reference
Install ssml-builder "pip install ssml-builder", and use it:
from ssml_builder.core import Speech
speech = Speech()
speech.add_text('sample text')
ssml = speech.speak()
print(ssml)
These comments really helped a lot in figuring out how to make SSML works using the ask-sdk-python. Instead of
speech_text = "Wait for it 3 seconds<break time="3s"/> Buuuu!" - from wmatt's comment
I defined variables that represents the start and end of every tags that I'm using
ssml_start = '<speak>'
speech_text = ssml_start + whispered_s + "Here are the latest alerts from MMDA" + whispered_e
using single quotes and concatenate those strings to the speech output and it worked! Thanks a lot guys! I appreciate it a lot!
This question was somewhat vague, however I did manage to figure out how to incorporate SSML into a Python script. Here's a snippet that plays some audio:
if 'Item' in intent['slots']:
chosen_item = intent['slots']['Item']['value']
session_attributes = create_attributes(chosen_item)
speech_output = '<speak> Here is something to play' + \
chosen_item + \
'<audio src="https://s3.amazonaws.com/example/example.mp3" /> </speak>'
The ssml package for python exists.
you can install like below by pip
$ pip install pyssml
or
$ pip3 install pyssml
so example is link below
http://blog.naver.com/chandong83/221145083125
sorry. it is korean.
# -*- coding: utf-8 -*-
# for amazon
import re
import os
import sys
import time
from boto3 import client
from botocore.exceptions import BotoCoreError, ClientError
import vlc
from pyssml.PySSML import PySSML
# amazon service fuction
# if isSSML is True, SSML format
# else Text format
def aws_polly(text, isSSML = False):
voiceid = 'Joanna'
try:
polly = client("polly", region_name="ap-northeast-2")
if isSSML:
textType = 'ssml'
else:
textType = 'text'
response = polly.synthesize_speech(
TextType=textType,
Text=text,
OutputFormat="mp3",
VoiceId=voiceid)
# get Audio Stream (mp3 format)
stream = response.get("AudioStream")
# save the audio Stream File
with open('aws_test_tts.mp3', 'wb') as f:
data = stream.read()
f.write(data)
# VLC play audio
# non block
p = vlc.MediaPlayer('./aws_test_tts.mp3')
p.play()
except ( BotoCoreError, ClientError) as err:
print(str(err))
if __name__ == '__main__':
# normal pyssml
#s = PySSML()
# amazon speech ssml
s = AmazonSpeech()
# normal
s.say('i am normal')
# speed is very slow
s.prosody({'rate':"x-slow"}, 'i am very slow')
# volume is very loud
s.prosody({'volume':'x-loud'}, 'my voice is very loud')
# take a one sec
s.pause('1s')
# pitch is very high
s.prosody({'pitch':'x-high'}, 'my tone is very high')
# amazone
s.whisper('i am whispering')
# print to convert to ssml format
print(s.ssml())
# request aws polly and play
aws_polly(s.ssml(), True)
# Wait while playback.
time.sleep(50)

How does vlc.py play video stream?

I want to use vlc.py to play mpeg2 stream http://wiki.videolan.org/Python_bindings.
There are some examples here: http://git.videolan.org/?p=vlc/bindings/python.git;a=tree;f=examples;hb=HEAD
When I run the examples, it just can play video file, I want to know is there any examples to play video stream ?
According to this Pastebin entry, linked to in this mailing list, it can be solved using a method like this:
import vlc
i = vlc.Instance('--verbose 2'.split())
p = i.media_player_new()
p.set_mrl('rtp://#224.1.1.1')
p.play()
I haven't tried it though, so please let me know if it works.
This is a bare bones solution:
import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('http://localhost/postcard/GWPE.avi')
Media.get_mrl()
player.set_media(Media)
player.play()
if the media is a local file you will have to alter:
Media = Instance.media_new('http://localhost/postcard/GWPE.avi')
Media.get_mrl()
to:
Media = Instance.media_new_path('/path/to_your/file/filename.avi')
note that you must lose the get_mrl() as well as changing the function.
import vlc
vlcInstance = vlc.Instance()
player = vlcInstance.media_player_new()
player.set_mrl("rtsp://URL_PATH")
player.play()
I was able to open a stream with the following code, combining the previous answers.
Tested this with a network webcam

Categories

Resources