| Line 2: |
Line 2: |
| | | | |
| | 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. | | 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== |
| | + | |
| | + | === Python === |
| | + | Example demonstrating how to play a video with audio: |
| | + | <syntaxhighlight lang="python" line> |
| | + | from psychopy import logging, prefs |
| | + | logging.console.setLevel(logging.WARNING) |
| | + | prefs.hardware['audioLib'] = ['PTB'] |
| | + | prefs.hardware['audioLatencyMode'] = 2 |
| | + | |
| | + | from psychopy import visual, core, event |
| | + | from psychopy.hardware import keyboard |
| | + | |
| | + | # File paths for video and audio |
| | + | video_file = "tick_rhythm_combined_30min.mp4" |
| | + | |
| | + | win = visual.Window(size=(1024, 768), fullscr=False, color=(0, 0, 0)) |
| | + | |
| | + | video = visual.VlcMovieStim( |
| | + | win, filename=video_file, |
| | + | autoStart= False |
| | + | ) |
| | + | |
| | + | kb = keyboard.Keyboard() |
| | + | |
| | + | # Play the video |
| | + | win.flip() |
| | + | core.wait(3.0) |
| | + | video.play() |
| | + | video_start_time = core.getTime() |
| | + | |
| | + | # Main loop for video playback |
| | + | while video.status != visual.FINISHED: |
| | + | # Draw the current video frame |
| | + | video.draw() |
| | + | win.flip() |
| | + | |
| | + | keys = kb.getKeys(['q'], waitRelease=True) |
| | + | if 'q' in keys: |
| | + | break |
| | + | |
| | + | win.close() |
| | + | core.quit() |
| | + | </syntaxhighlight> |
| | + | |
| | + | Example demonstrating how to play a video with audio disconected: |
| | + | <syntaxhighlight lang="python" line> |
| | + | from psychopy import logging, prefs |
| | + | logging.console.setLevel(logging.DEBUG) |
| | + | from psychopy import visual, core, sound, event |
| | + | import time |
| | + | |
| | + | prefs.hardware['audioLib'] = ['PTB'] |
| | + | prefs.hardware['audioLatencyMode'] = 2 |
| | + | |
| | + | # File paths for video and audio |
| | + | video_file = "tick_rhythm_30min.mp4" |
| | + | audio_file = "tick_rhythm_30min.wav" |
| | + | |
| | + | win = visual.Window(size=(1280, 720), fullscr=False, color=(0, 0, 0), units="pix") |
| | + | |
| | + | video = visual.VlcMovieStim( |
| | + | win, filename=video_file, |
| | + | size=None, # Use the native video size |
| | + | pos=[0, 0], # Center of the window |
| | + | flipVert=False, |
| | + | flipHoriz=False, |
| | + | loop=False, |
| | + | autoStart=False, |
| | + | noAudio=True, |
| | + | volume=100, |
| | + | name='myMovie' |
| | + | ) |
| | + | |
| | + | # Load the audio |
| | + | audio = sound.Sound(audio_file, -1) |
| | + | |
| | + | # Synchronize audio and video playback |
| | + | win.flip() |
| | + | time.sleep(5) |
| | + | |
| | + | audio.play() |
| | + | time.sleep(0.04) |
| | + | video.play() |
| | + | video_start_time = core.getTime() |
| | + | |
| | + | while video.status != visual.FINISHED: |
| | + | # Draw the current video frame |
| | + | video.draw() |
| | + | win.flip() |
| | + | |
| | + | # Check for keypress to quit |
| | + | if "q" in event.getKeys(): |
| | + | audio.stop() |
| | + | break |
| | + | |
| | + | # Close the PsychoPy window |
| | + | win.close() |
| | + | core.quit() |
| | + | |
| | + | </syntaxhighlight> |
| | | | |
| | ==Video encoding== | | ==Video encoding== |