Difference between revisions of "Video Playback"

From TSG Doc
Jump to navigation Jump to search
Line 393: Line 393:
  
 
- '''Testing''': Always test playback on different devices or players to confirm synchronization.
 
- '''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==
 
==Editing==

Revision as of 15:48, 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 playback

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.

Python

Example demonstrating how to play a video with audio:

 1from psychopy import logging, prefs
 2prefs.hardware['audioLib'] = ['PTB']
 3prefs.hardware['audioLatencyMode'] = 2
 4
 5from psychopy import visual, core, event
 6from psychopy.hardware import keyboard
 7
 8# File paths for video and audio
 9video_file = "tick_rhythm_combined_30min.mp4"
10
11win = visual.Window(size=(1024, 768), fullscr=False, color=(0, 0, 0))
12
13video = visual.VlcMovieStim(
14    win, filename=video_file,
15    autoStart= False
16)
17
18kb = keyboard.Keyboard()
19
20# Play the video
21win.flip()
22core.wait(3.0)
23video.play()
24video_start_time = core.getTime()
25
26# Main loop for video playback
27while video.status != visual.FINISHED:
28    # Draw the current video frame
29    video.draw()
30    win.flip()
31
32    keys = kb.getKeys(['q'], waitRelease=True)
33    if 'q' in keys:
34        break
35
36win.close()
37core.quit()

Example demonstrating how to play a video with audio disconected:

 1from psychopy import logging, prefs
 2from psychopy import visual, core, sound, event
 3import time
 4
 5prefs.hardware['audioLib'] = ['PTB']
 6prefs.hardware['audioLatencyMode'] = 2
 7
 8# File paths for video and audio
 9video_file = "tick_rhythm_30min.mp4"
10audio_file = "tick_rhythm_30min.wav"
11
12win = visual.Window(size=(1280, 720), fullscr=False, color=(0, 0, 0), units="pix")
13
14video = visual.VlcMovieStim(
15    win, filename=video_file,
16    size=None,  # Use the native video size
17    pos=[0, 0], 
18    flipVert=False,
19    flipHoriz=False,
20    loop=False,
21    autoStart=False,
22    noAudio=True,
23    volume=100,
24    name='myMovie'
25)
26
27# Load the audio
28audio = sound.Sound(audio_file, -1)
29
30# Synchronize audio and video playback
31win.flip()
32time.sleep(5)
33 
34audio.play()
35time.sleep(0.04)
36video.play()
37video_start_time = core.getTime()
38
39while video.status != visual.FINISHED:
40    # Draw the current video frame
41    video.draw()
42    win.flip()
43
44    # Check for keypress to quit
45    if "q" in event.getKeys():
46        audio.stop()
47        break
48
49# Close the PsychoPy window
50win.close()
51core.quit()

Example demonstrating how to disconnect audio from video:

 1import os
 2import subprocess
 3
 4input_file = 'tick_rhythm_combined_1min.mp4'
 5
 6directory = os.path.dirname(input_file)
 7base_name = os.path.splitext(os.path.basename(input_file))[0]
 8
 9output_video = os.path.join(directory, f"{base_name}_video_only.mp4")
10output_audio = os.path.join(directory, f"{base_name}_audio_only.wav")
11
12subprocess.run(['ffmpeg', '-i', input_file, '-an', output_video])
13
14subprocess.run(['ffmpeg', '-i', input_file, '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', output_audio])
15
16print(f"Video saved to: {output_video}")
17print(f"Audio saved to: {output_audio}")

Example demonstrating how to combine audio and video:

 1import os
 2import subprocess
 3
 4# --- Inputs
 5video_file = 'tick_rhythm_combined_1min_video_only.mp4'   # Your video-only file
 6audio_file = 'mic_segment.wav'                            # Your trimmed audio
 7output_file = 'final_synced_output.mp4'                   # Output file name
 8
 9# --- FFmpeg command to combine
10subprocess.run([
11    'ffmpeg',
12    '-i', video_file,
13    '-i', audio_file,
14    '-c:v', 'copy',               # Copy video stream as-is
15    '-c:a', 'aac',                # Encode audio with AAC (widely compatible)
16    '-shortest',                 # Trim to the shortest stream (prevents overhang)
17    output_file
18])
19
20print(f"Synchronized video saved to: {output_file}")

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)) ik wil hier een link naar de dll?
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

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:

right click background → Display settings → 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.

Python

Example demonstrating how to record a video with a facecam:

 1#!/usr/bin/env python3.10
 2# -*- coding: utf-8 -*-
 3
 4import datetime
 5import cv2
 6import ctypes
 7import ffmpegcv
 8
 9#set sleep to 1ms accuracy
10winmm = ctypes.WinDLL('winmm')
11winmm.timeBeginPeriod(1)
12
13def configure_webcam(cam_id, width=1920, height=1080, fps=60):
14    cap = cv2.VideoCapture(cam_id, cv2.CAP_DSHOW)
15    if not cap.isOpened():
16        print(f"Error: Couldn't open webcam {cam_id}.")
17        return None
18
19    # Try to set each property
20    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
21    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
22    cap.set(cv2.CAP_PROP_FPS, fps)
23
24    # Read back the values
25    actual_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
26    actual_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
27    actual_fps = cap.get(cv2.CAP_PROP_FPS)
28
29    print(f"Resolution set to: {actual_width}x{actual_height}")
30    print(f"FPS set to: {actual_fps}")
31
32    return cap
33
34def getWebcamData():
35    global frame_width
36    global frame_height
37
38    print("opening webcam...")
39    camera = configure_webcam(1, frame_width, frame_height)
40    time_stamp = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
41    file_name = time_stamp +'_output.avi'
42    video_writer = ffmpegcv.VideoWriter(file_name, 'h264', fps=freq)
43    
44    while True:
45        grabbed = camera.grab()
46        if grabbed:
47            grabbed, frame = camera.retrieve()
48            
49            video_writer.write(frame)  # Write the video to the file system
50            
51            frame = cv2.resize(frame, (int(frame_width/4),int(frame_height/4)))
52            cv2.imshow("Frame", frame)  # show the frame to our screen
53        
54        if cv2.waitKey(1) & 0xFF == ord('q'):
55            break
56
57freq = 60
58frame_width = 1920 
59frame_height = 1080
60
61getWebcamData()
62
63cv2.destroyAllWindows()

Audio encoding

Audio Settings

We recommend using the following settings for audio:

Codec lossless or high-quality codecs
PCM (WAV) uncompressed
Sample Rate 48 kHz

Set your audio for low-latency, high-accuracy playback with ffmpeg:

   ffmpeg -i input.wav -ar 48000 -ac 2 -sample_fmt s16 output_fixed.wav

   Explanation:
   -ar 48000 → Set sample rate to 48000 Hz (standard for ASIO/Windows audio, matches most soundcards)
   -ac 2 → Set 2 channels (stereo)
   -sample_fmt s16 → Use 16-bit signed integer samples

Windows Settings

Windows 10 Settings to check

sound → Playback → right-click → Properties → Advanced Tab:

   - Set Default Format to 48000 Hz, 16 bit, Studio Quality.

   - Disable sound enhancements.

   - In the same properties window, go to Enhancements tab → Disable all enhancements.

   - Exclusive Mode:

   - In the same Advanced tab.

   - Allow applications to take exclusive control of this device → CHECKED

   - Give exclusive mode applications priority → CHECKED

Python

Example demonstrating how to check and play your audio:

 1#!/usr/bin/env python3.10
 2
 3import psychopy
 4print(psychopy.__version__)
 5import sys
 6print(sys.version)
 7
 8import keyboard
 9from psychopy import prefs
10from psychopy import visual, core, event
11
12from psychopy.sound import backend_ptb
13# 0: No special settings (default, not optimized)
14# 1: Try low-latency but allow some delay
15# 2: Aggressive low-latency
16# 3: Exclusive mode, lowest latency but may not work on all systems
17backend_ptb.SoundPTB.latencyMode = 2
18
19prefs.hardware['audioLib'] = ['PTB']
20prefs.hardware['audioDriver'] = ['ASIO']
21prefs.hardware['audioDevice'] = ['ASIO4ALL v2']
22from psychopy import sound
23
24# --- OS-level audio device sample rate ---
25default_output = sd.query_devices(kind='output')
26print("\nDefault output device info (OS level):")
27print(f"  Name: {default_output['name']}")
28print(f"  Default Sample Rate: {default_output['default_samplerate']} Hz")
29print(f"  Max Output Channels: {default_output['max_output_channels']}")
30
31# Confirm the audio library and output settings
32print(f"Using {sound.audioLib} for sound playback.")
33print(f"Audio library options: {prefs.hardware['audioLib']}")
34print(f"Audio driver: {prefs.hardware.get('audioDriver', 'Default')}")
35print(f"Audio device: {prefs.hardware.get('audioDevice', 'Default')}")
36
37audio_file = 'tick_rhythm_5min.wav'
38
39print("Creating sound...")
40wave_file = sound.Sound(audio_file)
41
42print("Playing sound...")
43wave_file.play()
44
45while not keyboard.is_pressed('q'):
46    pass
47
48# Clean up
49print("Exiting...")
50win.close()
51core.quit()

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.

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 48000: Sets audio sample rate to 48.0 kHz.
	-fflags +genpts: Ensures accurate timestamps.
	-async 1: Synchronizes audio and video streams.

Enumeration

- 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.

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


Playback

PsychoPy

Example demonstrating how to play a video:

 1#!/usr/bin/env python3.10
 2# -*- coding: utf-8 -*-
 3
 4import time
 5import keyboard
 6from psychopy import visual 
 7from psychopy import core
 8
 9## Setup Section
10win = visual.Window([720,720], fullscr=False, monitor="testMonitor", units='cm')
11
12# append this stimulus to the list of prepared stimuli
13vlc_movies = []
14my_movies = ['YourMovie.mp4']#path to your movies from this directory
15
16for movie in my_movies:
17    mov = visual.VlcMovieStim(win, movie,
18    size=600,  # set as `None` to use the native video size
19    pos=[0, 0],  # pos specifies the /center/ of the movie stim location
20    flipVert=False,  # flip the video picture vertically
21    flipHoriz=False,  # flip the video picture horizontally
22    loop=False,  # replay the video when it reaches the end
23    autoStart=True)  # start the video automatically when first drawn
24    vlc_movies.append(mov)
25
26print("playing video....")
27while not(keyboard.is_pressed('q')) and vlc_movies[0].status != visual.FINISHED:
28    vlc_movies[0].draw()
29    win.flip()
30    buffer_in = vlc_movies[0].frameIndex
31    print(vlc_movies[0].status)
32
33print("Stop")
34
35## Closing Section
36core.quit()