Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use VLC to play videos on a PyGame surface
import os
import sys
import vlc
import pygame
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()
# Pass pygame window id to vlc player, so it can render its contents there.
player.set_hwnd(pygame.display.get_wm_info()['window'])
# 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

This comment has been minimized.

Copy link

@wildlux 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

This comment has been minimized.

Copy link

@WardBenjamin WardBenjamin commented Feb 5, 2014

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

This comment has been minimized.

Copy link

@chozc 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

This comment has been minimized.

Copy link

@jmm0 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

This comment has been minimized.

Copy link

@joelngwt joelngwt commented Dec 18, 2015

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

This comment has been minimized.

Copy link

@MahanMehrvarz MahanMehrvarz commented Mar 17, 2016

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

This comment has been minimized.

Copy link

@Krakawet Krakawet commented Oct 23, 2016

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

This comment has been minimized.

Copy link

@ljaraque ljaraque commented Dec 13, 2016

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.