python vlc control on windows - python

I am working on a python project to control vlc player - volume up/down, play/pause, rewind/forward and I have installed python2.7, vlc 3.0.8 32bit on windows 7. this is the code below which is to play the local mp4 file
import vlc
instance = vlc.Instance()
player = instance.media_player_new()
Media = instance.media_new('d:\1280.mp4')
Media.get_mrl()
player.set_media(Media)
player.play()
problem is the keywords - Instance and other functions are not getting highlighted except import and am getting the following error
Traceback (most recent call last):
File "C:\Python27\vlc3.py", line 4, in <module>
Media = instance.media_new('d:\1280.mp4')
File "C:\Python27\vlc.py", line 1916, in media_new
m._instance = self
AttributeError: 'NoneType' object has no attribute '_instance'
I am not able to understand please help me out

Use forward slashes instead of backward slashes.
Use:
Media = instance.media_new('d:/1280.mp4')
Instead of:
Media = instance.media_new('d:\1280.mp4')

Related

Error in python script using python-vlc and pafy

Recently I have created a python script to play youtube videos using pafy and python-vlc.
The below code is the script:
# importing vlc module
import vlc
# importing pafy module
import pafy
# url of the video
url = "https://www.youtube.com/watchv=il_t1WVLNxk&list=PLqM7alHXFySGqCvcwfqqMrteqWukz9ZoE"
# creating pafy object of the video
video = pafy.new(url)
# getting stream at index 0
best = video.streams[0]
# creating vlc media player object
media = vlc.MediaPlayer(best.url)
# start playing video
media.play()
And after running it I get this error:
Traceback (most recent call last):
File "C:\Users\harsh\Desktop\don't.py", line 11, in <module>
video = pafy.new(url)
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site-
packages\pafy\pafy.py", line 124, in new
return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site-
packages\pafy\backend_youtube_dl.py", line 31, in __init__
super(YtdlPafy, self).__init__(*args, **kwargs)
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site-
packages\pafy\backend_shared.py", line 97, in __init__
self._fetch_basic()
File "C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site-
packages\pafy\backend_youtube_dl.py", line 54, in _fetch_basic
self._dislikes = self._ydl_info['dislike_count']
KeyError: 'dislike_count'
Please help me with this error. If you have any questions please ask.
The dislike button on youtube has been made private, so some modification on backend_youtube_dl.py is required to run pafy.
Navigate to C:\Users\harsh\AppData\Local\Programs\Python\Python310\lib\site-
packages\pafy
Open backend_youtube_dl.py file
Comment or remove this code: self._dislikes = self._ydl_info['dislike_count']
Besides the dislike function, everything else works fine.
It worked for me and hope the same goes for everyone.
Pafy its very usefull tool if you want to extract information about youtube video like, retrieve metadata such as viewcount, duration, rating, author, thumbnail, keywords or Download video or audio at requested resolution.
But it doesnt work(at least untill now) to play a video.
If you want to play a Yt video inside your code use it:
import webbrowser
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
webbrowser.open(url)

Audio isn't loading

I have used pygame a lot, but never used sound before. I have checked and the files are there by the right name, and I have used the right pathway, but I just get this error message:
Traceback (most recent call last):
File "C:\Users\Rajive\AppData\Local\Programs\Python\Python3.4.3\D C W.py", line 11, in module
door_opening = pygame.mixer.Sound('C:\Users\Rajive\Downloads\door_opening.m4a')
pygame.error: Unable to open file 'C:\Users\Rajive\Downloads\door_opening.m4a'
import random, time, pygame
time = time.clock()
pygame.init()
diff = 1
door_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\door_opening.m4a')
door_closing = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\door_closing.m4a')
closet_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\closet_opening.m4a')
closet_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\closet_closing.m4a')
window_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\window_opening.m4a')
window_opening = pygame.mixer.Sound('C:\\Users\\Rajive\\Downloads\\window_closing.m4a')
From pygame documentation:
The Sound can be loaded from an OGG audio file or from an uncompressed WAV.
Doesn't look like the .m4a codec is supported. Perhaps try converting your file first.

Attempting to embed a video in pygame using pyglet

im trying to display a video on a pygame surface using the pyglet library to import, play and then convert the frames into an image. I managed to battle my way through installing avbin but now I'm hitting a type error with my code
def cutscene(window_surface, filename):
player = pyglet.media.Player()
source = pyglet.media.load(filename)
player.queue(source)
player.play()
pygame.display.flip()
while True:
window_surface.fill(0)
player.dispatch_events()
tex = player.get_texture()
raw = tex.get_image_data().get_data('RGBA',tex.width*4)
raw = ctypes.string_at(ctypes.addressof(raw), ctypes.sizeof(raw))
img = pygame.image.frombuffer(raw, (tex.width, tex.height), 'RGBA')
window_surface.blit(img, (0,0))
pygame.display.flip()
when I run, i get the following error
Traceback (most recent call last):
File "dino_game.py", line 348, in <module>
main()
File "dino_game.py", line 45, in main
cutscene(window_surface, "Cutscenes/Cutscene1.mov")
File "dino_game.py", line 68, in cutscene
raw = tex.get_image_data().get_data('RGBA',tex.width*4)
AttributeError: 'NoneType' object has no attribute 'get_image_data'
Nothing that I do seems to solve it
EDIT: So after testing both this file and the sample files provided by pyglet, it seems that I get this error no matter what filetype I use, could this be an installation error with pyglet or AVbin?
Gave up trying to use pyglet and swapped to VLC where I just have to pass the window ID for the game and it does the rest for me
VLC script: https://wiki.videolan.org/Python_bindings/

VLC python module not working in Ubuntu

I have code like this
import vlc # no error
Instance = vlc.Instance() # no error
MediaPlayer = Instance.media_player_new() # error triggered here
the error message is
Instance.media_player_new()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "vlc.py", line 895, in media_player_new
p._instance = self
AttributeError: 'NoneType' object has no attribute '_instance'
I am using python 2.6
Whats the problem with my code?
Looks like a bug. I did a peek into the code at https://github.com/geoffsalmon/vlc-python/blob/master/generated/vlc.py (see snippets below).
Looks like media_player_new calls libvlc_media_player_new( self ) and libvlc_media_player_new calls the native libvlc_media_player_new( ... ) function. This function returns NULL which results in None and the further code fails.
According to the doc string NULL will be returned if an error occurs. Maybe you could enable logging via the libvlc_log functions and see what's going wrong. Refer to http://www.videolan.org/developers/vlc/doc/doxygen/html/group_libvlc_log.html
EDIT: it looks like that you can set up logging via vlc.py, too. (You shouldn't need native calls then)
def media_player_new(self, uri=None):
"""Create a new MediaPlayer instance.
#param uri: an optional URI to play in the player.
"""
p = libvlc_media_player_new(self)
if uri:
p.set_media(self.media_new(uri))
p._instance = self
return p
and
def libvlc_media_player_new(p_libvlc_instance):
'''Create an empty Media Player object.
#param p_libvlc_instance: the libvlc instance in which the Media Player should be created.
#return: a new media player object, or NULL on error.
'''
f = _Cfunctions.get('libvlc_media_player_new', None) or \
_Cfunction('libvlc_media_player_new', ((1,),), class_result(MediaPlayer),
ctypes.c_void_p, Instance)
return f(p_libvlc_instance)

ID3-mp3 editing in python - up-to-date package?

Edit: Trying to get these libraries to work in python 3.3 was clearly the wrong approach, and my problem now is entirely different so I'll just re-ask it in a new question.
I want to be able to edit ID3 tags of mp3 files with python commands, for example something like setAlbumName("folderPath\song.mp3", "albumname"). So far I've tried Mutagen, PyID3, pytagger, eyeD3, and they all seem to be outdated because the installation fails due to syntax errors. I tried to fix it in eyeD3, but I hit a dead end: http://i41.tinypic.com/o6zklv.png (second screenshot from after I had fixed all the prints and "except Error, e" and so on).
I tried the same with Mutagen, but I ran into a wall there as well when replacing "raise KeyError, key" with "raise KeyError as key" didn't work.
I didn't even know what to make of this one (pytagger): http://i41.tinypic.com/29fz7mh.png
It seems to suggest that there's something wrong with my python installation? Not getting into that.
So, would anyone like to point me to an ID3 package that works, or have a go at fixing an outdated one?
(Also, I tried both "python setup.py install" and "setup.py install" and it seemed to make no difference. I'm on windows 8.)
Edit: From the screenshot below, plus the source code (mutagen with python 2.7.5)
from mutagen.mp3 import MP3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = MP3(p)
audio["title"] = "An example"
audio.pprint()
audio.save()
_
Traceback (most recent call last):
File "id3tag.py", line 5, in <module>
audio.pprint()
File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 138, in pprint
try: tags = self.tags.pprint()
File "C:\Python27\lib\site-packages\mutagen\id3.py", line 190, in pprint
frames = list(map(Frame.pprint, self.values()))
TypeError: unbound method pprint() must be called with Frame instance as first a
rgument (got str instance instead)
_
from mutagen.mp3 import MP3
p = "E:\\Musik\\Aeon\\2005 Bleeding the False\\01 Cenobites - Copy.mp3"
audio = MP3(p)
audio["title"] = "An example"
audio.save()
_
Traceback (most recent call last):
File "id3tag.py", line 7, in <module>
audio.save()
File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 132, in save
return self.tags.save(filename, **kwargs)
File "C:\Python27\lib\site-packages\mutagen\id3.py", line 370, in save
framedata = [self.__save_frame(frame) for (key, frame) in frames]
File "C:\Python27\lib\site-packages\mutagen\id3.py", line 461, in __save_frame
framedata = frame._writeData()
AttributeError: 'str' object has no attribute '_writeData'
mutagen works great for me with Python 2.7.
examples:
https://code.google.com/p/mutagen/wiki/Tutorial
from mutagen.mp3 import MP3
audio = MP3("example.mp3")
audio["title"] = "An example"
audio.pprint()
audio.save()
p.s. please post code samples so people can help.. not links to screenshots.
p.p.s. it looks like you are trying to install Python2 libs into Python3.
Mutagen also has an EasyID3 tool, which handles simple tasks like changing the file's title:
from mutagen.easyid3 import EasyID3
f = EasyID3("file.mp3")
f["title"] = u"Some title"
f.save()
Works like a charm. But it has very restricted functionality.
See more examples at http://code.google.com/p/mutagen/wiki/Tutorial

Categories

Resources