ffmpeg: combine two mono live audio stream to single stereo stream - audio

I have two live audio streams, they both are MONO streams.
Now I want these two streams to merge in one stream and output to single new stream.
I am merging two input streams with following command:
ffmpeg -i rtmp://myIp:1935/live/stream1 -i rtmp://myIp:1935/live/stream2 -codec:a aac -strict -2 -filter_complex "[0:a][1:a]amerge" -f flv rtmp://myIp:1935/live/myStream
The above command works but when I listen to new stream i.e. myStream, both streams are able to listen but in separate channel. Means stream1 is only in Left channel and stream2 is only in Right channel.
What I want is, both input stream stream1 and stream2 should be available to both the channels (Left & Right).
I have tried lot but can't get success and I am not good at ffmpeg also. So Is there anyone who can help me in this ???

Basically you're on the right track with your approach. But you used the amerge filter (the same filter is named join in LIBAV). This filter combines individual mono streams into a single multichannel streams.
From your description, it looks rather that you want to achieve something different: you want to mix those two mono streams into a single mono stream. You can achieve that with the amix-Filter.

Related

ffmpeg audio encoding based on codec and not on stream identifier

I have an RTSP Stream with one video stream and three audio streams as the source. Two of the audio streams are encoded with .mp2 and one is encoded with .ac-3. I want to convert the .mp2 streams to AAC. This would be easy if the .mp2streams would have the same stream identifier every time I start ffmpeg, but unfortunately the stream identifiers change. This means sometimes the two .mp2 streams are 0:a:0 and 0:a:1 and the next time they are 0:a:1 and 0:a:2.
Is there an option to re-encode only the .mp2 streams and keep the .ac-3 stream untouched?
I should probably also mention that this encoding is used for live TV so it is not an option to produce intermediate files or have several ffmpeg commands.
Try
ffprobe -show_entries stream_tags -select_streams a INPUT_URL
and see if there are any stream tags (metadata) that distinguishes mp2 streams. Then you can use the metadata stream specifier to selectively set re-encoding:
ffmpeg ... -c copy -c:a:m:{name}:{value} ac3 ...
where {name} and {value} are the name and value of the tag, respectively.
Reference on stream specifier: https://ffmpeg.org/ffmpeg.html#Stream-specifiers-1
If there isn't any usable tag, your only solution likely is to run ffprobe first to identify the stream # before running ffmpeg.

FFMPEG: Properly sidechain_compress stereo background with stereo sidechain into stereo output

I'm doing voiceover and since Sony Vegas does not support sidechaining, I render voiceover into voices.wav and then use sidechain_compress filter, as per ffmpeg documentation:
ffmpeg -y -i background.m4a -i voices.wav -filter_complex \
"[1:a]asplit=2[sc][mix];\
[0:a][sc]sidechaincompress=threshold=0.015:ratio=2:level_sc=0.8:release=500:attack=1[compr];\
[compr][mix]amerge" sidechain_1.wav
voices.wav is a stereo audio file, as well as background.m4a. But here's how the result file looks like when loaded into Sony Vegas:
This shows that in channels 1/2 I get the compressed background, while in channel 3 and 4 I get two mono tracks that somehow differ (probably, that's the original voices input and somewhat altered voices input, both in mono). UPD: I don't want to further process resulting tracks in Sony Vegas, I'd prefer ffmpeg to be the last step in my production process. The screenshot above is for illustration purposes only.
Is the background gets sidechain compressed with only left or right channel of voices? If so, how to change that to make it compressed by both channels (some voices are panned into left or right, so there might be actual difference in compressed result)
What are those channels 3 and 4? Why are they mono?
How do I get single 1/2 stereo track in the output wav file instead of this weird 4 channels in 3 tracks? (I've looked at pan complex filter, but didn't figure out how to set it up in my case).
amerge adds the channels of the inputs. amix uses the channel count of the input with the most channels. So, switch to amix.
ffmpeg -y -i background.m4a -i voices.wav -filter_complex \
"[1:a]asplit=2[sc][mix];\
[0:a][sc]sidechaincompress=threshold=0.015:ratio=2:level_sc=0.8:release=500:attack=1[compr];\
[compr][mix]amix" sidechain_1.wav

OBS alternative for server: Creating a continuous video streaming to RTMP server and beign able to manipulate it using NodeJS

What I want is to be able to create a livestream from a Ubuntu v14.04 server to a RTMP Server (like Twitch) and to be able to use NodeJS to control visual aspects (adding layers, text, images) and add different sources (video files, others livestreams, etc). Like having OBS running on a server.
What I've done/researched so far:
FFmpeg
With ffmpeg I can can create video files streams like that:
ffmpeg -re -i video.mp4 -c:v libx264 -preset fast -c:a aac -ab 128k -ar 44100 -f flv rtmp://example.com
Also using the filter_complex I can create something near to a layer like this tutorial explains:
https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos
But I found the following problems:
The streams that I create with ffmpeg only last until the video file is over, if I wanted to stream multiple video files (dynamic playlist) it would interrupt the stream between each file;
The manipulation is very limited as far as I am concerned, I can't edit filter_complex once ffmpeg is executing;
Can't display text and create animated overlays, like sliding text.
I tried to search for any cli/nodejs package that is able to create a continuos video stream and manipulate it to use as input source for ffmpeg which streams to the RTMP server.
Can someone give me more information about what I am trying to do?
I'm playing with github.com/fluent-ffmpeg/node-fluent-ffmpeg to see if I have a different outcome.

Sync end of audio and video in ffmpeg?

I'd like to sync an audio and video file together so that they end at the same time. Is there an easy way to do this with ffmpeg?
The only ideas I have are 1) finding the lengths of the two (which is annoying) and delaying the shorter one by the difference between the two lengths, and 2) reversing the two, syncing them, then reversing again. There's got to be a better way...
After finding the lengths of the two and calculating the required delay, you can run
ffmpeg -i video -i audio -af "adelay=6500|6500" -c:v copy output
Here, both the channels of a stereo audio stream are delayed by 6500 milliseconds i.e. 6.5 seconds. The video stream is copied over without recompression.

Merging two video streams and saving as one file

I'm writing chat application with video call using webRTC. I have two MediaStreams, remote and local and want to merge and save them as one file. So when opening a file, i shall see large video frame (remote stream) and little video frame at top right (local stream). Now I can record these two streams separately using RecordRTC. How can i merge them with nodejs? (no code because I don't know how it's done)
You can use FFmpeg with -filter_complex, here is a working and tested example using FFmpeg version N-62162-gec8789a:
ffmpeg -i main_video.mp4 -i in_picture.mp4 -filter_complex "[0:v:0]scale=640x480[main_video]; [1:v:0]scale=240x180[in_picture];[main_video][in_picture]overlay=390:10" output.mp4
So, this command tells FFmpeg to read from two input files, main_video.mp4 and in_picture.mp4, then it send some information to the -filter_complex flag...
The -filter_complex flag takes the [0:v:0] (first input, first video track) and scale this video to be 640x480px and it identifies the video as [main_video], then, takes the [1:v:0] (second input, video track 0) and resize the video to 240x180px naming the video [in_picture], then it merges both videos making an overlay of the second one at x=390 y=10.
Then it saves the output to output.mp4
It is that what you want?
UPDATE: I forgot to add, all you need in node is a module to run FFmpeg, there are plenty of those:
https://nodejsmodules.org/tags/ffmpeg

Resources