Skip to content

Instantly share code, notes, and snippets.

@smathot
Created December 26, 2011 12:26
Show Gist options
  • Save smathot/1521059 to your computer and use it in GitHub Desktop.
Save smathot/1521059 to your computer and use it in GitHub Desktop.
Use VLC to play videos on a PyGame surface
import os
import sys
import vlc
import pygame
def callback(self, player):
print
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!"
@wildlux
Copy link

wildlux commented Nov 15, 2013

i try this code and run but crash!
#!/usr/bin/env python
import pygame
pygame.init()
percorso='13.mpg'

percorso='/home/pi/MULTIMEDIA/13.mpg'

a=pygame.movie.Movie(percorso)
cine=pygame.movie.Movie(percorso)
pygame.movie.Movie(percorso)

print cine

a.play()
a.set_volume(1)
del cine
pygame.quit()
cine=pygame.display.set_mode((200,300))
cine=pygame.movie.Movie(percorso)
cine.play()

@WardBenjamin
Copy link

Where can you get the vlc module that this requires? I found this: http://git.videolan.org/?p=vlc/bindings/python.git;a=blob;f=generated/vlc.py;hb=HEAD but I don't know if that is it or not.

@chozc
Copy link

chozc commented May 6, 2014

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.

@jmm0
Copy link

jmm0 commented Jun 24, 2015

WardBenjamin: I think that this script imports the vlc.py script that is referred to here: https://wiki.videolan.org/Python_bindings

@joelngwt
Copy link

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)

@MahanMehrvarz
Copy link

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

@Krakawet
Copy link

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
:)

@ljaraque
Copy link

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!

@BootsManOut
Copy link

BootsManOut commented Nov 7, 2021

So dope! Thank you so much!

@BootsManOut
Copy link

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