Created
December 26, 2011 12:26
-
-
Save smathot/1521059 to your computer and use it in GitHub Desktop.
Use VLC to play videos on a PyGame surface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!