ffmpeg audio encoding based on codec and not on stream identifier - audio

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.

Related

How do you encode raw pcm_f32le audio to AAC encoded audio with FFmpeg (C/C++)?

I am trying to encode raw audio (pcm_f32le) to AAC encoded audio. One thing I've noticed is that I can accomplish this via the CLI tool:
ffmpeg -f f32le -ar 48000 -ac 2 -c:a pcm_f32le -i out.raw out.m4a -y
This plays just fine and decodes fine.
The steps I've taken:
When I am using the C example code: https://ffmpeg.org/doxygen/3.4/encode_audio_8c-example.html and switch the encoder to codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
Output the various sample formats associated with AAC, it only provides FLTP. That assumes a planar/interleaved format.
This page seems to provide the various supported input formats per codec.
This is confusing because I don't think my raw captured audio is interleaved. I've certainly tried passing it through and it doesn't work as intended.
It will stay stuck here with this ret code indefinitely after calling avcodec_receive_packet:
AVERROR(EAGAIN): output is not available in the current state - user must try to send input
Questions:
How can I modify the example code from FFmpeg to convert pcm_f32le raw audio to AAC encoded audio?
Why is the CLI tool able to?
I am using libsoundio to capture raw audio from Linux's Dummy Output. I wonder how I could get a planar format to pass through to get AAC encoded audio.
If AAC is not a possibility, is doing so with MP3?
Find here a working example of how to encode raw pcm_f32le to aac with ffmpeg

MPEG-DASH streaming live using encoded stream buffer

I have been trying to implement HTTP live streaming using mpeg-dash but need guidance on some issues.
Provided :
I have audio and video encoded stream in buffered input.
a direct mpeg-2 transport stream for above is also available in a buffer.
Current approach :
Save the transport stream into chunks of fixed length.
use ffmpeg to extract video stream.
ffmpeg -i latest_chunk.ts -s 720x480 -c:v libx264 -b:v 600k -y -an output_video_stream.mp4
use ffmpeg to extract video stream.
ffmpeg -i latest_chunk.ts -c:a aac -b:a 128k -y -vn output_audio_stream.mp4
use mp4box to create dash segments and mpd.
mp4box -dash 7000 -profile live output_video_stream.mp4 output_audio_stream.mp4 -out manifest.mpd
A server running continuously in another thread serves the generated mpd and segments.
Issues :
The above approach gives a considerable amount of latency. Can this be done more efficiently?
I want to know if there is a method to take directly encoded streams buffer as input and produce mpeg-dash segments and mpd. HTTP server will do the rest. If there is, please provide an example.
Also i provided the length of the transport stream chunks (in sec) in mp4box as argument -mpd-refresh 12, but the player only requests for the mpd once, plays the segments, and stops. It also does not include minimumUpdatePeriod attribute in the generated mpd file
mp4box -dash 7000 -profile live -mpd-refresh 12 output_video_stream.mp4 output_audio_stream.mp4 -out manifest.mpd
Does the mpeg-dash has support for mpeg-2 encoded media streams?
Any advice/solution/reference for the same is appreciated.

Extract audio from Transport Stream and preserve length

I'm using ffmpeg to extract audio from MPEG Transport Stream file recorded by DVB-S card. The command:
ffmpeg -i video.ts -vn audio.wav
The source file seems to be corrupted. I noticed the corruption happens from time to time, especially for videos longer than 1 hour. I've got errors like these:
[mp2 # 0x1bb5500] Header missing
Error while decoding stream #0:1
[mpegts # 0x17eaf40] Continuity check failed for pid 5261 expected 2 got 6
The problem is that the resulting audio.wav is shorter than the source video (40m33s and 40m59s accordingly). I'm looking for the way to preserve the original length in the resulting audio file.
I tried the recent ffmpeg under Windows and avconv under Ubuntu, output format was MP3 and WAV. For every case I've got the same results.
I didn't find whether it's possible to do it with ffmpeg however I found ProjectX - a tool which tries to fix the broken TS stream. Website: http://project-x.sourceforge.net/
With:
java -jar ProjectX.jar -demux my_video.ts
the stream is demuxed into audio and video files which are guaranteed to have the same length. I simply mux them back using ffmpeg.

AAC bitstream not in ADTS format and extradata missing

With FFMPEG, I'm sending a stream from Computer A over to Computer B, via UDP.
This is done over a MPEGTS stream, encoded with libx264 and aac.
Computer B takes this stream with FFMPEG and puts it into an m3u8 playlist.
After a random time (2-35 minutes), the message
[mpegts # 0533f000] AAC bitstream not in ADTS format and extradata missing
av_interleaved_write_frame(): Invalid data found when processing input
appears.
What I figures is that the receiving FFMPEG can't read the header file of the audio part for this particular package, and since it can't put video and audio together anymore, it stops creating the .ts files and just stops running.
Here's the cmdline of the receiving stream:
ffmpeg -i udp://address -vcodec copy -acodec copy -map 0 -f segment -segment_list playlist.m3u8 -analyzeduration 100000 -probesize 100000-segment_list_flags +live-cache -segment_time 8 -segment_wrap 10 out%03d.ts
Now I need to know the answer to either one of these 2 questions:
1) Can I put something in my commandline in order to avoid this particular problem or
2) Can I tell FFMPEG to just ignore it for this particular message, quite possibly creating weird audio or none at all, and to simply move on to the next one?

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

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.

Resources