Difference between revisions of "Video Playback"
| Line 120: | Line 120: | ||
=== PsychoPy === | === PsychoPy === | ||
| + | Example demonstrating how to play a video: | ||
<syntaxhighlight> | <syntaxhighlight> | ||
#!/usr/bin/env python3.10 | #!/usr/bin/env python3.10 | ||
Revision as of 13:58, 28 April 2025
When using video in your experiment, especially when presenting time-critical stimuli, special care should be taken to optimize the video and audio settings on multiple levels (hardware, OS, script), as many things can go wrong along the way.
This page outlines some best practices; however, we advise to always consult a TSG member if you plan to run a video experiment in the labs.
Video encoding
When recording video for stimulus material or as input for your experiment, please: Use a high-quality camera, with settings appropriate for your application (e.g., frame rate, resolution). Use a high-quality recorder or capture device, capable of recording at 1080p (1920×1080) and 60fps or higher. Stabilize the camera and avoid automatic exposure, white balance, or focus during recording to prevent inconsistencies. Record in a controlled environment with consistent lighting and minimal background distractions. You can use the facecam for high quality video recording.
Video Settings
We recommend using the following settings:
| File format | .mp4 (H.264 codec(libx264)) |
|---|---|
| Frame rate | 60 fps (frames per second) |
| Resolution | 1920×1080 (Full HD) or match your experiment's display settings |
| Bitrate | 10-20 Mbps for Full HD video |
| Constant Frame Rate (CFR) | enforce a constant frame rate |
Python
Example demonstrating how to create a video:
1#!/usr/bin/env python3.10
2# -*- coding: utf-8 -*-
Audio encoding
Audio Settings
We recommend using the following settings:
| Codec | lossless or high-quality codecs |
|---|---|
| PCM (WAV) | uncompressed |
| Sample Rate | 48 kHz |
Python
Example demonstrating how to create audio:
1#!/usr/bin/env python3.10
2# -*- coding: utf-8 -*-
FFmpeg
Synchronization
Ensure the audio and video streams have consistent timestamps:
FFmpeg Options:
-fflags +genpts: Generates accurate presentation timestamps (PTS) for the video.
-async 1: Synchronizes audio and video when they drift.
-map 0:v:0 and -map 0:a:0: Explicitly map video and audio streams to avoid accidental mismatches.
Python
Example demonstrating how to use ffmpeg:
1#!/usr/bin/env python3.10
2# -*- coding: utf-8 -*-
Recommended FFmpeg Command
Here’s a command that encodes video and audio while maintaining high time accuracy: ffmpeg -i input.mp4 \
-c:v libx264 -preset slow -crf 18 -vsync cfr -g 30 \
-c:a pcm_s16le -ar 44100 \
-fflags +genpts -async 1 \
output.mp4
• -c:v libx264: Encode video using H.264. • -preset slow: Optimize for quality and compression efficiency. • -crf 18: Adjusts quality (lower = better; range: 0–51). • -vsync cfr: Enforces constant frame rate. • -c:a pcm_s16le: Encodes audio in uncompressed WAV format. • -ar 44100: Sets audio sample rate to 44.1 kHz. • -fflags +genpts: Ensures accurate timestamps. • -async 1: Synchronizes audio and video streams.
Tips
• Ensure Low Latency: If you're processing video/audio in real time, use low-latency settings (e.g., -tune zerolatency for H.264). • Avoid Resampling: If possible, use the original frame rate and sample rate to avoid timing mismatches. • Testing: Always test playback on different devices or players to confirm synchronization.
Alternatively, you can use Shotcut, a simple open-source editor, available here: https://shotcut.org/
The Lab Computer displays are typically set to 1920×1080 at 120Hz. We found that this is sufficient for most applications. There are possibilities to go higher.
Editing
We recommend using DaVinci Resolve for editing and converting video files. DaVinci Resolve is a free, professional-grade editing program, available here: https://www.blackmagicdesign.com/products/davinciresolve
Windows Settings
Windows 10 has a habit of automatically enabling video enhancements or unnecessary processing features, which can interfere with smooth playback. Therefore, please make sure these are disabled:
Open Settings → System → Display → Graphics Settings. If available, disable "Hardware-accelerated GPU scheduling" for critical timing experiments. For specific applications (e.g., PsychoPy), under "Graphics Performance Preference," set them to "High Performance" to ensure they use the dedicated GPU.
Playback
PsychoPy
Example demonstrating how to play a video:
#!/usr/bin/env python3.10
# -*- coding: utf-8 -*-
import time
import keyboard
from psychopy import visual
from psychopy import core
## Setup Section
win = visual.Window([720,720], fullscr=False, monitor="testMonitor", units='cm')
# append this stimulus to the list of prepared stimuli
vlc_movies = []
my_movies = ['YourMovie.mp4']#path to your movies from this directory
for movie in my_movies:
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()