# Substantial audio lag using MovieStim3

**URL:** https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273
**Category:** Coding
**Created:** [September 1, 2023, 6:01pm UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273 "2023-09-01T18:01:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lex1](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Lex1](https://discourse.psychopy.org/u/Lex1)
#### Post date: [September 1, 2023, 6:01pm UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/1 "2023-09-01T18:01:35Z")

</div>

I am trying to present mp4 files (about 10 minutes in length) for a fMRI experiment. The code that I have is working, and the movie stimulus successfully comes up and is played. However, the video lags about 3 seconds behind the audio. Is there a way to fix this? I’m not sure what is causing the delay, and can’t seem to find a solution online. For reference, I’m using MovieStim3 with psychopy v2023.2.0. The FrameRate is 60 (which matches my graphics card). Thank you for your help!

---

<div class="post-metadata">

### Author: ![jonathan.kominsky](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jonathan.kominsky](https://discourse.psychopy.org/u/jonathan.kominsky)
#### Post date: [September 4, 2023, 12:29pm UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/2 "2023-09-04T12:29:10Z")

</div>

What OS are you using, and how are the movie files encoded?

MovieStim3 isn’t particularly efficient as a movie player, unfortunately. It’s good for short clips but when it has to load a longer clip into memory it can introduce some weird behavior. You could try switching to MovieStim or vlcMovieStim and see if either of them work better, or see if there’s any media encoding that runs better (e.g., .h264 encoding, lower resolution or framerate, lower audio sample rate, etc.)

---

<div class="post-metadata">

### Author: ![Lex1](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Lex1](https://discourse.psychopy.org/u/Lex1)
#### Post date: [September 5, 2023, 1:42am UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/3 "2023-09-05T01:42:53Z")

</div>

Thank you, that is helpful information! I will try those suggestions. I primarily chose MovieStim3 because it gives more accurate timing information compared to MovieStim. Do you know of any potential work-arounds to get more accurate frame timing information using MovieStim? Sorry for asking a potentially basic question, it’s my first time using PsychoPy to present movie stimuli in an fMRI experiment.

---

<div class="post-metadata">

### Author: ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)
#### Post date: [September 5, 2023, 10:31am UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/4 "2023-09-05T10:31:02Z")

</div>

The `ffpyplayer` engine for `MovieStim` is the recommended approach and the only one the core team are now working on. It has by far the best performance so please use that and let us know if there are any issues we need to fix with that solution

thanks

---

<div class="post-metadata">

### Author: ![l\_b](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@l\_b](https://discourse.psychopy.org/u/l_b)
#### Post date: [March 18, 2024, 1:51pm UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/5 "2024-03-18T13:51:22Z")

</div>

Hey, I am running into the same problem as Lex1, even with very short mp4 videos where the audio lags a lot.  
Having seen your comment, I tried to use movieStim, which helps reduce the lagging issue, but PsychoPy freezes after the video played (still showing the last frame on the screen).  
Would you have any idea how to fix this?

I am working on Windows 10, PsychoPy3 v2023.1.2, Python 3.8.10

movie = visual.MovieStim(win, filename=“vids/test.mp4”, size=(720, 480), pos=(0, 0), volume=1.0)  
movie.play()  
while movie.status != visual.FINISHED:  
movie.draw()  
win.flip()  
movie.stop()

---

<div class="post-metadata">

### Author: ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)
#### Post date: [March 19, 2024, 11:55am UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/6 "2024-03-19T11:55:21Z")

</div>

@l_b in the code that you’ve got here the `movie.draw()` is what causes the movie to carry on being visible. If you call `win.flip()` without `movie.draw()` after the movie has finished then you’ll get a screen without drawing the final frame of the movie. It’s by design that if you choose to draw the movie after it’s finished you see the final frame still

---

<div class="post-metadata">

### Author: ![Wjpmitchell3](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wjpmitchell3/32/33648_2.png) [@Wjpmitchell3](https://discourse.psychopy.org/u/Wjpmitchell3)
#### Post date: [March 28, 2024, 11:59pm UTC](https://discourse.psychopy.org/t/substantial-audio-lag-using-moviestim3/36273/7 "2024-03-28T23:59:03Z")

</div>

Hi Jon,

I’m having a slightly different issue. I have my event coded (I think) like you described, with a subsequent win.flip() following the stimulus ending:

```auto
# Initializing keyboard
kb = keyboard.Keyboard()

# Defining our video object
# Forgive the undefined variables; part of a larger script
Stimulus = visual.MovieStim(win=win, units = units, 
                            filename=video, name=video_name)
  
# Starting the video
Stimulus.play()

# If the stimulus is still playing
while Stimulus.status != visual.FINISHED:
 
    # Draw the next frame
    Stimulus.draw()

    # Flip the frame
    win.flip()

    # if the escape key is pressed ...
    if kb.getKeys(keyList=["escape"]):

        # Quit the task
        core.quit()
  
# Flip the window
win.flip()

# Stop the stimulus  
Stimulus.stop()

```

and I am not getting the freezing on the final frame. However, I am getting the last tiny audio clip of the video playing repeatedly (about 6 times in a row), before the task actually closes. This occurs regardless of which video I play. I’ve been trying to debug this with 10 to 90 second .mp4 formatted videos, all \< 4000KB in size, but haven’t had any luck. Any ideas? If it’s just a side effect with no current solution, I can live with it. MovieStim seems to be a big improvment over MovieStim3 and is still the best performing player for my purposes, so I really appreciate the hard work on it, but figured I’d ask since I came across this thread. Thanks again for your time !
