Note
Go to the end to download the full example code.
Video playing made simple#
This shows how to play a video file in expyfun. It requires that FFmpeg (for Pyglet >= 1.4) or AVBin (otherwise) has already been installed.
@author: drmccloy

exp_name: simple video example
date: 2025-10-27 20_19_09.825544
file: /home/circleci/project/examples/stimuli/simple_video.py
participant: foo
session: foo
2025-10-27 20:19:09,825 - INFO    - Expyfun: Using version 2.0.0.dev0 (requested dev)
2025-10-27 20:19:09,908 - INFO    - Expyfun: Setting up sound card using pyglet backend with 2 playback channels
2025-10-27 20:19:09,994 - WARNING - Expyfun: Mismatch between reported stim sample rate (48000.0) and device sample rate (44100.0). Experiment Controller will resample for you, but this takes a non-trivial amount of processing time and may compromise your experimental timing and/or cause artifacts.
2025-10-27 20:19:09,994 - INFO    - Expyfun: Setting up screen
2025-10-27 20:19:10,048 - EXP     - Expyfun: Set screen visibility True
2025-10-27 20:19:10,053 - INFO    - Initialized [720 480] window on screen XlibScreenXrandr(display=<pyglet.display.xlib.XlibDisplay object at 0x7f48acd262c0>, x=0, y=0, width=1400, height=900) with DPI 69.73
2025-10-27 20:19:10,053 - INFO    - Expyfun: Initializing dummy triggering mode
2025-10-27 20:19:10,055 - INFO    - Expyfun: Initialization complete
2025-10-27 20:19:10,055 - EXP     - Expyfun: Participant: foo
2025-10-27 20:19:10,055 - EXP     - Expyfun: Session: foo
2025-10-27 20:19:11,222 - INFO    - Expyfun: Exiting
2025-10-27 20:19:11,228 - EXP     - Expyfun: Audio stopped and reset.
from expyfun import ExperimentController, building_doc, fetch_data_file
from expyfun import analyze as ea
print(__doc__)
movie_path = fetch_data_file("video/example-video.mp4")
ec_args = dict(
    exp_name="simple video example",
    window_size=(720, 480),
    full_screen=False,
    participant="foo",
    session="foo",
    version="dev",
    output_dir=None,
)
screenshot = None
with ExperimentController(**ec_args) as ec:
    ec.load_video(movie_path)
    ec.video.set_scale("fit")
    t_zero = ec.video.play()
    while not ec.video.finished:
        if ec.video.playing:
            fliptime = ec.flip()
        if screenshot is None:
            screenshot = ec.screenshot()
        if building_doc:
            break
        ec.check_force_quit()
    ec.delete_video()
    ec.flip()
    ec.screen_prompt("video over", max_wait=1.0)
ea.plot_screen(screenshot)
Total running time of the script: (0 minutes 1.611 seconds)