Skip to content

Instantly share code, notes, and snippets.

@sinkers
Created June 17, 2014 06:14
Show Gist options
  • Save sinkers/766f86755ad7b36e60e1 to your computer and use it in GitHub Desktop.
Save sinkers/766f86755ad7b36e60e1 to your computer and use it in GitHub Desktop.
Check keyframe alignment with ffprobe
#!/usr/bin/python
'''
This simple script allows you to input a file where you want to evaluate where the keyframes are
You simply input an infile that you want to evaluate
Where you expect the keyframes e.g. every 2 seconds
and the framerate e.g. 25 fps
It then scans the file and reports on where it expects keyframes and where they actually are
'''
import os
import re
import sys
import subprocess
import logging
import math
logger = logging.getLogger(__name__)
#infile = sys.argv[1]
#infile= "/tmp/ffmpeg_main_profile/out_gop12.ts"
infile = "/Users/andrew/Desktop/demo/vbvtests/bc_hls_1024x576.ts"
#target_dir = "/tmp/ffmpeg_main_profile/"
def get_keyframes(infile, keyframe_int, fps):
# Should be able to return when keyframes are expected which is every x seconds
# this would mean we should have an iframe every x * fps
# Specific value for ffprobe output
lines_past_type = 20
line_count = 0
print_line = False
frame = 0
found_iframe = False
output_str = ""
next_keyframe = math.ceil(float(fps) * keyframe_int)
key_framedist = next_keyframe
cmd = "ffprobe -show_frames %s" % infile
logger.info(cmd)
# -print_format json
process = subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line = process.stdout.readline()
if line != '':
#the real code does filtering here
#print line.rstrip()
if (line.rstrip()=="media_type=video"):
print_line = True
if (print_line and (line_count < lines_past_type)):
#print line.rstrip()
if re.match(r'pict_type=I.*',line):
found_iframe = True
#print line.rstrip()
match = re.match(r'coded_picture_number=(.*)',line)
if (match):
frame = match.group(1)
if (found_iframe):
output_str += "%s," % frame
found_iframe = False
if (int(frame)>=next_keyframe):
if (int(frame)==next_keyframe):
print "Found I frame as expected at %s" % next_keyframe
else:
print "Expected I frame at %s found at %s" % (next_keyframe, frame)
next_keyframe+=key_framedist
line_count+=1
if (line_count >= lines_past_type):
print_line = False
line_count = 0
else:
break
return output_str
print get_keyframes(infile, 2, 25)
'''
[FRAME]
media_type=video
key_frame=1
pkt_pts=2499750
pkt_pts_time=27.775000
pkt_dts=2499750
pkt_dts_time=27.775000
pkt_duration=3750
pkt_duration_time=0.041667
pkt_pos=2905164
width=1280
height=720
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=I
coded_picture_number=633
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
reference=3
[/FRAME]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment