Using ffmpeg, extract audio streams from video file and output to single stereo wav file - audio

I've been trying for a few hours to get the right arguments to do the below operation with ffmpeg, but i'm a bit out of my depth. If anyone can give me some guidance, that would be very helpful.
Input file is MXF with one video track and two separate 48k 24b PCM audio tracks.
I want to extract just the audio, map first track to left, second track to right and output as a stereo wave file.
I am trying variations on the below command, but it seems to be unhappy about the video stream, giving the error
"mapchan: stream #0.0 is not an audio stream."
ffmpeg -i test2.mxf -vn -map_channel 0.0.1 output.wav_CH0 -map_channel 0.0.2 output.wav_CH1
Here is ffmpeg's analysis of the streams in case it is helpful:
Stream #1:0: Video: mpeg2video (4:2:2), yuv422p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], max. 50000 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc
Stream #1:1: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:2: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:3: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:4: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:5: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:6: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:7: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s
Stream #1:8: Audio: pcm_s24le, 48000 Hz, mono, s32, 1152 kb/s

The filtergraph function seems to do the trick.
ffmpeg -i test2.mxf -filter_complex "[0:1:0][0:2:0]amerge=inputs=2[aout]" -map "[aout]" output.wav
The 0:1:0 seems to represent input1 (the -i option), stream1 (the track), channel (left/right if it were stereo). --could be wrong with my interpretation here, but it works nonetheless.
ffmpeg documentation and FFmpeg Wiki: Audio Channel Manipulation were very helpful.

Related

Use ffmpeg to downscale, tonemap, downmix and re encode mkv

I try to use ffmpeg to downscale a 4k and tonemap a 4k HDR mkv to a 1080p SDR mkv with this code:
ffmpeg -i "Input.mkv" -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:v hevc_nvenc -b:v 12M -preset slow "Output.mkv"
The problem is, that only the first audio track (of four) is copied to the new mkv and the subtitle tracks are missing:
Input:
Stream #0:0: Video: hevc (Main 10), yuv420p10le(tv, bt2020nc/bt2020/smpte2084), 3840x2160 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 23.98 tbc (default)
Stream #0:1(ger): Audio: eac3, 48000 Hz, 7.1, fltp (default)
Stream #0:2(ger): Audio: dts (DTS-HD MA), 48000 Hz, 7.1, s16p
Stream #0:3(eng): Audio: truehd, 48000 Hz, 7.1, s32 (24 bit)
Stream #0:4(eng): Audio: ac3, 48000 Hz, 5.1(side), fltp, 640 kb/s
Stream #0:5(ger): Subtitle: subrip (default) (forced)
Stream #0:6(ger): Subtitle: dvd_subtitle, 1920x1080 (forced)
Stream #0:7(ger): Subtitle: hdmv_pgs_subtitle, 1920x1080 (forced)
Stream #0:8(ger): Subtitle: dvd_subtitle, 1920x1080
Stream #0:9(ger): Subtitle: hdmv_pgs_subtitle
Stream #0:10(eng): Subtitle: dvd_subtitle, 1920x1080
Stream #0:11(eng): Subtitle: hdmv_pgs_subtitle
Output:
Stream #0:0: Video: hevc (Main), yuv420p(tv), 3840x2160 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn, 23.98 tbc (default)
Stream #0:1(ger): Audio: vorbis, 48000 Hz, 7.1, fltp (default)
Stream #0:2(ger): Subtitle: ass (default) (forced)
I would like to have an mkv as output converts all the audio tracks to aac Stereo (but keeps all four of them), copies the subtitle tracks and tonemaps/downscales the video track.
I have tried to use the -map 0 or -map 0:a:0 -map 0:a:1 ... commands (and some similiar to those, however I seemingly end up with either one audio track, no video track or a video track that is just copied.
I possible, I would also like to use nvenc with a high quality preset, which is the reason for -c:v hevc_nvenc -b:v 12M -preset slow in my command, however I have no idea, if this is done right, since the ouput mkv also as a 4k video track instead of a 1080p. Maybe this is caused by the -c:v because it overrides -vf? Sorry, I am feeling dumb and am just getting started with ffmpeg.
There is so much information about ffmpeg out there, but it is either too complicated for me, or not answering my questions.
Thanks for your help!
ffmpeg -i "Input.mkv" -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -map 0 -c:v hevc_nvenc -b:v 12M -preset slow -c:s copy -ac 2 "Output.mkv"
Add -map 0 to include all streams (default behavior only chooses 1 stream per type). See FFmpeg Wiki: Map.
-ac 2 for stereo audio.
-c:s copy to stream copy subtitles. Video and audio are being filtered, so they can't be stream copied and must be re-encoded.
I'm not a user of NVENC so I can't comment on that.

Merge two MP3 files and maintain high bitrate and original properties of MP3s

I have two MP3 files that were created from the same source, with different audio within them. Here are the properties from ffprobe
Duration: 00:00:08.86, bitrate: 384 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 24000 Hz, 1 channels, s16, 384 kb/s
NOTE: Even though the file is an MP3 it shows as pcm_s16le
When I try and join the two files together using
ffmpeg -i download.mp3 -i download1.mp3 -filter_complex [0:a:0][1:a:0]concat=n=2:v=0:a=1[outa] -map [outa] joineddownloads.mp3
I get the following result and a big drop in bitrate(quality)
Duration: 00:00:10.42, start: 0.046042, bitrate: 32 kb/s
Stream #0:0: Audio: mp3, 24000 Hz, mono, fltp, 32 kb/s
How can I maintain the high 320kbs bitrate and all the other properties that were present before I created the joined file?
To avoid re-encoding - concatenate the two mp3s
First create a text file ‘files.txt’ containing two lines:
file '/path/download.mp3'
file '/path/download1.mp3'
Second:
ffmpeg -f concat -i files.txt -c copy out.mp3

Extract audio with ffmpeg, linux

I'm trying to extract audio tracks from some Avi videos and save them to their own files, ideally without re-encoding.
I've had a look through here https://www.ffmpeg.org/ffmpeg.html#Audio-Options and here ffmpeg to extract audio from video though I'm getting errors regardless of the approach I try.
My latest command string is:
ffmpeg -i /home/d/Pictures/Test/input-video.AVI -map 0:a -vn -acodec copy /home/d/Pictures/Test/output-audio.m4a
The key part of the output is:
Guessed Channel Layout for Input Stream #0.1 : mono
Input #0, avi, from '/home/d/Pictures/Test/input-video.AVI':
Duration: 00:00:05.94, start: 0.000000, bitrate: 18131 kb/s
Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 17995 kb/s, 30.28 fps, 30.28 tbr, 30.28 tbn, 30.28 tbc
Stream #0:1: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 11025 Hz, 1 channels, s16, 176 kb/s
File '/home/d/Pictures/Test/output-audio.m4a' already exists. Overwrite ? [y/N] y
[ipod # 0x1d89520] Codec for stream 0 does not use global headers but container format requires global headers
[ipod # 0x1d89520] Could not find tag for codec pcm_s16le in stream #0, codec not currently supported in container
Output #0, ipod, to '/home/d/Pictures/Test/output-audio.m4a':
Metadata:
encoder : Lavf56.40.101
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 11025 Hz, mono, 176 kb/s
Stream mapping:
Stream #0:1 -> #0:0 (copy)
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
I'm believe I have got the right audio stream number from this output and thus am assuming the "-map 0:a" part isn't the problem.
I'm running on Linux Mint 18.1
MP4 family of formats don't store PCM audio, so you either have to re-encode or save to another format, like Matroska.
ffmpeg -i video.AVI -map 0:a -vn -acodec copy audio.mka

Not able to change the program number for the video

I have been trying to change the program number of a particular .ts file using FFmpeg without any success. I saw the documentation and it mentions to use the -program option. But, I am not able to add the streams as I desire. Here's the program information for the stream that I possess currently:
Input #0, mpegts, from 'output_2_without_pgm_num.ts':
Duration: 00:01:49.92, start: 1.400000, bitrate: 1816 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv, progressive), 720x576 [SAR 64:45 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x101](ger): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, fltp, 384 kb/s (clean effects)
Stream #0:2[0x102](eng): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, fltp, 384 kb/s (clean effects)
Stream #0:3[0x103](ger): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006)
Now, I want to change the program number from 1 to 22. Any idea how I could do that? I have tried using -program option, but I can't add streams 2 and 3 to the newly created .ts file.
Thanks in advance!
There are two steps involved here. Telling ffmpeg which streams to include in the output using the -map option, and setting the program metadata, using the -program option.
ffmpeg -i in.ts -map 0 -c copy -program program_num=22:st=0:st=1:st=2:st=3 out.ts
-map 0 tells ffmpeg to include all streams from the first input.
The st values tell ffmpeg which output stream indices should be included in the program.

Transcoding HD RTP/UDP stream with FFMPEG

I've a Tera STI440 streamer with two inputs used (for TV channel streaming). When I watch the source stream it seems allright, but when I start FFMPEG transcoding, the image is going to collapse on random times (probably around every 5-15 sec). The transcoding is okay with SD 480P streams.
FFPROBE one of my HD stream:
`
Input #0, rtp, from 'rtp://239.192.24.3:1234/':
Duration: N/A, start: 18002.926656, bitrate: N/A
Program 202
Metadata:
service_name : M5 HD
service_provider: Magyar Televizio
Stream #0:1: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt709, top first), 1440x1080 [SAR 4:3 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:0(hun): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 128 enter code herekb/s
FFPROBE one of my SD stream (Which is working properly):
Input #0, rtp, from 'rtp://239.192.24.4:1234/':
Duration: N/A, start: 51003.435044, bitrate: N/A
Program 203
Metadata:
service_name : RTL Klub
service_provider: Magyar RTL Telev�zi� Zrt.
Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, bt470bg, top first), 720x576 [SAR 12:11 DAR 15:11], 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1(hun): Audio: aac_latm (HE-AAC) ([17][0][0][0] / 0x0011), 48000 Hz, stereo, fltp
I want to do a HLS stream so I ran this script for FFMPEG:
/usr/bin/ffmpeg -loglevel 16 -i rtp://239.192.24.3:1234/ -s pal -c:v libx264 -c:a libmp3lame -b:v 1000k -b:a 96k -ar 44100 -vf 'yadif=0:-1:1' -preset:v superfast -f hls -hls_time 7 -hls_list_size 10 -hls_wrap 10 -hls_base_url http://10.3.1.3:8080/ts/ -hls_segment_filename /tvman/hls_out/ts/M5_hq_%03d /tvman/hls_out/subplaylist/M5-hq.m3u8
I access the playlist file from NGINX but also tried Apache too.
Here's some log from FFMPEG process (loglevel 16)
I made some diagnostics:
Looked for network traffic (it's around 60 mbps, so it should be okay with a 100mbps ethernet
CPU & RAM load allright
I reduced the network traffic by turning off TV channel streams on streamer. It was around 6-7 mbps.
If you need any more information, I'll provide it for you.
Thanks for the help!
P.S.: I'm not native English speaker, so sorry for mistakes. :)

Resources