Monday 23 October 2017

video editing - ffmpeg's select-filter generates unwanted black frames


I want to cut a video by frames with ffmpeg. What I came up with so far is


ffmpeg -i P2.mp4 -vf "fps,setpts='PTS-STARTPTS',select='gte(n\,20)*lte(n\,30)',crop='600:430:100:650',scale='280:-1'" P2o.avi

The parameters are here for testing to see if everything performs as required.


What I get is a video with 20 black frames followed by 10 frames of content. While I want the 10 frames I don't know how the 20 black frames got there. The documentation of the select-filter clearly states that only the frames for which the expression evaluates to a value different to 0 are selected. In the above gte(n,20) should remove them.


If it helps, here the output of ffmpeg:


G:\>ffmpeg -i P2.mp4 -vf "fps,setpts='PTS-STARTPTS',select='gte(n\,20)*lte(n\,30)',crop='600:430:100:650',scale='280:-1'" P2o.avi
ffmpeg version N-50911-g9efcfbe Copyright (c) 2000-2013 the FFmpeg developers
built on Mar 13 2013 21:26:48 with gcc 4.7.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 19.100 / 52. 19.100
libavcodec 55. 0.100 / 55. 0.100
libavformat 55. 0.100 / 55. 0.100
libavdevice 54. 4.100 / 54. 4.100
libavfilter 3. 45.103 / 3. 45.103
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
[h264 @ 039a1ae0] mmco: unref short failure
Last message repeated 5 times
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'P2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.0.100
Duration: 01:51:58.78, start: 0.000000, bitrate: 8479 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080
[SAR 1:1 DAR 16:9], 8084 kb/s, 49.96 fps, 50 tbr, 90k tbn, 50 tbc
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, stereo, fltp, 384 kb/s
Metadata:
handler_name : SoundHandler
File 'P2o.avi' already exists. Overwrite ? [y/N] y
Invalid pixel aspect ratio 603/602, limit is 255/255 reducing
Output #0, avi, to 'P2o.avi':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
ISFT : Lavf55.0.100
Stream #0:0(und): Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 280x201 [SAR 1:1 DAR 280:201], q=2-31, 200 kb/s, SAR 603:602 DAR 60:43, 25 tbn, 25 tbc
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: mp3 (U[0][0][0] / 0x0055), 48000 Hz, stereo, fltp
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 -> mpeg4)
Stream #0:1 -> #0:1 (ac3 -> libmp3lame)
Press [q] to stop, [?] for help
[h264 @ 046f1680] mmco: unref short failure
Last message repeated 1 times
[h264 @ 05e4a900] mmco: unref short failure
Last message repeated 3 times
[swscaler @ 039b63e0] Warning: data is not aligned! This can lead to a speedloss

frame= 1 fps=0.0 q=4.2 size= 29kB time=00:00:01.82 bitrate= 131.8kbits/s
frame= 11 fps= 11 q=7.4 size= 71kB time=00:00:03.16 bitrate= 184.4kbits/s
frame= 11 fps=7.2 q=7.4 size= 71kB time=00:00:04.60 bitrate= 126.8kbits/s
frame= 11 fps=5.4 q=7.4 size= 71kB time=00:00:06.19 bitrate= 94.3kbits/s
frame= 11 fps=4.3 q=7.4 size= 71kB time=00:00:07.48 bitrate= 78.0kbits/s
frame= 11 fps=3.6 q=7.4 size= 71kB time=00:00:08.76 bitrate= 66.7kbits/s
frame= 11 fps=3.1 q=7.4 size= 71kB time=00:00:10.08 bitrate= 58.0kbits/s
frame= 11 fps=2.7 q=7.4 size= 71kB time=00:00:11.42 bitrate= 51.1kbits/s
frame= 11 fps=2.6 q=7.4 Lsize= 253kB time=00:00:12.04 bitrate= 171.8kbits/s
video:42kB audio:189kB subtitle:0 global headers:0kB muxing overhead 9.670474%

G:\>

Does anyone please have a suggestion how I can get rid of those 20 frames? I don't think I can use -ss here.



Answer



Try placing the setpts filter after the select filter.


setpts='PTS-STARTPTS' resets the presentation time stamp of each frame (so the first frame is 0.0 seconds, and they increment correctly) - the select filter will send the selected frames to the rest of ffmpeg, but it will not, by itself, alter the presentation time stamp, which can cause all sorts of problems - as you found out.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...