Storing multiple channels with different encoding/sampling in a single WAV file - audio

I have two RTP streams (one for each call direction) that I want to mix in a single WAV file.
The problem is that the two streams may use different codecs (and therefore different sampling frequency, encoding, etc).
Is it possible to store the two RTP streams in a WAV file using two channels (i.e. stereo)? Asked differently, is there a way to store multiple channels with different encoding, sampling frequency, etc?

Structure of the WAV file assumes that sampling rate and channel bitness is the same for all channels of the feed. Encoding applies the entire feed (with many encodings/formats/codecs you cannot separate a channel without decoding the feed).
You will need to store feeds in separate files, or you need a file format which supports multiple audio tracks (MP4, MKV for example) though they all have their own restrictions.

As Roman R. mentioned it is not immediate. You will need to take an extra step in between to convert whatever you have on your RTP stream into a proper WAV file. The idea is to use a software like ffmpeg to do so:
2 × mono → stereo: ffmpeg -i left.mp3 -i right.mp3 -ac 2 output.wav
After that you could try something of the flavor (untested):
ffmpeg -i rtp://leftrtp -i rtp://rightrtp -ac 2 output.wav
Most likely you will need to tune the codec settings to make it work as you want. You can Google around and find some infos on the subject or read the ffmpeg doc.

Related

How to merge several audio files using Libav API?

Currently, I am implementing a new feature of my software using the Libav API. This is the requirement: to merge a list of audio files (MP3 and WAV) and create a unique
audio file (MP3) as output. Note: The challenge is not about concatenating files, but merging them. When the output sound is played, all the input audio content must sound at the same time, as when you merge several files in a video editor.
I was researching about Libav audio streams, and I am just guessing that my requirement is related to the "channels" concept, I mean, that there is possible to include several audios in the stream, using one channel per audio or something like that. I was hoping to find more information about this topic, but FFmpeg/Libav documentation is actually scarce.
Right now, I am able to merge several audio streams to a video stream successfully and I can create a playable MP4 file. My problem is that players like MPlayer/VLC only reproduce the first audio stream with the video, the other two audio streams are ignored.
I was looking at the set of examples included in the FFmpeg source code, but there is nothing specifically related to my requirement, so I would appreciate any
source code reference or algorithm explanation about how to merge several audio files into one using libav. Thanks.
Update:
The ffmpeg command to merge several audio files requires de filter flag "amix", like in this example:
ffmpeg -i 1.mp3 -i 2.mp3 -i 3.mp3 -filter_complex amix=inputs=3:duration=first result.mp3
All the syntax related to this option is described in the FFmpeg Documentation
Checking the FFmpeg source code, it seems the amix feature implementation is included in the file af_amix.c
I am not 100% sure, but it seems the general algorithm is described in the function:
static int activate(AVFilterContext *ctx)
Do you know how to merge several audio files using command line ffmpeg? It would help you if you first understand how to do it with the ffmpeg command then reverse engineer how it achieves it. It's all about how to constrct a filtergraph and pass data through it.
As for examples, check out examples/filter_audio.c and examples/filtering_audio.c
This C example gets two WAV audio files and merges them to generate a new WAV file using ffmpeg-4.4 API. Tip: The key of the process is to use these filters: abuffer, amix and abuffersink.
https://github.com/xtingray/audio_mixer/
Although it doesn't support MP3 format as the output, it gives you the basics to understand how to implement your own requirements. I hope it can be handy for anyone looking for references about this specific topic.

adding silent audio to channels 3-8 in ffmpeg

This has been discussed before, but my question is a bit different.
Lets say have a video file with 1+2 as stereo.
Now I want to add 3-8 with silent audio - most likely using the anullsrc.
I need to map that generator to only affect channel 3-8.
Anyone has a solution for that?
No need for a source filter. Just need pan.
ffmpeg -i input -af "pan=8C|c0=c0|c1=c1" -c:v copy out
The first two output channels have their maps set to the two input channels. Since the other 6 are omitted, they will be mute.

Ffmpeg, avconv and sameq

Earlier I wrote so:
ffmpeg -i input.mp4 -sameq output.mp3
...and thus receive audio from video file. Ffmpeg just taken out or converted audio to mp3 with an appropriate quality. All thanks to key: -sameq [use same quantizer as source]
Now in Ubuntu instead of ffmpeg we have libav and there (in man for avcomv) I see no -sameq key. Well, here is a question: what I have to do now?..
What I have to do now to get converted audio file with the same quality as in original?
PS. -sameq : Use same quantizer as source (implies VBR).
$ man ffmpeg | col -b > ./man_ffmpeg
this man_ffmpeg is there: http://pastebin.com/qYxz1M1E
FFMPEG(1)
NAME
ffmpeg - ffmpeg video converter
SYNOPSIS
ffmpeg [[infile options][-i infile]]... {[outfile options] outfile}...
...
...
...
-sameq
Use same quantizer as source (implies VBR).
...
...
...
SEE ALSO
avplay(1), avprobe(1), avserver(1) and the Libav HTML documentation
AUTHORS
The Libav developers
2014-02-06
FFMPEG(1)
You are correct, -sameq option has been deprecated and then removed from avconv, there were many reasons for it. Not the least of it being that there are different quantizers and it makes little sense talking about same quantizer parameters when reencoding between different codecs.
Majority of people, when reencoding are looking for quality, not quantizers. So they should use -qscale n where n is between 1 and 31 representing quality from best to worst.
In a way if you have gotten used to -sameq option, you have fallen victim to a tool that should have been there at best for testing purposes. It doesn't produce anything reasonable, and can be kinned to trying to put "same metadata" into the container that doesn't support it, or doing "copy stream" into an archaic file format (leading to things like AVI with vorbis audio, that can't even be played). You can hack something together that does all these things, but it has no place in a video encoding tool.
I suggest that if you are going to be doing much stress testing of different containers and codecs, then you install ffmpeg which has more tools allowing the creation of frankensteins. If you are reencoding for the purposes of actually keeping the files that you produce or distributing them, than you can create another question explaining your situation, and what is your desired outcome.
In short "How can i create reencoding process with exactly the same quantizer?" Can only be answered with "No".

Difference between audio encoding/decoding and format conversion

Recently i have been trying to convert an audio file from one format to another through ffmpeg. i was trying to do some google but results made me a little confused about the difference between encoding and decoding an audio file and converting from one format to another.
Let me describe it this way: There are several different file formats for video files (sometimes also called "wrappers"). There are also several different codecs which can be used to encode (or compress) the audio and video. Audio and video use different codecs - and the encoded formats can be sorted in different file types/formats.
So when you talk about "encoding" vs. "converting" a couple of things come into play.
"Encoding" would be the act of taking audio/video and encoding them into a given codec(s). "Converting" implies having stuff in one format, but wanting it in another. There are two ways of looking at this:
Often called "repackaging" - this is when the video (for example) has been encoded correctly (let's say h264, with a bunch of parameters), but you want it in a different file-type - maybe it's an .AVI and you wanted it in an .MP4. This doesn't involve changing the actual video - just re-wraping the h264 stream in a new "wrapper", and is thus a fast operation.
Re-encoding. Let's say your audio was in a MP3 format, and you wanted it in an AAC format. This would require decoding the entire MP3 stream, and re-encoding it into AAC.
Obviously you can also do "1" and "2" together.
Refer Formats and Codecs for detailed information.
Hope it helps!

Mix Audio tracks with offset in SOX

From ASP.Net, I am using FFMPEG to convert flv files on a Flash Media Server to wavs that I need to mix into a single MP3 file. I originally attempted this entirely with FFMPEG but eventually gave up on the mixing step because I don't believe it it possible to combine audio only tracks into a single result file. I would love to be wrong.
I am now using FFMPEG to access the FLV files and extract the audio track to wav so that SOX can mix them. The problem is that I must offset one of the audio tracks by a few seconds so that they are synchronized. Each file is one half of a conversation between a student and a teacher. For example teacher.wav might need to begin 3.3 seconds after student.wav. I can only figure out how to mix the files with SOX where both tracks begin at the same time.
My best attempt at this point is:
ffmpeg -y -i rtmp://server/appName/instance/student.flv -ac 1 student.wav
ffmpeg -y -i rtmp://server/appName/instance/teacher.flv -ac 1 teacher.wav
sox -m student.wav teacher.wav combined.mp3 splice 3.3
These tools (FFMEG/SoX) were chosen based on my best research, but are not required. Any working solution would allow an ASP.Net service to input the two FMS flvs and create a combined MP3 using open-source or free tools.
EDIT:
I was able to offset the files using the delay switch in SOX.
sox -M student.wav teacher.wav combined.mp3 delay 2.8
I'm leaving the question open in case someone has a better approach than the combined FFMPEG/SOX solution.
For what it's worth, this should be possible with a combination of -itsoffset and the amix filter, but a bug with -itsoffset prevents it. If it worked, the command would look something like this:
ffmpeg -i student.flv -itsoffset 3.3 -i teacher.flv -vn -filter_complex amix out.mp3
mixing can be pretty simple: how to mix two audio channels?
well i suggest you should use flash.
it may sounds weird, correct me if im wrong but with Flash's new multimedia abilities you can mix a couple tracks.
im not sure, but i'm just trying to help you,
theese 2 link can help you for your aim (specially second link i guess);
http://3d2f.com/programs/25-187-swf-to-mp3-converter-download.shtml
http://blog.debit.nl/2009/02/mp3-to-swf-converter-in-actionscript-3/

Resources