1 (edited by Selur 2015-01-09 09:35:52)

Topic: Naming the extracted key frames according to their position in video

Hello Everyone.

I have used the following command line to extract I frames from video

ffmpeg -i input.flv -f image2 -vf "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr thumb%04d.png

But this above command names the extracted frames as thumb0001.png, thumb0002, thumb0003.png and so on.

I want the extracted frames to be named according to the position of the I frame. If the first frame is I frame then it should be named as thumb0001.png, if eighth frame is an I frame then it should be named as thumb0008.png and so on...

I have read about about tag 'n', but didn't work .
Please guide me in writing the correct command line to name the I frames according to their position.
Thanks in advance..

---
edit by Selur: added code-tags

2

Re: Naming the extracted key frames according to their position in video

Sadly I don't think what you want is possible, since:
a. ffmpeg only 'recently' added support for addressing frames by frame number and not by time code
b. ffmpeg doesn't really have much options for the output file name generation
c. http://ffmpeg.org/pipermail/ffmpeg-user … 2837.htmll is exactly what you want, but looking at the corresponding ticked in the bug tracker (https://trac.ffmpeg.org/ticket/1452), it seems like the idea was abandoned and nobody is working on it.

---------------

Not exactly what you want, but what could be done is:

A. Extract the frames and their positions using for example:

ffmpeg -i "F:\TestClips&Co\test.avi" -f image2 -vsync vfr -vf "[in]select='eq(pict_type,PICT_TYPE_I)',showinfo[out]" h:\Temp\thumb%04d.png 2> "h:\Temp\positions.txt"

B. parse the positions.txt, which should look similar to:

ffmpeg version git-2015-01-06-3ebd76a Copyright (c) 2000-2015 the FFmpeg developers
  built on Jan  6 2015 06:49:38 with gcc 4.9.2 (Rev2, Built by MSYS2 project)
  configuration: --arch=x86_64 --disable-debug --disable-shared --disable-doc --disable-w32threads --enable-gpl --enable-version3 --enable-runtime-cpudetect --enable-avfilter --enable-bzlib --enable-zlib --enable-decklink --enable-librtmp --enable-gnutls --enable-avisynth --enable-frei0r --enable-filter=frei0r --enable-libbluray --enable-libcaca --enable-libopenjpeg --enable-fontconfig --enable-libfreetype --enable-libass --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-libschroedinger --enable-libsoxr --enable-libtwolame --enable-libspeex --enable-libtheora --enable-libutvideo --enable-libvorbis --enable-libvo-aacenc --enable-libopus --enable-libvidstab --enable-libvpx --enable-libwavpack --enable-libxavs --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzvbi
  libavutil      54. 16.100 / 54. 16.100
  libavcodec     56. 19.100 / 56. 19.100
  libavformat    56. 18.100 / 56. 18.100
  libavdevice    56.  3.100 / 56.  3.100
  libavfilter     5.  6.100 /  5.  6.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, avi, from 'F:\TestClips&Co\test.avi':
  Metadata:
    ISGN            : Drama
    IRTD            : Unrated
    title           : Encoding TestClip
    artist          : AutorTest
    album           : Movie
    date            : Heute
    genre           : Documentary
    ISBJ            : downfall of Hitler 
    copyright       : CopyrightTest
    IKEY            : Hitler
    comment         : Kleiner Testclip den ich zum Testen verwende
    encoder         : VirtualDub build 29393/releaseNandub v1.0rc2
  Duration: 00:00:17.16, start: 0.000000, bitrate: 860 kb/s
    Stream #0:0: Video: mpeg4 (Advanced Simple Profile) (XVID / 0x44495658), yuv420p, 640x352 [SAR 1:1 DAR 20:11], 723 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc
    Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 48000 Hz, stereo, s16p, 123 kb/s
Output #0, image2, to 'h:\Temp\thumb%04d.png':
  Metadata:
    ISGN            : Drama
    IRTD            : Unrated
    title           : Encoding TestClip
    artist          : AutorTest
    album           : Movie
    date            : Heute
    genre           : Documentary
    ISBJ            : downfall of Hitler 
    copyright       : CopyrightTest
    IKEY            : Hitler
    comment         : Kleiner Testclip den ich zum Testen verwende
    encoder         : Lavf56.18.100
    Stream #0:0: Video: png, rgb24, 640x352 [SAR 1:1 DAR 20:11], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
    Metadata:
      encoder         : Lavc56.19.100 png
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg4 (native) -> png (native))
Press [q] to stop, [?] for help
[Parsed_showinfo_1 @ 000000000522b220] n:0 pts:1 pts_time:0.04 pos:17964 fmt:yuv420p sar:1/1 s:640x352 i:P iskey:1 type:I checksum:9C9C7346 plane_checksum:[FC0C56E3 CA79F450 DF592804] mean:[53 123 128 ] stdev:[29.1 3.4 2.1 ]
[Parsed_showinfo_1 @ 000000000522b220] n:1 pts:46 pts_time:1.84 pos:343292 fmt:yuv420p sar:1/1 s:640x352 i:P iskey:1 type:I checksum:8500ADB7 plane_checksum:[1DDB2EE1 10FA4027 26273EAF] mean:[69 122 127 ] stdev:[36.7 5.3 4.2 ]
[Parsed_showinfo_1 @ 000000000522b220] n:2 pts:296 pts_time:11.84 pos:1313548 fmt:yuv420p sar:1/1 s:640x352 i:P iskey:1 type:I checksum:BCE2AE84 plane_checksum:[999C1BAD D11CF63F A56A9C89] mean:[63 123 128 ] stdev:[24.6 2.4 3.2 ]
frame=    3 fps=0.0 q=0.0 Lsize=N/A time=00:00:11.88 bitrate=N/A    
video:541kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

The interesting lines are the '[Parsed_showinfo_1 ..]'-lines, in example:

[Parsed_showinfo_1 @ 000000000522b220] n:0 pts:1 pts_time:0.04 pos:17964 fmt:yuv420p sar:1/1 s:640x352 i:P iskey:1 type:I checksum:9C9C7346 plane_checksum:[FC0C56E3 CA79F450 DF592804] mean:[53 123 128 ] stdev:[29.1 3.4 2.1 ]
...
[Parsed_showinfo_1 @ 000000000522b220] n:2 pts:296 pts_time:11.84 pos:1313548 fmt:yuv420p sar:1/1 s:640x352 i:P iskey:1 type:I checksum:BCE2AE84 plane_checksum:[999C1BAD D11CF63F A56A9C89] mean:[63 123 128 ] stdev:[24.6 2.4 3.2 ]
frame=    3 fps=0.0 q=0.0 Lsize=N/A time=00:00:11.88 bitrate=N/A    

now looking at 'n:0' and 'pts_time:0.04' you know that:
a. image 0001 (1 = 0 + 1) is shown at 0.04 seconds
b. image 0003 (3 = 2 + 1) is shown at 11.84 seconds
C. based on those relations one could rename the files
this would probably require a batch file or a small program

Note: You could also use ffindex to create file listing the key frame numbers using:

ffindex -k input.flv h:\

The content of the created '_track00.kf.txt' file should look similar to:

# keyframe format v1
fps 0
0
45
295

which would tell you that:
0001.png should be remamed to 1.png
0002.png should be remamed to 46.png
0003.png should be remamed to 296.png

again a custom program or script would be required.


I know those are probably not the solutions you were looking for. smile

Cu Selur

Ps.: This might also be interesting.

3

Re: Naming the extracted key frames according to their position in video

Thank you so much for your time Sir...
I already have the position of the I frames from was unable to get it as the filename.....
I'll use your suggestions.....
Thanks once again...

4

Re: Naming the extracted key frames according to their position in video

I have learn't from your suggestions that its not possible till now.
But can you suggest me any other tool in java or any other language using which the above problem can be solved??
Awaiting your response...

5

Re: Naming the extracted key frames according to their position in video

Sorry, don't know of a finished tool which does what you want.

6 (edited by Selur 2015-01-11 15:25:32)

Re: Naming the extracted key frames according to their position in video

As per your suggestions I ran the command

ffmpeg -i "cut.mp4" -f image2 -vsync vfr -vf "[in]select='eq(pict_type,PICT_TYPE_I)',showinfo[out]" iframes/thumb%04d.png 2> "positions.txt"

But it should have output the exact locations of the I frames..but I got this..

ffmpeg version N-68756-g627f565 Copyright (c) 2000-2014 the FFmpeg developers
  built on Dec 27 2014 22:04:14 with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --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-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      54. 15.100 / 54. 15.100
  libavcodec     56. 19.100 / 56. 19.100
  libavformat    56. 16.102 / 56. 16.102
  libavdevice    56.  3.100 / 56.  3.100
  libavfilter     5.  6.100 /  5.  6.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'cut.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.16.102
  Duration: 00:00:10.01, start: 0.000000, bitrate: 673 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360, 571 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Output #0, image2, to 'if\thumb%04d.png':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.16.102
    Stream #0:0(und): Video: png, rgb24, 640x360, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc56.19.100 png
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> png (native))
Press [q] to stop, [?] for help
[Parsed_showinfo_1 @ 02a82b80] n:0 pts:0 pts_time:0 pos:48 fmt:yuv420p sar:0/1 s:640x360 i:P iskey:1 type:I checksum:C25C5336 plane_checksum:[D2E3ECE7 07333D72 162128CE] mean:[105 125 136 ] stdev:[64.7 9.3 11.9 ]
[Parsed_showinfo_1 @ 02a82b80] n:1 pts:30720 pts_time:2.4 pos:245515 fmt:yuv420p sar:0/1 s:640x360 i:P iskey:1 type:I checksum:ADF38A99 plane_checksum:[8654EE8F E02B11B4 38658A47] mean:[100 126 131 ] stdev:[59.8 11.3 9.2 ] 
frame=    2 fps=0.0 q=0.0 size=N/A time=00:00:00.04 bitrate=N/A    

[Parsed_showinfo_1 @ 02a82b80] n:2 pts:61440 pts_time:4.8 pos:452626 fmt:yuv420p sar:0/1 s:640x360 i:P iskey:1 type:I checksum:E83D9EE6 plane_checksum:[E4A42060 532A2D6F B46D5117] mean:[100 129 137 ] stdev:[63.9 9.8 16.3 ]
[Parsed_showinfo_1 @ 02a82b80] n:3 pts:92160 pts_time:7.2 pos:642222 fmt:yuv420p sar:0/1 s:640x360 i:P iskey:1 type:I checksum:4B7458B9 plane_checksum:[9AA66B29 FEB0AC2D 2A754154] mean:[93 124 135 ] stdev:[61.4 13.4 9.6 ]
[Parsed_showinfo_1 @ 02a82b80] n:4 pts:121856 pts_time:9.52 pos:797619 fmt:yuv420p sar:0/1 s:640x360 i:P iskey:1 type:I checksum:BEF4EC30 plane_checksum:[B6EC333C E5EBE8F0 732ACFF5] mean:[95 126 131 ] stdev:[55.1 7.4 8.6 ]
frame=    5 fps=0.0 q=0.0 Lsize=N/A time=00:00:09.56 bitrate=N/A    
video:1360kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Here am getting n=1,2,3,4...sequence numbers..instead of their exact locations...what might have gone wrong..?

---
by Selur: added code-tags

7

Re: Naming the extracted key frames according to their position in video

You misunderstood me.
'n:X' indicates which I frame is referenced there
and
'pts_time' indicates at which time this frame is shown.
if you want the frame position instead use 'ffindex' like I suggested.

8

Re: Naming the extracted key frames according to their position in video

For this command how to know the frame rate at which the I/P frames were extracted?

ffmpeg -i cut.mp4 -f image2 -vf "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr thumb%04d.png

Running this command for I frame I got 5 I frames
Running this command for P frame I got 245 frames.

9

Re: Naming the extracted key frames according to their position in video

If your input is vfr, you can't from this output. (Since the frame rate is changing.)
The average frame rate is shown inside the video stream infos:

Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360, 571 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)

but for vfr you can't calculate the absolute frame position from the presentation time stamp.

Running this command for I frame I got 5 I frames
Running this command for P frame I got 245 frames.

It's normal that there are way more P than I frames in non-intra-only encoded content.

Cu Selur