<p>I got it working by adding <code>-fflags nobuffer -flags low_delay -strict experimental</code> before all the inputs.
Got helped reading <a href="https://stackoverflow.com/questions/57278415/how-to-sync-multiple-rtsp-inputs-in-ffmpeg">this</a> thread</p>

CC BY-SA 4.0
2020-10-01T15:32:24.920Z
2020-10-01T15:32:24.920Z
cub33
<p><code>-vsync</code> parameter</p>

<p>Video sync method. </p>

<ul>
<li><code>0</code>: Each frame is passed with its timestamp from the demuxer to the muxer </li>
<li><code>1</code>: Frames will be duplicated and dropped to achieve exactly the requested constant framerate. </li>
<li><code>2</code>: Frames are passed through with their timestamp or dropped to prevent 2 frames from having the same timestamp </li>
<li><code>-1</code>: Chooses between 1 and 2 depending on muxer capabilities. This is the default method. </li>
</ul>

<p>With <code>-map</code> you can select from which stream the timestamps should be taken. You can leave either video or audio unchanged and sync the remaining stream(s) to the unchanged one.</p>

<p><code>-async samples_per_second</code></p>

<p>Audio sync method. "Stretches/squeezes" the audio stream to match the timestamps, the parameter is the maximum samples per second by which the audio is changed. <code>-async 1</code> is a special case where only the start of the audio stream is corrected without any later correction.</p>

<p><code>-copyts</code></p>

<p>Copy timestamps from input to output.</p>

CC BY-SA 4.0
2020-03-02T23:47:16.060Z
2020-03-04T21:25:20.030Z
2020-03-04T21:25:20.030Z
Igor Bat
<p>I'm trying to combine (side by side) two live video streams coming over RTMP, using the following ffmpeg command:</p>

<pre><code>ffmpeg -i "rtmp://first" -i "rtmp://first" -filter_complex "[0v][1v]xstack=inputs=2:layout=0_0|1920_0[stacked]" -map "[stacked]" -preset ultrafast -vcodec libx264 -tune zerolatency -an -f flv output.flv
</code></pre>

<p>In this example I actually use the same input stream two times, because the issue is more visible this way. And the issue is in the output two streams are out of sync by about 2-3 seconds. That is - I expect (since I have two identical inputs) to have exactly the same left and right sides in the output. Instead - left side is behind right side by 2-3 seconds.</p>

<p>What I believe is happening is ffmpeg connects to inputs in order (I see this in output log) and connection to each one takes 2-3 seconds (maybe it waits for I-frame, those streams have I-frame interval of 3 seconds). Then probably, it buffers frames received from first (already connected) input, while connecting to the second one. When second one is connected and frames from both inputs are ready to be put through the filter - first input buffer already contains 2-3 seconds of video - and result is out of sync.</p>

<p>Again, that's just my assumptions. So, how can achieve my goal? What I basically want is for ffmpeg to discard all "old" frames received before BOTH inputs are connected OR somehow put "empty" (black?) frames for second input, while waiting for that second input to become available. I tried play with various flags, with PTS (setpts filter), but to no avail.</p>

CC BY-SA 4.0
2020-03-06T06:49:45.440Z
Can you expose or share one of these URLs?
Gyan
CC BY-SA 4.0
2020-03-09T07:55:05.110Z
@Gyan I cannot share such url, but it's produced by streaming my desktop via OBS Studio to rtmp endpoint on server (which is private, so I cannot share it) and consuming that endpoint. Server doesn't do any processing of that RTMP stream, so I think it can be reproduced. Same happens when I stream my camera (or any other "live" source). I consume the same stream 2 times in my example in question, so that synchronization issue is very visible.
Evk
CC BY-SA 4.0
2020-04-30T11:37:05.110Z
I'm trying to do something similar and have tried various combinations of `-vsync`, `-max_delay`, `-copyts` and `-start_at_zero` but not managed to synchronise my two live streams yet.
IpsRich
CC BY-SA 4.0
2020-04-30T12:14:56.313Z
@RichardWiseman yes, I've given up on this and had to manually mix frames using C++ (still with the help of ffmpeg, but not with command-line utility).
Evk
<video><ffmpeg><encoding><rtmp>
Combining two live RTMP streams into another RTMP stream, synchronization issues (with FFMPEG)
Evk
# Combining two live RTMP streams into another RTMP stream, synchronization issues (with FFMPEG)

(Asked by: username_0 on 2020-03-02)

I'm trying to combine (side by side) two live video streams coming over RTMP, using the following ffmpeg command:
ffmpeg -i "rtmp://first" -i "rtmp://first" -filter_complex "[0v][1v]xstack=inputs=2:layout=0_0|1920_0[stacked]" -map "[stacked]" -preset ultrafast -vcodec libx264 -tune zerolatency -an -f flv output.flv

In this example I actually use the same input stream two times, because the issue is more visible this way. And the issue is in the output two streams are out of sync by about 2-3 seconds. That is - I expect (since I have two identical inputs) to have exactly the same left and right sides in the output. Instead - left side is behind right side by 2-3 seconds.
What I believe is happening is ffmpeg connects to inputs in order (I see this in output log) and connection to each one takes 2-3 seconds (maybe it waits for I-frame, those streams have I-frame interval of 3 seconds). Then probably, it buffers frames received from first (already connected) input, while connecting to the second one. When second one is connected and frames from both inputs are ready to be put through the filter - first input buffer already contains 2-3 seconds of video - and result is out of sync.
Again, that's just my assumptions. So, how can achieve my goal? What I basically want is for ffmpeg to discard all "old" frames received before BOTH inputs are connected OR somehow put "empty" (black?) frames for second input, while waiting for that second input to become available. I tried play with various flags, with PTS (setpts filter), but to no avail.

**Tags:** video, ffmpeg, encoding, rtmp

> **Comments:**
> **username_1**: Can you expose or share one of these URLs?  
> **username_0**: @username_1 I cannot share such url, but it's produced by streaming my desktop via OBS Studio to rtmp endpoint on server (which is private, so I cannot share it) and consuming that endpoint. Server doesn't do any processing of that RTMP stream, so I think it can be reproduced. Same happens when I stream my camera (or any other "live" source). I consume the same stream 2 times in my example in question, so that synchronization issue is very visible.  
> **username_2**: I'm trying to do something similar and have tried various combinations of `-vsync`, `-max_delay`, `-copyts` and `-start_at_zero` but not managed to synchronise my two live streams yet.  
> **username_0**: @RichardWiseman yes, I've given up on this and had to manually mix frames using C++ (still with the help of ffmpeg, but not with command-line utility).  

---

## 2 answers

### Answer by username_3 on 2020-10-01. Score: 2

I got it working by adding `-fflags nobuffer -flags low_delay -strict experimental` before all the inputs.
Got helped reading [this](https://stackoverflow.com/questions/57278415/how-to-sync-multiple-rtsp-inputs-in-ffmpeg) thread


---

### Answer by username_4 on 2020-03-02. Score: 1

`-vsync` parameter

Video sync method.

* `0`: Each frame is passed with its timestamp from the demuxer to the muxer
* `1`: Frames will be duplicated and dropped to achieve exactly the requested constant framerate.
* `2`: Frames are passed through with their timestamp or dropped to prevent 2 frames from having the same timestamp
* `-1`: Chooses between 1 and 2 depending on muxer capabilities. This is the default method.

With `-map` you can select from which stream the timestamps should be taken. You can leave either video or audio unchanged and sync the remaining stream(s) to the unchanged one.

`-async samples_per_second`

Audio sync method. "Stretches/squeezes" the audio stream to match the timestamps, the parameter is the maximum samples per second by which the audio is changed. `-async 1` is a special case where only the start of the audio stream is corrected without any later correction.

`-copyts`

Copy timestamps from input to output.


---

