-
-
Save smathot/1521059 to your computer and use it in GitHub Desktop.
import os | |
import sys | |
import vlc | |
import pygame | |
def callback(self, player): | |
print 'FPS =', player.get_fps() | |
print 'time =', player.get_time(), '(ms)' | |
print 'FRAME =', .001 * player.get_time() * player.get_fps() | |
if len( sys.argv )< 2 or len( sys.argv )> 3: | |
print 'Usage: vlctest <file_name>' | |
else: | |
# Enable in Windows to use directx renderer instead of windib | |
#os.environ["SDL_VIDEODRIVER"] = "directx" | |
pygame.init() | |
screen = pygame.display.set_mode((800,600),pygame.RESIZABLE) | |
pygame.display.get_wm_info() | |
print "Using %s renderer" % pygame.display.get_driver() | |
print 'Playing: %s' % sys.argv[1] | |
# Get path to movie specified as command line argument | |
movie = os.path.expanduser(sys.argv[1]) | |
# Check if movie is accessible | |
if not os.access(movie, os.R_OK): | |
print('Error: %s file not readable' % movie) | |
sys.exit(1) | |
# Create instane of VLC and create reference to movie. | |
vlcInstance = vlc.Instance() | |
media = vlcInstance.media_new(movie) | |
# Create new instance of vlc player | |
player = vlcInstance.media_player_new() | |
# Add a callback | |
em = player.event_manager() | |
em.event_attach(vlc.EventType.MediaPlayerTimeChanged, \ | |
callback, player) | |
# Pass pygame window id to vlc player, so it can render its contents there. | |
win_id = pygame.display.get_wm_info()['window'] | |
if sys.platform == "linux2": # for Linux using the X Server | |
player.set_xwindow(win_id) | |
elif sys.platform == "win32": # for Windows | |
player.set_hwnd(win_id) | |
elif sys.platform == "darwin": # for MacOS | |
player.set_agl(win_id) | |
# Load movie into vlc player instance | |
player.set_media(media) | |
# Quit pygame mixer to allow vlc full access to audio device (REINIT AFTER MOVIE PLAYBACK IS FINISHED!) | |
pygame.mixer.quit() | |
# Start movie playback | |
player.play() | |
while player.get_state() != vlc.State.Ended: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit(2) | |
if event.type == pygame.KEYDOWN: | |
print "OMG keydown!" | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
print "we got a mouse button down!" | |
WardBenjamin: I think that this script imports the vlc.py script that is referred to here: https://wiki.videolan.org/Python_bindings
Thanks for the gist, helped me a lot.
Took me a few hours to figure out why pygame mouse events didn't work. You'll need to disable VLC's event capturing:
player.video_set_mouse_input(False)
player.video_set_key_input(False)
thanks for the nice code, I am a beginner and have a silly question. but I can't find where to define the specific video that the code will play. right now it only prints . 'Usage: vlctest <file_name>'. :D
Mahan : you see the sys.argv
part in the code ? It means that some arguments are used by this script. Try :
./vlctest /path/to/myvideo.mpg
:)
Hi. I was using this script and works fine! But I have a pygame application and I want to play the video with VLC only on a corner of my pygame window, because in the remaining area there will be some text and figures. Do you know how to play the video on a specific partial zone of the display??
thanks a lot!
So dope! Thank you so much!
After I close the pygame window in which I played the video, I always get that message:
[0000018278c0e800] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[000001827990a070] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[0000018279ab6c50] avcodec decoder: Using D3D11VA (Intel(R) UHD Graphics, vendor 8086(Intel), device 9b21, revision 2) for hardware decoding
Why is that?
Thanks, man, this is great alternative to Pygame's rather limited movie player.
Is there any way to disable the mouse cursor completely during the playback though? I am using this to make a game with cutscenes, but after the video ends, Pygame is unable to turn off the mouse visibility again.