5

I scan input video to extract certain frames using ffmpeg's select filter. The selection is based on a complex creteria and the number of extracted frames can't be predicted (I'm doing scene detection, but this can be something different - e.g. selecting all the I frames, etc).

What I need is to display the percentage of scanned (decoded) video (e.g. 10% or 90%).


I tried several ways to do this parsing console output as people usually do when dealing with encoding, but it doesn't help with the progress of scanning (e.g. Can ffmpeg show a progress bar? or ffmpeg Progress Bar - Encoding Percentage in PHP)

ffmpeg -progress sceneProgr.txt -i input.wmv -vsync passthrough -an -vf select='gt(scene\,0.2)',showinfo scene%%05d.png

The output this produces is as follows:

<..>    
frame=    0 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A    
n:0 pts:16550 pts_time:16.55 pos:4205325 fmt:rgb24 sar:0/1 s:640x480 i:P iskey:1 type:I checksum:95895BC9 plane_checksum:[95895BC9]
frame=    1 fps=0.7 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A    
n:1 pts:24591 pts_time:24.591 pos:6685325 fmt:rgb24 sar:0/1 s:640x480 i:P iskey:0 type:P checksum:FF4CC015 plane_checksum:[FF4CC015]
'frame=...' and 'n:...' lines are repeated for each of the extracted frames.

Both frame=... and n:... lines refer to the numbers in the output and therefore can't be used to calculate progress the way people usually do this (as I can't predict how many frames will be found beforehead and besides, they are not uniformly spread across the input video).


If I specify -progress progress.txt parameter, the progress.txt file is as follows:

frame=5
fps=1.2
stream_0_0_q=0.0
total_size=N/A
out_time_ms=43209833
out_time=00:00:43.209833
dup_frames=0
drop_frames=0
progress=continue


frame=6
fps=1.3
stream_0_0_q=0.0
total_size=N/A
out_time_ms=52252200
out_time=00:00:52.252200
dup_frames=0
drop_frames=0
progress=continue

frame=6
fps=1.2
stream_0_0_q=0.0
total_size=N/A
out_time_ms=52252200
out_time=00:00:52.252200
dup_frames=0
drop_frames=0
progress=continue

New portion is written approximately every second and refers to the last extracted frame. Which is somewhat helpful as out_time refers to the last extracted frames' position in the input video, so I can calculate the progress of the scan from it as

progress = out_time_ms/total_input_time 

But this is not ideal as it will be updated only when the new frame which matches the select criteria is extracted. So, If I have a large portion of video with no matching frames, the progress won't change for a lot of time.


Wrapping-up:

I am looking for a way to calculate the progress of video scanning when using select filter.

Any ideas are strongly appreciated.

Community
  • 1
  • 1
Isantipov
  • 19,491
  • 2
  • 26
  • 41
  • try using stats that report what has NOT been process yet to get at percent complete. each frame has a timestamp. on init , you have var for total duration. those 2 vars always provide measure of progress – Robert Rowntree Apr 04 '13 at 14:33
  • @RobertRowntree , thanks for taking a look: how do I get this "tats that report what has NOT been process yet" which you refer to? – Isantipov Apr 04 '13 at 15:52
  • the stdout info on ffmpeg for input media tracks at BEGIN includes 'duration' or the total length in seconds.... can you READ this or get it somehow? Then , use that along with the timestamp on every frame to figure out percent complete . – Robert Rowntree Apr 04 '13 at 15:56
  • @RobertRowntree , I can get the total length easily, my problem is that I can't get the **currently decoded/scanned** frame number as ffmpeg outputs the frame stat only if it has passed through the *select* condition (see the first example output in my question). – Isantipov Apr 04 '13 at 16:00

1 Answers1

0

what you get on every frame like data from ffprobe.....

[FRAME]
media_type=video
key_frame=1
pkt_pts=0
pkt_pts_time=0.000000
pkt_dts=0
pkt_dts_time=0.000000
pkt_duration=1
pkt_duration_time=0.040000
pkt_pos=44
width=800
height=542
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=I
coded_picture_number=0
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
reference=3
[/FRAME]

what you get at BEGIN

I/stderr  ( 7434): Input #0, image2, from '/storage/sdcard0/Pictures/me374658335.jpg':
I/stderr  ( 7434):   Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
I/stderr  ( 7434):     Stream #0:0: Video: mjpeg, yuvj420p, 1296x972 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 25 tbn, 25 tbc
I/stderr  ( 7434): Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/sdcard0/PictureComment/1347139284144.3gp':
I/stderr  ( 7434):   Metadata:
I/stderr  ( 7434):     major_brand     : 3gp4
I/stderr  ( 7434):     minor_version   : 0
I/stderr  ( 7434):     compatible_brands: isom3gp4
I/stderr  ( 7434):     creation_time   : 2012-09-08 21:21:41
I/stderr  ( 7434):   Duration: 00:00:17.22, start: 0.000000, bitrate: 67 kb/s
I/stderr  ( 7434):     Stream #1:0(eng): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, s16, 64 kb/s
I/stderr  ( 7434):     Metadata:
I/stderr  ( 7434):       creation_time   : 2012-09-08 21:21:41
I/stderr  ( 7434):       handler_name    : SoundHandle
I/stderr  ( 7434): [libx264 @ 0x5a070910] using SAR=1/1
I/stderr  ( 7434): [libx264 @ 0x5a070910] using cpu capabilities: ARMv6 NEON
I/stderr  ( 7434): [libx264 @ 0x5a070910] profile High, level 3.2

can you use the frame timestamp along with the Duration to calculate percent complete?

Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
  • Robert, thank you for your input. Unfortunately, I can't get how I can run ffprobe to execute the filters I need (**vf select='gt(scene\,0.2)',showinfo**) - is it possible? I am also a bit confused with your "BEGIN" - is it a command or a way to execute a command? – Isantipov Apr 04 '13 at 21:47
  • To answer your question: I can easily calculate the progreess from the **currently decoded** frame timestamp if I could only somehow make ffmpeg output this. (instead it prints **currently encoded** frame). I.e. I have a long video (say, **10000000 frames**) with only **1 or 2 frames** in the end which meet the filter creteria - so ffmpeg outputs only 2 lines for these 2 frames when it finds them - keeping scanning progress unknown till then. – Isantipov Apr 04 '13 at 21:49
  • i dont know the interfaces that your code is working with. I only forwarded examples of data available via other interfaces related to CLI execution of ffmpeg, ffprobe, etc. I cant point you to a method/function on the interface you are working with. – Robert Rowntree Apr 05 '13 at 00:07
  • Hi Robert, thank you for your help. Sorry if it is not clear from the question:I am executing ffmpeg from the command-line in the following way: **ffmpeg -progress sceneProgr.txt -i input.wmv -vsync passthrough -an -vf select='gt(scene\,0.2)',showinfo scene%%05d.png** my problem is that stats printed into **stder** describe the **output frames** (those, selected by **select** filter and saved to disk) whereas I need to know the number of the frame **currently being extracted from the decoder**(before passing to the select filter) to calculate which portion of the video has been analysed so far – Isantipov Apr 05 '13 at 09:01
  • http://dranger.com/ffmpeg/tutorial05.html u may want to look at this. In require using the c interface for 'read-frame' but it does describe programming code around the packet time stamp. – Robert Rowntree Apr 05 '13 at 15:42
  • somewhere under libavcodec, observe the PTS for each packet and somehow make available higher up the stack , a sampled, every nth value of the TS to use for calc PERCENT COMPLETE. – Robert Rowntree Apr 05 '13 at 15:48
  • Robert, thank you very much for your help! I do understand your idea, but unfortunatelly,I can't afford using ffmpeg library directly now as the executing application is developed in .NET, which makes it quite hard and error-prone to use native libraries. I will continue looking for a solution using console execution. I have tried to add the second **showinfo** filter before the **select** one - it looks like I can distinguish messages from the 2 **showinfo** filters I have now (one before **select** and the one after it).I am in the process of confirming if it is reliably distinguisshed now. – Isantipov Apr 05 '13 at 18:12