Video Playback: Difference between revisions

Jump to navigation Jump to search
Line 121: Line 121:
This is an example of a Python script that plays a .mp4 video file with high time accuracy. <syntaxhighlight lang="python" line> from psychopy import visual, core, prefs prefs.hardware['videoLib'] = ['avbin', 'ffpyplayer'] # Choose based on installed libraries
This is an example of a Python script that plays a .mp4 video file with high time accuracy. <syntaxhighlight lang="python" line> from psychopy import visual, core, prefs prefs.hardware['videoLib'] = ['avbin', 'ffpyplayer'] # Choose based on installed libraries


Create a window
#!/usr/bin/env python3.10
win = visual.Window(fullscr=True, monitor="testMonitor", units="pix")
# -*- coding: utf-8 -*-


Path to video file
import time
video_file = "stimulus.mp4"
import keyboard
from psychopy import visual
from psychopy import core


Load video
## Setup Section
movie = visual.MovieStim3(win, video_file, size=(1920, 1080), flipVert=False, flipHoriz=False, loop=False)
win = visual.Window([720,720], fullscr=False, monitor="testMonitor", units='cm')


Play video
# append this stimulus to the list of prepared stimuli
while movie.status != visual.FINISHED: movie.draw() win.flip()
vlc_movies = []
my_movies = ['YourMovie.mp4']#path to your movies from this directory


Close window
for movie in my_movies:
win.close() core.quit() </syntaxhighlight>
    mov = visual.VlcMovieStim(win, movie,
    size=600,  # set as `None` to use the native video size
    pos=[0, 0],  # pos specifies the /center/ of the movie stim location
    flipVert=False,  # flip the video picture vertically
    flipHoriz=False,  # flip the video picture horizontally
    loop=False,  # replay the video when it reaches the end
    autoStart=True)  # start the video automatically when first drawn
    vlc_movies.append(mov)
 
print("playing video....")
while not(keyboard.is_pressed('q')) and vlc_movies[0].status != visual.FINISHED:
    vlc_movies[0].draw()
    win.flip()
    buffer_in = vlc_movies[0].frameIndex
    print(vlc_movies[0].status)
 
print("Stop")
 
## Closing Section
core.quit()
</syntaxhighlight>