How can I capture audio AND video simultenaous with ffmpeg from a linux USB capture device - audio

I'm capturing a video by means of an USB Terratec Grabster AV350 (which is based on the em2860 chip).
I don't succeed to get the audio when it is played . If I play the captured video with vlc or with ffplay I got only 3 seconds sound and then a silence for the rest of the video ...
During the capturing I don't get any errors. At the end it indicates the size of the video and audio captured ....
I'm using the ffmpeg command for this :
ffmpeg -f alsa -ac 2 -i hw:3 -f video4linux2 -i /dev/video0 -acodec ac3 -ab 128k -vcodec mpeg4 -b 6000k -r 25 test5.avi
The log is :
[alsa # 0x9bcd420]Estimating duration from bitrate, this may be inaccurate
Input #0, alsa, from 'hw:3':
Duration: N/A, start: 69930.998994, bitrate: N/A
Stream #0.0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
[video4linux2 # 0x9bf5d30]Estimating duration from bitrate, this may be inaccurate
Input #1, video4linux2, from '/dev/video0':
Duration: N/A, start: 1307111377.654173, bitrate: -2147483 kb/s
Stream #1.0: Video: rawvideo, yuyv422, 720x576, -2147483 kb/s, 1000k tbr, 1000k tbn, 1000k tbc
[ac3 # 0x9bf9590]No channel layout specified. The encoder will guess the layout, but it might be incorrect.
Output #0, avi, to 'test5.avi':
Metadata:
ISFT : Lavf52.64.2
Stream #0.0: Video: mpeg4, yuv420p, 720x576, q=2-31, 6000 kb/s, 25 tbn, 25 tbc
Stream #0.1: Audio: ac3, 44100 Hz, stereo, s16, 128 kb/s
Stream mapping:
Stream #1.0 -> #0.0
Stream #0.0 -> #0.1
Press [q] to stop encoding
frame= 1283 fps= 25 q=2.3 Lsize= 38677kB time=51.32 bitrate=6173.9kbits/s
**video:37755kB audio:846kB** global headers:0kB muxing overhead 0.198922%
If I reduce the command for only capturing audio, then the audio file can be played successfully :
ffmpeg -f alsa -ac 2 -i hw:3,0 -acodec ac3 -ab 128k test5.avi
[alsa # 0x8ede420]Estimating duration from bitrate, this may be inaccurate
Input #0, alsa, from 'hw:3,0':
Duration: N/A, start: 70395.998935, bitrate: N/A
Stream #0.0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
[ac3 # 0x8eebac0]No channel layout specified. The encoder will guess the layout, but it might be incorrect.
Output #0, avi, to 'test5.avi':
Metadata:
ISFT : Lavf52.64.2
Stream #0.0: Audio: ac3, 44100 Hz, stereo, s16, 128 kb/s
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop encoding
size= 227kB time=13.62 bitrate= 136.8kbits/s
**video:0kB audio:213kB** global headers:0kB muxing overhead 6.902375%
If I run the command for only video capturing then vlc or ffplay can play the video successfully :
ffmpeg -f video4linux2 -i /dev/video0 -vcodec mpeg4 -b 12000k -r 25 test5.avi
[video4linux2 # 0x91d6420]Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2, from '/dev/video0':
Duration: N/A, start: 1307112044.025687, bitrate: -2147483 kb/s
Stream #0.0: Video: rawvideo, yuyv422, 720x576, -2147483 kb/s, 1000k tbr, 1000k tbn, 1000k tbc
Output #0, avi, to 'test5.avi':
Metadata:
ISFT : Lavf52.64.2
Stream #0.0: Video: mpeg4, yuv420p, 720x576, q=2-31, 12000 kb/s, 25 tbn, 25 tbc
Stream mapping:
Stream #0.0 -> #0.0
Press [q] to stop encoding
frame= 388 fps= 25 q=2.0 Lsize= 12963kB time=15.52 bitrate=6842.5kbits/s
**video:12949kB audio:0kB** global headers:0kB muxing overhead 0.114584%
Strange behaviour I noticed is that when I tried capturing video and audio, I can not capture the audio afterwards any more,
unless I unplug the AV350 first.
The G350 is located at card 3 :
htpc#htpc-01:/proc/asound/G350/pcm0c$ more info
card: 3
device: 0
subdevice: 0
stream: CAPTURE
id: USB Audio
name: USB Audio
subname: subdevice #0
class: 0
subclass: 0
subdevices_count: 1
subdevices_avail: 1
The OS is a Linux 2.6.38-8-generic with the Ubuntu Natty Narwhal version
Any help on how to tackle this issue would be great ....
Thanks !

I was working on something like that and I used this command:
ffmpeg -y -f alsa -i default -f v4l2 -i /dev/video0 -acodec aac -strict -2 -ac 1 -b:a 64k -vcodec libx264 -b:v 300k -r 30 -g 30 prueba1.mp4
At the beginning I was having trouble with hw:3 component. Instead of that I put in default and the code actually worked. I hope this helps.

It works for high quality video:
ffmpeg -f alsa -ac 2 -i hw:1,0 -f video4linux2 -i /dev/video0 -acodec ac3 -ab \
128k -f matroska -s 1280x720 -vcodec libx264 -preset ultrafast -qp 16 testsize.mkv

I've found that using the command at the bottom works for an external USB webcam (mine is currently a Logitech C310). I used this command on my laptop, which already has a built in mic/webcam, and it still captured the audio and video from my C310. Obviously, change the /dev/video0 to fit your needs. You can use
ls /dev/video*
in terminal to see what video capture devices your OS has set up.
ffmpeg -f alsa -i default -itsoffset 00:00:00 -f video4linux2 -s 1280x720 -r 25 -i /dev/video0 out.avi

Related

Why can't I reorder the streams in my mpg container with ffmpeg?

Background: Could having audio as stream 0 and video as stream 1 explain why my MPG will play on OSX QuickTime Player, but not Win10 Movies & TV?
I've got an mpg file with audio as stream 0 and video as stream 1.
It plays fine under OSX QT Player, but not under Win10's default app.
For lack of a better idea, I'm assuming the unusual stream ordering is my problem, and I'm trying to fix it with ffmpeg.
What luck! https://trac.ffmpeg.org/wiki/Map describes exactly my case!
Re-order streams
The order of your -map options determines the order of the streams in the output. In this example the input file has audio as stream #0 and video as stream #1 (which is possible but unusual). Example to re-position video so it is listed first, followed by the audio:
ffmpeg -i input.mp4 -map 0:v -map 0:a -c copy output.mp4
This example stream copies (re-mux) with -c copy to avoid re-encoding.
I use exactly that command, but the flipping doesn't seem to work, like so:
ffprobe -hide_banner myfile.trimmed.mpg
[h264 # 000001b965b569c0] Increasing reorder buffer to 2
Input #0, mpeg, from 'myfile.trimmed.mpg':
Duration: 00:02:41.09, start: 0.500000, bitrate: 6255 kb/s
Stream #0:0[0x80]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream #0:1[0x1e2]: Video: h264 (High), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc
ffmpeg -hide_banner -i myfile.trimmed.mpg -map 0:v -map 0:a -c copy myfile.trimmed.flipped.mpg
[h264 # 000001fa0ee94680] Increasing reorder buffer to 2
Input #0, mpeg, from 'myfile.trimmed.mpg':
Duration: 00:02:41.09, start: 0.500000, bitrate: 6255 kb/s
Stream #0:0[0x80]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream #0:1[0x1e2]: Video: h264 (High), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc
[mpeg # 000001fa0ee88dc0] VBV buffer size not set, using default size of 230KB
If you want the mpeg file to be compliant to some specification
Like DVD, VCD or others, make sure you set the correct buffer size
[mpeg # 000001fa0ee88dc0] ac3 in MPEG-1 system streams is not widely supported, consider using the vob or the dvd muxer to force a MPEG-2 program stream.
Output #0, mpeg, to 'myfile.trimmed.flipped.mpg':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Video: h264 (High), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 59.94 fps, 59.94 tbr, 90k tbn, 59.94 tbc
Stream #0:1: Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream mapping:
Stream #0:1 -> #0:0 (copy)
Stream #0:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 9570 fps=0.0 q=-1.0 Lsize= 123008kB time=00:02:40.95 bitrate=6260.6kbits/s speed= 518x
video:114772kB audio:7545kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.565047%
ffprobe -hide_banner myfile.trimmed.flipped.mpg
[h264 # 0000021edcf36ac0] Increasing reorder buffer to 2
Input #0, mpeg, from 'myfile.trimmed.flipped.mpg':
Duration: 00:02:41.09, start: 0.500000, bitrate: 6255 kb/s
Stream #0:0[0x80]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream #0:1[0x1e2]: Video: h264 (High), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc
What, what?!
The command output looks like it did exactly what I asked, but the resulting file has the same stream ordering as the original file. What am I missing?
One possible clue: It looks like the audio stream starts before the video stream. The smallest pkt_pts_time I see in the audio stream is 00:00:00.500000, while the smallest I see in the video stream is 0:00:01.912967. Could that matter?
Tricky one this seemed at first. I wondered if this old FFmpeg trac ticket might hold the key:
There is no stream order in mpeg-ps.
what you see from ffmpeg output order is likely just if a audio or video packet comes first
But that's not actually the problem; however it is worth noting that your file has a .mpg extension, when you should be trying to output an MP4 or MKV. ".mpg" is only valid if it contains legacy MPEG-1 and MPEG-2 formats. H.264 or AAC elementary streams are invalid.
If you've not created this file yourself, it's either a mislabelled container (e.g. MKV or MP4), or someone has bizarrely muxed the streams to .mpg. Note how FFmpeg warns you of the incompatible codec during your stream reorder attempt.
MPEG-PS is a packetised format, so there's no elementary streams as such. If it's genuinely an MPEG-PS file, it may be that an audio sample appears first. Either way, you should abandon using .mpg for your formats.
See the end of this answer for how you can use FFprobe to fairly accurately identify the actual container format.
I had another think, and finally a neuron reminded me about how the -map output follows the order of assignment.
An important thing to note is that -map 0:v -map 0:a doesn't quite work how you might expect it with files containing more than one of a stream type, as that syntax matches all applicable streams of that type.
Gyan has clarified that if you have a file with exactly one video and one audio stream, -map 0:v -map 0:a will function equivalently to -map 0:1 -map 0:0.
If you want to use the 0:a syntax, if you have more than one audio for example you must address them individually, otherwise FFmpeg will group them when reordering. -map 0:a will move both audios; -map 0:a:0 will move just the first audio.
The alternative, remembering to always check stream order in every file you manipulate, is to specify absolute stream numbers in the order you wish to have them in the output. So, -map 0:1 -map 0:0 if your video is the second of two streams in the source file.
For files with one video and one audio stream, you can use either method.
Tests
I created an .MP4 file containing one H.264 video as stream 0:0 and one MP3 audio as stream 0:1.
Original file:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\temp\video-audio.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.78.100
Duration: 00:05:00.30, start: 0.000000, bitrate: 422 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 421 kb/s, 23.98 fps, 23.98 tbr, 11988 tbn, 47.95 tbc (default)
Metadata:
handler_name : GPAC ISO Video Handler
vendor_id : [0][0][0][0]
Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 180 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Then I fed it back to FFmpeg:
ffmpeg -i C:\temp\video-audio.mp4 -map 0:1 -map 0:0 -c copy C:\temp\swapped.mp4
(equivalent to -map 0:a -map 0:v in this case)
Result: swapped streams; MP3 audio stream is 0:0, H.264 video stream is 0:1
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\temp\swapped.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.78.100
Duration: 00:05:00.30, start: 0.000000, bitrate: 422 kb/s
Stream #0:0(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 180 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream #0:1(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 421 kb/s, 23.98 fps, 23.98 tbr, 11988 tbn, 47.95 tbc (default)
Metadata:
handler_name : GPAC ISO Video Handler
vendor_id : [0][0][0][0]
This appears to accomplish what you want :-)
ffmpeg -i INPUTFILE -map 0:1 -map 0:0 -c copy OUTPUTFILE.mp4 or
ffmpeg -i INPUTFILE -map 0:v -map 0:a -c copy OUTPUTFILE.mp4
Reminder:
For files with one video and one audio stream, you can use either method above.
For files with multiple audios or videos, determine the stream index and specify using the more granular 0:v:0, 0:a:0 syntax.
ffmpeg -i INPUTFILE shows a file's numeric stream IDs, and this may be quicker for the odd file. However, numeric references skips the sanity check of specifying only audio, video, subtitles etc. when reordering.
FFmpeg's Stream Specifiers and Advanced Options documentation is worth bookmarking.
Further reading:
FFmpeg Trac documentation for -map and their main documentation for -map.
This previous Q&A on Video.SE, which in turn references this answer which talks about the -disposition switch. NB that -disposition is used for indicating which streams the player 'should' consider as default (Section 5.4, "Main Options" in the FFmpeg docs), not doing any physical stream reordering.
(big thanks to Gyan for being so observant!)
Using FFprobe to identify container
FFprobe can do a best-effort detection of the real container format, ignoring the extension. Here's an example of my demo "swapped.mp4", renamed to .mpg:
ffprobe -hide_banner -show_error -show_format -i "C:\temp\swapped.mpg"
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\temp\swapped.mpg':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.78.100
Duration: 00:05:00.30, start: 0.000000, bitrate: 422 kb/s
Stream #0:0(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 180 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream #0:1(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 421 kb/s, 23.98 fps, 23.98 tbr, 11988 tbn, 47.95 tbc (default)
Metadata:
handler_name : GPAC ISO Video Handler
vendor_id : [0][0][0][0]
[FORMAT]
filename=C:\temp\swapped.mpg
nb_streams=2
nb_programs=0
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime / MOV
start_time=0.000000
duration=300.301000
size=15847817
bit_rate=422184
probe_score=100
TAG:major_brand=isom
TAG:minor_version=512
TAG:compatible_brands=isomiso2avc1mp41
TAG:encoder=Lavf58.78.100
[/FORMAT]
Note major_brand=isom (ISO Base Media file format), format_name, format_long_name and so on.
A true MPEG-2 video I produced last month (a DVD rip) outputs this:
ffprobe -hide_banner -show_error -show_format -i "C:\temp\opera.mpg"
Input #0, mpeg, from 'C:\temp\opera.mpg':
Duration: 02:15:23.71, start: 66240.530111, bitrate: 4194 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv, smpte170m, top first), 720x576 [SAR 64:45 DAR 16:9], 25 fps, 25 tbr, 90k tbn, 50 tbc
Side data:
cpb: bitrate max/min/avg: 7000000/0/0 buffer size: 1835008 vbv_delay: N/A
Stream #0:1[0x80]: Audio: ac3, 48000 Hz, stereo, fltp, 192 kb/s
[FORMAT]
filename=C:\temp\opera.mpg
nb_streams=2
nb_programs=0
format_name=mpeg
format_long_name=MPEG-PS (MPEG-2 Program Stream)
start_time=66240.530111
duration=8123.712000
size=4259503237
bit_rate=4194637
probe_score=26
[/FORMAT]
FFprobe correctly reports format_name as mpeg and format_long_name as MPEG-PS.
Everything Chris said was right, and a big help. The original .MPG with H264 video inside came from TivoDecoder; I never thought to question whether that container was valid for that video format, and when QT was willing to play it, I figured the ffprobe warning wasn't a big deal. :)
Remuxing to an MP4 container gave me something Windows will play.
ffmpeg -hide_banner -ss 00:17:24 -i myfile.mpg -t 00:02:40 -c copy myfile.trimmed.mp4
[h264 # 000002d5d73f4040] Increasing reorder buffer to 2
[mpegts # 000002d5d73edbc0] PES packet size mismatch
[mpegts # 000002d5d73edbc0] Packet corrupt (stream = 1, dts = 8467425232).
[mpegts # 000002d5d73edbc0] Could not find codec parameters for stream 2 (Unknown: none ([151][0][0][0] / 0x0097)): unknown codec
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[mpegts # 000002d5d73edbc0] PES packet size mismatch
[mpegts # 000002d5d73edbc0] Packet corrupt (stream = 1, dts = 8467425232).
Input #0, mpegts, from 'myfile.mpg':
Duration: 00:30:00.63, start: 92282.982578, bitrate: 6249 kb/s
Program 1
Stream #0:0[0x1aab]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc
Stream #0:1[0x1abf]: Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream #0:2[0x1ac1]: Unknown: none ([151][0][0][0] / 0x0097)
[mp4 # 000002d5d7e90540] track 1: codec frame size is not set
Output #0, mp4, to 'myfile.trimmed.mp4':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, progressive), 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 59.94 fps, 59.94 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 384 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 9570 fps=0.0 q=-1.0 Lsize= 122587kB time=00:02:39.99 bitrate=6276.6kbits/s speed= 462x
video:114772kB audio:7545kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.220873%
ffprobe -hide_banner -show_error -show_format -i myfile.trimmed.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'myfile.trimmed.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.45.100
Duration: 00:02:40.96, start: 0.000000, bitrate: 6239 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv), 1280x720 [SAR 1:1 DAR 16:9], Closed Captions, 5890 kb/s, 59.94 fps, 59.94 tbr, 90k tbn, 119.88 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 384 kb/s (default)
Metadata:
handler_name : SoundHandler
Side data:
audio service type: main
[FORMAT]
filename=myfile.trimmed.mp4
nb_streams=2
nb_programs=0
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime / MOV
start_time=0.000000
duration=160.960000
size=125529106
bit_rate=6239021
probe_score=100
TAG:major_brand=isom
TAG:minor_version=512
TAG:compatible_brands=isomiso2avc1mp41
TAG:encoder=Lavf58.45.100
[/FORMAT]
I've got a new problem with the video and audio being massively out of sync, but I'll ask that as a separate question.
It's very tricky but possible to reorder audio streams with ffmpeg tool.
If you know the source streams' numbers, or their ids just map them in order you want:
ffmpeg -i "..." -map 0:a:0,0:1 -map 0:a:1,0:1 ... or use -map i:0xbbbb -map i:0xaaaa ... i.e. the -map commands allows to select new order of your audio streams.
The more tricky is if you want to reorder them based on their language: imagine your source contains English, French and Spanish (and possible other) audio tracks, and you want to order them as French, English, Spanish, and any other languages following. So based on -map command you can do the following: ffmpeg -i "..." -map 0:V,0:0 -map 0:m:language:fre? 0:m:language:eng? 0:m:language:spa? -map 0:a? -map -0:s? .... a little explanation of parameters: first map real video stream (capital V) as target stream 0 (you can change this), then search for stream with metadata's language French and if it exists (the question mark at end) put as first audio stream, repeat the same with other requested languages, but before finish map any other(s) stream in case there is no any of your requested languages or won't loose some source audio stream, and finally (very important in some cases) delete mapping of subtitles because the metadata for subtitles is same as metadata for audio streams, and if there is subtitle stream for French but no audio stream for French the result will be subtitle stream mapped and no audio stream - this depends on your requirements of course.
Do not forget that using -map command prevent automatically mapping all stream(s) to target, that's why '-map 0:v' is placed in command, the same is true for data and and download streams too. The question marks at end of any audio streams selection allows absent of exact audio stream, so the ffmpeg command will not stops with error.
Note: it's possible mapping commands to duplicate your audio stream(s) in output...

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.

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