I'd like to the ability to run some code when the timestamp of a file has been reached (i.e. trigger an alert)
How can this be achieved?
I was looking at this https://www.geeksforgeeks.org/python-vl ... edia-time/
However, I feel like I'm really looking for some kind of daemon (i.e. start the media file with VLC and then have this "listener" perform an action when timestamp on vlc has been reached)
I'm not opposed to pulling down the code, modifying changes to add a hook, and re-compiling to get what I need. I just need some pointers on where in the codebase this would all live.
What direction may I be pointed to to achieve this? Thanks!
You want the EventManager class, the documentation for which can be found
here. Here's an example of how to use it to notify you when a video stops playing.
import vlc
def callback(event, player):
print('FPS:', player.get_fps())
print('Time(ms):', player.get_time())
print('Frame:', .001 * player.get_time() * player.get_fps())
# Create new media
inst = vlc.Instance()
media = inst.media_new('path/to/video')
# Create new instance of vlc player
player = inst.media_player_new()
# Add a callback
em = player.event_manager()
em.event_attach(vlc.EventType.MediaPlayerStopped, \
callback, player)
# Load video into vlc player instance
player.set_media(media)
# Play media
player.play()
Edit: Could you try this code?
import vlc
def callback(player):
print('FPS:', player.get_fps())
print('Time(ms):', player.get_time())
print('Frame:', .001 * player.get_time() * player.get_fps())
media_player.stop()
# creating vlc media player object
media_player = vlc.MediaPlayer()
# media object
media = vlc.Media("src/backtalk/default/oscar.mp4")
# setting media to the media player
media_player.set_media(media)
# start playing video
media_player.play()
while True:
if media_player.get_state() == vlc.State.Ended:
callback(media_player)
Related
I have a script that plays live streaming from RTSP protocol in python using VLC library.
I want a button to start recording the stream on button click. From the documentations I get that only way to save stream is using
--sout file/muxer:stream.xyz
The document says I need to use sout while initiating the vlc instance like this.
i = vlc.Instance(url_to_video_feed, "--sout=file/ps:output.mpg")
This would mean the entire streaming video feed will be saved in the output.mpg file. I only want certain sections of the video feed to save.
Below is the snippet of how the script to stream the video
self.Instance = vlc.Instance()
self.player = self.Instance .media_player_new()
media = self.Instance .media_new(url_to_video_feed,':network-caching=300', )
self.player.set_media(media)
self.player.play() # hit the player button
I'm trying to make a simple script that will play a video (.mp4) in python. I don't want to play a you tube video or anything online, just a video on my computer.
Here's my code:
import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('test.mp4')
Media.get_mrl()
player.set_media(Media)
player.play()
I keep getting this error when I run it (talking about line 3):
AttributeError: 'NoneType' object has no attribute 'media_player_new'
I'm using python 3.5.4 in the IDLE on macOS Sierra.
I solved the problem by comment out find_lib() in vlc.py.
p = os.getcwd()
os.chdir(os.path.join(p, 'sdk'))
dll = ctypes.CDLL("libvlc.dll")
plugin_path = os.path.join(os.getcwd(), r'sdk\plugins')
You are not holding the instance open
import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('2005.mp3')
Media.get_mrl()
player.set_media(Media)
player.play()
while player.get_state() !=6:
continue
Should work or
try this:
import vlc
import time
import sys
def progressbar(progress,guage_length=50):
div = 100 / float(guage_length)
prog = int(progress / div) # ensure progress fits in guage
text = "\rPlaying: {0}{1}{2} [{3}%]".format(">"*prog,"|","-"*(guage_length - prog),format(progress,'.2f'))
sys.stdout.write(text)
sys.stdout.flush()
instance = vlc.Instance()
player = instance.media_player_new()
player.set_mrl("V2.mp4")
player.play()
playing = set([1,2,3,4])
play=True
guage_length=30
while play == True:
time.sleep(0.5)
play_state = player.get_state()
if play_state in playing:
length = player.get_length()
ptime = player.get_time()
progress = ptime/float(length)*100
progressbar(progress,guage_length)
continue
else:
progressbar(100,guage_length)
play = False
print ("\nPlay terminated")
Note that # State 0: None,1 Opening,2 Buffering,3 Playing,4 Paused,5 Stopped,6 Ended,7 Error
Caveat: tested on Linux only
You could simplify your code using simply
import vlc
p = vlc.MediaPlayer('2005.mp3')
p.play()
but this would not solve your problem. Apparently, the vlc instance is not correctly created. This can be caused by a variety of issues. Use
i = vlc.Instance('--verbose 3')
to see possible error messages.
I got this error message on a Raspberry Pi Zero with Raspbian Buster Lite. I searched a long time for the answer but suddenly I noticed the remark on the pypi project page of python-vlc:
Note that it relies on an already present install of VLC.
So installing vlc with sudo apt install vlc solved the problem!
I'm using a MediaListPlayer instance to execute a playlist. On a standard MediaPlayer instance you can use MediaPlayer.audio_set_volume(newVolume), but when I try to use the same method(audio_set_volume(newVolume)) on a MediaListPLayer instance, I get an error.:
AtributeError: 'MediaListPLayer' object has no attribute 'audio_set_volume'. What is the replacement of that method for the MediaListPlayer?
This is the code:
from vlc import Instance
playlist = ['/home/user/Music/01 Signs.mp3','/home/user/Music/2U.mp3']
player = Instance()
mediaListPlayer = player.media_list_player_new()
mediaList = player.media_list_new()
for element in playlist:
mediaList.add_media(player.media_new(element))
mediaListPlayer.set_media_list(mediaList)
mediaListPlayer.play()
mediaListPlayer.audio_set_volume(80)
Two years later was wondering the same thing. So here is a solution that worked for me:
import vlc
inst = vlc.Instance()
player = inst.media_list_player_new()
media_list = inst.media_list_new(["example.mp3"])
player.set_media_list(media_list)
player.play()
player.get_media_player().audio_set_volume(50)
MediaListPlayer.get_media_player() returns the MediaPlayer that can be used to control the volume of the MediaListPlayer during playback.
As I said in my comment, it does look like an oversight.
However, I have managed to set the initial volume by hacking a sub_player but once it is set and you invoke the list player, I haven't found a way of adjusting it after that.
import vlc
import time
playlist=['/home/rolf/vp1.mp3','/home/rolf/vp.mp3']
inst = vlc.Instance()
sub_player = inst.media_player_new()
player = inst.media_list_player_new()
mediaList = inst.media_list_new(playlist)
player.set_media_list(mediaList)
volume = 60
sub_player.audio_set_volume(volume)
sub_player.play()
playing = set([1,2,3,4])
player.play()
while player.get_state() in playing:
time.sleep(1)
I have posted a question on videolan ,https://forum.videolan.org/viewtopic.php?f=32&t=139505 so someone with a greater knowledge of these things might provide a better solution. If I get an answer, I'll post it here.
I want to add playlist functionality to my music player. The first track in the list plays. And typing "next" in the console and hitting return should start playing the next track but song stops playing and nothing happens.
setting the state to GST_STATE_READY instead of GST_STATE_NULL before
changing the "location" also does not work.
Can someone correct my code and tell me where I am wrong?
import pygst, gobject, time, sys
pygst.require("0.10")
import gst
class AudioPlayer:
def __init__(self):
self.songs = ['Holy Diver.mp3','Paranoid.mp3','Fast as a Shark.mp3']
# create a new gstreamer pipeline
self.pipeline = gst.Pipeline("mypipeline")
# add a file source to the pipeline
self.filesrc = gst.element_factory_make("filesrc","source")
self.pipeline.add(self.filesrc)
# add a generic decoder to the pipeline and link it to thesource
self.decode = gst.element_factory_make("decodebin","decode")
self.decode.connect("new-decoded-pad", self.decode_link)
self.pipeline.add(self.decode)
self.filesrc.link(self.decode)
# add a convertor to the pipeline
self.convert = gst.element_factory_make("audioconvert","convert")
self.pipeline.add(self.convert)
# add an alsa sink to the pipeline and link it to theconvertor
self.sink = gst.element_factory_make("alsasink", "sink")
self.pipeline.add(self.sink)
self.convert.link(self.sink)
# start playing
self.filesrc.set_property("location", self.songs.pop(0))
self.pipeline.set_state(gst.STATE_PLAYING)
def decode_link(self, dbin, pad, islast):
pad.link(self.convert.get_pad("sink"))
def next(self):
self.convert.unlink(self.sink)
self.filesrc.set_state(gst.STATE_NULL)
self.filesrc.set_property("location", self.songs.pop(0))
self.convert.link(self.sink)
self.pipeline.set_state(gst.STATE_PLAYING)
return True
player = AudioPlayer()
loop = gobject.MainLoop()
gobject.threads_init()
context = loop.get_context()
while 1:
value = sys.stdin.readline()
if value == "next\n":
player.next()
context.iteration(True)
In next you are setting the wrong thing to NULL:
self.filesrc.set_state(gst.STATE_NULL)
should be
self.pipeline.set_state(gst.STATE_NULL)
That is the crux of your problem, you aren't stopping the pipeline but also you do not need to unlink and relink self.convert either but that is a side issue.
I am trying to set volume while playing an audio file but it doesn't seem to work at all. Please what am I doing wrong?
# create a vlc playable object from source
self.playable = vlc.libvlc_media_new_path(self.instance, sourceURL)
# create a new vlc player
self.player = vlc.libvlc_media_player_new_from_media(self.playable)
# play
vlc.libvlc_media_player_play(self.player)
while not self.stop:
sleep(10) # sleep for a while to allow playback
self.player.audio_set_volume(50) # suppose to reduce volume. Doesn't work
sleep(10) # sleep for a while to allow playback
self.stop = True
UPDATE try the following:
self.instance = vlc.Instance()
self.mediaplayer = self.instance.media_player_new()
self.media = self.instance.media_new(unicode(sourceURL))
self.mediaplayer.set_media(self.media)
self.media.parse()
while True :
sleep(10)
self.mediaplayer.audio_set_volume(50)
Example usage
Keep in mind that volume_level is an integer between 0 and 100, 100 will be equal to 0db.
Made a big edit because I think I originally misunderstood a bit. Does the video output work for you?