ffmpeg - duration usage in input text file - ffmpeg

I am trying to use ffmpeg to concatenate video segments with some black screen. To do that I've first generated a blank 10-second video (no audio track) with:
$ ffmpeg -f lavfi -i color=black:s=320x240:r=1 -f lavfi -i anullsrc -t 10 -vcodec libvpx -an blank.mkv
I then created the simplest possible scenario within input.txt file (contents below) in order to have three seconds of black screen followed by some video (no audio track):
file 'blank.mkv'
duration 3
file 'video_example.mkv'
And, finally, ran the following ffmpeg command to concat the contents of that input file:
$ ffmpeg -f concat -i input.txt -codec:v copy -codec:a copy output.mkv
The issue that I have is that the duration 3 is not considered, so the final video still has ten seconds of black frames (instead of three) followed by my video. And also "Non-monotonous DTS in output stream 0:0 ..." message is shown when using duration x in the file. If I remove duration the warnings are gone and getting the 10-second black screen first output as well.
Full output of the ffmpeg concat command:
$ ffmpeg -hide_banner -f concat -i input.txt -codec:v copy -codec:a copy output.mkv
Input #0, concat, from 'input.txt':
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: vp8, yuv420p(progressive), 320x240, SAR 1:1 DAR 4:3, 1 fps, 1 tbr, 1k tbn, 1k tbc
Metadata:
ENCODER : Lavc57.107.100 libvpx
DURATION : 00:00:10.000000000
File 'output.mkv' already exists. Overwrite ? [y/N] y
Output #0, matroska, to 'output.mkv':
Metadata:
encoder : Lavf57.83.100
Stream #0:0: Video: vp8 (VP80 / 0x30385056), yuv420p(progressive), 320x240 [SAR 1:1 DAR 4:3], q=2-31, 1 fps, 1 tbr, 1k tbn, 1k tbc
Metadata:
ENCODER : Lavc57.107.100 libvpx
DURATION : 00:00:10.000000000
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[concat # 000000000031a440] DTS 3000 < 9000 out of order
[matroska # 0000000000328420] Non-monotonous DTS in output stream 0:0; previous: 9000, current: 3000; changing to 9000. This may result in incorrect timestamps in the output file.
[matroska # 0000000000328420] Non-monotonous DTS in output stream 0:0; previous: 9000, current: 4001; changing to 9000. This may result in incorrect timestamps in the output file.
[matroska # 0000000000328420] Non-monotonous DTS in output stream 0:0; previous: 9000, current: 4998; changing to 9000. This may result in incorrect timestamps in the output file.
[matroska # 0000000000328420] Non-monotonous DTS in output stream 0:0; previous: 9000, current: 6004; changing to 9000. This may result in incorrect timestamps in the output file.
[matroska # 0000000000328420] Non-monotonous DTS in output stream 0:0; previous: 9000, current: 7002; changing to 9000. This may result in incorrect timestamps in the output file.
[matroska # 0000000000328420] Non-monotonous DTS in output stream 0:0; previous: 9000, current: 8005; changing to 9000. This may result in incorrect timestamps in the output file.
frame= 5794 fps=0.0 q=-1.0 Lsize= 7109kB time=01:37:09.70 bitrate= 10.0kbits/s speed=5.16e+004x
video:7043kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.926229%
Any idea what am I doing wrong? The warning seems to hint towards the issue here.
Other possibly useful info:
$ ffprobe -hide_banner blank.mkv
Input #0, matroska,webm, from 'blank.mkv':
Metadata:
ENCODER : Lavf57.83.100
Duration: 00:00:10.00, start: 0.000000, bitrate: 1 kb/s
Stream #0:0: Video: vp8, yuv420p(progressive), 320x240, SAR 1:1 DAR 4:3, 1 fps, 1 tbr, 1k tbn, 1k tbc (default)
Metadata:
ENCODER : Lavc57.107.100 libvpx
DURATION : 00:00:10.000000000
$ ffprobe -hide_banner video_example.mkv
Input #0, matroska,webm, from 'video_example.mkv':
Metadata:
encoder : GStreamer matroskamux version 1.8.1.1
creation_time : 2018-05-04T17:57:04.000000Z
Duration: 01:37:08.70, start: 15434.269000, bitrate: 9 kb/s
Stream #0:0(eng): Video: vp8, yuv420p(progressive), 320x240, SAR 1:1 DAR 4:3, 1 fps, 1 tbr, 1k tbn, 1k tbc (default)
Metadata:
title : Video
$ ffmpeg -v
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7.3.0 (GCC)

For video and audio files, inpoint/outpoint have to be used.
file 'blank.mkv'
outpoint 3
file 'video_example.mkv'
duration is useful for single images, like when making a slideshow, or raw audio/video streams where ffmpeg can't reliably discover the media duration.

Related

Split a live video stream from a webcam with ffmpeg [duplicate]

I need to split a large video file into multiple pieces quickly and without files with errors. The basic idea is, I have a 2GB video file which I want to change to multiple formats. I have read some encoders can not make use of more than 1 core when encoding to a different format. So I had the idea to split the large file (which is very quick) into 4/8 pieces depending on how many cores I have available on individual servers. re-encode each piece to a new format and use these to display video in sequence.
so
testfile.mp4
becomes
testfile_piece_1.mp4
testfile_piece_2.mp4
testfile_piece_3.mp4
testfile_piece_4.mp4
these can then be individually be converted.
The solution should not be format specific. However I have found issues with mp4 files.
I have tried the command below, which works really well and fast but creates files with errors.
ffmpeg -i testfile.mp4 -ss 00:00:00 -t 00:20:00 -c copy testfile_piece_1.mp4
When I play the testfile_piece_1.mp4 on VLC it works fine. An issue arises when converting the split file to a different height and width mp4 file. I would get an error similar to "moov atom not found"
I tried adding -movflags faststart with no luck
I then came across this library https://code.google.com/archive/p/moovrelocator/ which fixed the moov issue but I would then get an error with regards to aac "Error while opening encoder for output stream #0.0 - maybe incorrect parameters such as bit_rate, rate, width or height"
The other way of splitting the files is useless but involved re-encoding the file. not too bad for smaller file size but the 2GB file would probably take days to complete.
Is there a way to split the largr file quickly without producing files with errors? I have been working on it for days with no luck.
Console output for comment - FFmpeg splitting large files
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/usr/share/nginx/html/uploads/testfile01.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.36.100
Duration: 00:05:02.08, start: 302.120000, bitrate: 3254 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709) , 1920x1080 [SAR 1:1 DAR 16:9], 3252 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (de fault)
Metadata:
handler_name : VideoHandler
[libx264 # 0x165ffc0] width not divisible by 2 (853x480)
Output #0, mp4, to '/usr/share/nginx/html/uploads/testfile01_480.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.36.100
Stream #0:0(und): Video: h264, none, q=2-31, 128 kb/s, SAR 2560:2559 DAR 0:0 , 25 fps (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc56.41.100 libx264
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
console output 2
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/usr/share/nginx/html/uploads/testfile.mp4':
Metadata:
major_brand : dash
minor_version : 0
compatible_brands: iso6avc1mp41
creation_time : 2016-01-24 04:26:37
Duration: 01:15:58.08, start: 0.000000, bitrate: 3163 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 3161 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default)
Metadata:
creation_time : 2016-01-24 04:26:37
handler_name : VideoHandler
[segment # 0x1197060] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
Output #0, segment, to '/usr/share/nginx/html/uploads/testfile%02d.mp4':
Metadata:
major_brand : dash
minor_version : 0
compatible_brands: iso6avc1mp41
encoder : Lavf56.36.100
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 3161 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
Metadata:
creation_time : 2016-01-24 04:26:37
handler_name : VideoHandler
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
Use the segment muxer to break the input into segments:
ffmpeg -i testfile.mp4 -c copy -f segment -segment_time 1200 testfile_piece_%02d.mp4
This will split the source at keyframes, so segments may not be exactly 1200 seconds long. And the timestamps aren't reset, so some players will fail to play the 2nd and latter segments. If playability is needed, insert -reset_timestamps 1.
After the parallel encoding, you can stitch the generated segments by first creating a text file seg.txt like this
file 'encoded_testfile_piece_00.mp4'
file 'encoded_testfile_piece_01.mp4'
file 'encoded_testfile_piece_02.mp4'
file 'encoded_testfile_piece_03.mp4'
And then running
ffmpeg -f concat -i seg.txt -c copy -fflags +genpts encoded_full.mp4

Extract frames from video with ffmpeg - header problem?

I want to convert .AVI files coming from a camera-trap to individuals frames, ideally using ffmpeg. Up to now I am not succeeding.
The most simple thing I tried, to try to locate the problem, is this (I want all frames and my test file is test.avi):
ffmpeg -i test.avi output_%04d.png
It fails with the following console message:
[avi # 0x559fb596f8c0] unknown stream type 73647578
[avi # 0x559fb596f8c0] Something went wrong during header parsing, tag [0][0]id has size 338702712, I will ignore it and try to continue anyway.
[mjpeg # 0x559fb59709e0] No JPEG data found in image
Last message repeated 100 times
[avi # 0x559fb596f8c0] decoding for stream 0 failed
[avi # 0x559fb596f8c0] Could not find codec parameters for stream 0 (Video: mjpeg (MJPG / 0x47504A4D), none(bt470bg/unknown/unknown), 1280x720): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
[avi # 0x559fb596f8c0] Could not find codec parameters for stream 1 (Video: none (JUNK / 0x4B4E554A), none, 11025x22050): unknown codec
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, avi, from 'test.avi':
Duration: 00:00:10.50, start: 0.000000, bitrate: 28129 kb/s
Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), none(bt470bg/unknown/unknown), 1280x720, 20.01 fps, 20.01 tbr, 20.01 tbn, 20.01 tbc
Stream #0:1: Video: none (JUNK / 0x4B4E554A), none, 11025x22050, 11025 tbr, 11025 tbn, 11025 tbc
Stream mapping:
Stream #0:1 -> #0:0 (? (?) -> png (native))
Decoder (codec none) not found for input stream #0:1
The actual video duration is 10s (when read using vlc for instance, and this is indeed the length of video programmed on the camera-trap. ffmpeg says the duration is 10.50s, and says there is a problem with header parsing (see above).
I have no idea how to sort this, despite having looked at more than many ffmpeg 'convert video to frames' post. Any hint would be appreciated, thanks.
If you look at the error message, you will see that the file has 2 video streams.
Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), none(bt470bg/unknown/unknown), 1280x720, 20.01 fps, 20.01 tbr, 20.01 tbn, 20.01 tbc
Stream #0:1: Video: none (JUNK / 0x4B4E554A), none, 11025x22050, 11025 tbr, 11025 tbn, 11025 tbc
and ffmpeg is trying to read for the second one:
Stream mapping:
Stream #0:1 -> #0:0 (? (?) -> png (native))
You can use -map 0:0 to select the first stream.

Issue with output RTSP stream converted using ffmpeg filter-complex

I have a camera feed which I am getting in RTSP for example: rtsp://172.16.1.177:8554/test
here are the stream details I got using ffmpeg -i rtsp://172.16.1.177:8554/test
Input #0, rtsp, from 'rtsp://172.16.1.177:8554/test':
Metadata:
title : Session streamed with GStreamer
comment : rtsp-server
Duration: N/A, start: 0.710544, bitrate: N/A
Stream #0:0: Video: h264 (Constrained Baseline), yuv420p(progressive), 1920x1080, 15 tbr, 90k tbn, 180k tbc
Now, I am applying chromakey to the above stream which is giving me perfect output in mp4
ffmpeg -i background.jpg -i rtsp://172.16.1.177:8554/test -filter_complex "[1:v]colorkey=0x26ff0b:0.3:0.2[ckout];[0:v][ckout]overlay[out]" -map "[out]" output.mp4
After that, I've created and successfully started ffserver using the below config file
HTTPPort 8091
RTSPPort 8092
HTTPBindAddress 0.0.0.0
<Feed feed1.ffm>
File /tmp/feed1.ffm
FileMaxSize 2048M
ACL allow localhost
</Feed>
<Stream live.sdp>
Feed feed1.ffm
Format rtp
NoAudio
VideoCodec libx264
VideoFrameRate 15
VideoBitRate 1000
VideoSize 1920x1080
ACL allow 172.16.1.30 172.16.0.2
</Stream>
I am trying to export output stream using below command
ffmpeg -i background.jpg -i rtsp://172.16.1.177:8554/test -filter_complex "[1:v]colorkey=0x26ff0b:0.3:0.2[ckout];[0:v][ckout]overlay[out]" -map "[out]" http://localhost:8091/feed1.ffm
which gives me below error
Input #0, image2, from 'background.jpg':
Duration: 00:00:00.04, start: 0.000000, bitrate: 23866 kb/s
Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 854x480 [SAR 72:72 DAR 427:240], 25 tbr, 25 tbn, 25 tbc
Input #1, rtsp, from 'rtsp://172.16.1.177:8554/test':
Metadata:
title : Session streamed with GStreamer
comment : rtsp-server
Duration: N/A, start: 0.711933, bitrate: N/A
Stream #1:0: Video: h264 (Constrained Baseline), yuv420p(progressive), 1920x1080, 15 tbr, 90k tbn, 180k tbc
[tcp # 0x7fdb88706680] Connection to tcp://localhost:8091 failed (Connection refused), trying next address
[tcp # 0x7fdb88402920] Connection to tcp://localhost:8091 failed (Connection refused), trying next address
Filter overlay has an unconnected output
I don't have much experience with either ffmpeg or ffserver, so I don't exactly know why there is the issue with unconnected output
Filter overlay has an unconnected output

How to generate an RTMP test stream using ffmpeg command?

I would like to test my streaming infrastructure by generating an RTMP test video with a timestamp. This could look like that screen. The image doesn't matter. I'm after the working stream generated on-the-fly and timestamp only. I intend to use the ffmpeg tool for that purpose. The command could look something like
$ ffmpeg -i image.png \
-vf drawtext="fontfile=/Library/Fonts/Arial.ttf: \
timecode='00\:00\:00\:00': r=1: fontcolor=white: \
fontsize=24: box=1: boxcolor=black#0.5: \
boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2" \
-f flv rtmp://localhost/live/test
I do run locally a streaming server based on NGINX and its RTMP module.
However, the above command gives me the following error:
Input #0, png_pipe, from 'image.png':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: png, rgb24(pc), 768x576 [SAR 7874:7874 DAR 4:3], 25 tbr, 25 tbn, 25 tbc
Stream mapping:
Stream #0:0 -> #0:0 (png (native) -> flv1 (flv))
Press [q] to stop, [?] for help
[Parsed_drawtext_0 # 0x7fb78450ece0] Using non-standard frame rate 1/1
Output #0, flv, to 'rtmp://localhost/live/test':
Metadata:
encoder : Lavf57.71.100
Stream #0:0: Video: flv1 (flv) ([2][0][0][0] / 0x0002), yuv420p, 768x576 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 25 fps, 1k tbn, 25 tbc
Metadata:
encoder : Lavc57.89.100 flv
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
[flv # 0x7fb785812a00] Failed to update header with correct duration.
[flv # 0x7fb785812a00] Failed to update header with correct filesize.
frame= 1 fps=0.0 q=8.6 Lsize= 50kB time=00:00:00.00 bitrate=406016.0kbits/s speed=0.019x
video:49kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.451271%
The streaming server operates as expected. The problem is with the command. Would anyone be able to help me?
ffmpeg has testsrc you can use as a test source input stream:
ffmpeg -r 30 -f lavfi -i testsrc -vf scale=1280:960 -vcodec libx264 -profile:v baseline -pix_fmt yuv420p -f flv rtmp://localhost/live/test
-r, scaling, profile, etc are just an example and can be ommited/played with. The point is using -i testsrc

Increase the bitrate tolerance of ffmpeg for creating screenshots of a movie

I'm getting the error bitrate tolerance too small for bitrate so far no problem. I know that there are several switches to increase that but nothing works.
ffmpeg -y -r 1/30 -b:v 999999k -bt 999999k -maxrate 999999k -i in.flv out%03d.jpg
The source of that commandline is directly from ffmpeg. But that crashes:
ffmpeg version N-44123-g5d55830 Copyright (c) 2000-2012 the FFmpeg developers
built on Sep 2 2012 20:23:29 with gcc 4.7.1 (GCC)
[...]
Input #0, flv, from 'in.flv':
Duration: 00:05:00.13, start: 0.000000, bitrate: 259 kb/s
Stream #0:0: Video: flv1, yuv420p, 320x240, 1k tbr, 1k tbn, 1k tbc
Stream #0:1: Audio: nellymoser, 22050 Hz, mono, s16
[mjpeg # 04356860] bitrate tolerance too small for bitrate
[mjpeg # 04317540] ff_frame_thread_encoder_init failed
Output #0, image2, to 'out%03d.jpg':
Stream #0:0: Video: mjpeg, yuvj420p, 320x240, q=2-31, 200 kb/s, 90k tbn, 0.03 tbc
Stream mapping:
Stream #0:0 -> #0:0 (flv -> mjpeg)
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Some ideas what I'm doing wrong?

Resources