Body: <p>I'm having trouble muxing the video from one source with the audio from another because the audio drifts over time. As I mentioned in the title, the sources are at different frame rates, the video's source is at 23.976, and the audio's source is at 29.97.</p>
  
  <p>Maybe my current code will help to better understand the situation:</p>
  
  <pre><code>ffmpeg -y -i source1.mkv -itsoffset 0.85 -i source2.mp4 -map 0:0 -map 1:1 -c:v libx264 -crf 23 -c:a aac -b:a 112k output.mp4
  </code></pre>
  
  <p>The audio source's audio starts a bit early, hence the 0.85 second delay.</p>
  
  <p>I'm using FFmpeg version: 20170904-6cadbb1</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-08T09:18:39.833Z
Comments.Text: The (originally paired) video's framerate does not apply to the audio. You'll probably have to rescale the external audio. Check the stream durations: `ffprobe file -show_streams`
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-08T13:59:26.937Z
Comments.Text: Yeah I understand that audio doesn't work in frame rates but that doesn't explain why it goes out of sync. I tried slowing down the audio by matching its length to the video source's with the atempo audio filter, but it doesn't really work due to the offset.
Comments.UserDisplayName: Chuff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-08T14:41:49.067Z
Comments.Text: Try scaling to `audio length = video length - offset` and mux with the offset. This assumes that the drift is linear.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-08T15:12:25.447Z
Comments.Text: You mean to input into atempo the value of the video source's length minus the 0.85s offset, but to also include the offset in the code? so `ffmpeg -y -i videosource.mkv -itsoffset 0.85 -i audiosource.aac -map 0:0 -map 1:0 -af atempo=(video source's length - the offset) / video source's length -c:v libx264 -crf 23 out.mp4`? If so, it didn't work.
Comments.UserDisplayName: Chuff
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-08T15:17:10.383Z
Comments.Text: atempo = oldaudiolength / newaudiolength where new length is `video stream's length - the offset`.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-08T15:31:35.580Z
Comments.Text: Still doesn't work.
Comments.UserDisplayName: Chuff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-13T16:03:48.783Z
Comments.Text: works for me: `ffmpeg -i video_25fps.mkv -map 0:a:0 -af atempo=23976/25000 -c ac3 -b:a 192k audio_23.976fps.ac3 && ffmpeg -i video_23.976fps.mkv -itsoffset 0 -i audio_23.976fps.ac3 -c copy -map 0:v:0 -map 1:a:0 result.mkv`. im using a temporary audio file, so i can use `-c copy` in the second ffmpeg call. you may need to adjust `itsoffset` to fix audio/video sync. (i was a bit surprised that this requires to recode the audio... but well)
Comments.UserDisplayName: milahu
Tags: <video><ffmpeg><audio><encoding><sync>
Title: How can I mux video and audio from separate sources that're at different frame rates?
UserDisplayName: Chuff
ThreadText: # How can I mux video and audio from separate sources that're at different frame rates?
  
  (Asked by: username_0 on 2017-09-08)
  
  I'm having trouble muxing the video from one source with the audio from another because the audio drifts over time. As I mentioned in the title, the sources are at different frame rates, the video's source is at 23.976, and the audio's source is at 29.97.
  
  Maybe my current code will help to better understand the situation:
  
  ```
  ffmpeg -y -i source1.mkv -itsoffset 0.85 -i source2.mp4 -map 0:0 -map 1:1 -c:v libx264 -crf 23 -c:a aac -b:a 112k output.mp4
  
  ```
  
  The audio source's audio starts a bit early, hence the 0.85 second delay.
  
  I'm using FFmpeg version: 20170904-6cadbb1
  
  **Tags:** video, ffmpeg, audio, encoding, sync
  
  > **Comments:**
  > **username_1**: The (originally paired) video's framerate does not apply to the audio. You'll probably have to rescale the external audio. Check the stream durations: `ffprobe file -show_streams`  
  > **username_0**: Yeah I understand that audio doesn't work in frame rates but that doesn't explain why it goes out of sync. I tried slowing down the audio by matching its length to the video source's with the atempo audio filter, but it doesn't really work due to the offset.  
  > **username_1**: Try scaling to `audio length = video length - offset` and mux with the offset. This assumes that the drift is linear.  
  > **username_0**: You mean to input into atempo the value of the video source's length minus the 0.85s offset, but to also include the offset in the code? so `ffmpeg -y -i videosource.mkv -itsoffset 0.85 -i audiosource.aac -map 0:0 -map 1:0 -af atempo=(video source's length - the offset) / video source's length -c:v libx264 -crf 23 out.mp4`? If so, it didn't work.  
  > **username_1**: atempo = oldaudiolength / newaudiolength where new length is `video stream's length - the offset`.  
  > **username_0**: Still doesn't work.  
  > **username_2**: works for me: `ffmpeg -i video_25fps.mkv -map 0:a:0 -af atempo=23976/25000 -c ac3 -b:a 192k audio_23.976fps.ac3 && ffmpeg -i video_23.976fps.mkv -itsoffset 0 -i audio_23.976fps.ac3 -c copy -map 0:v:0 -map 1:a:0 result.mkv`. im using a temporary audio file, so i can use `-c copy` in the second ffmpeg call. you may need to adjust `itsoffset` to fix audio/video sync. (i was a bit surprised that this requires to recode the audio... but well)  
  
  ---
  
  ## 0 answers
  
  