kf

来自Tsetien’s Wiki
跳转到导航 跳转到搜索

To get timestamps of keyframes (I-frames) from given video files. This of course will need ffmpeg to be properly put in a folder in %PATH%.

kf.bat
@echo off
chcp 65001 >nul
if "%~1"=="" exit /b
for %%a in (%*) do (ffprobe -loglevel error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=print_section=0 "%%~fa" | findstr /ic:",K" > "%%~dpakf-%%~na.txt" && python C:\_c\kf.py "%%~dpakf-%%~na.txt")
kf.py
import sys, os, tempfile

def fmt(sec):
	sec=float(sec)
	if sec<60:
		return ('{:.3f}'.format(sec)).rstrip('0').rstrip('.')
	h=int(sec//3600)
	m=int((sec%3600)//60)
	s=sec-h*3600-m*60
	ss=('{:06.3f}'.format(s)).rstrip('0').rstrip('.')
	if h==0:
		return f'{m:02d}:{ss}'
	return f'{h}:{m:02d}:{ss}'

def convert(text):
	out=[]
	for line in text.splitlines():
		if not line.strip():
			continue
		part=line.strip().split(',',1)[0]
		try:
			out.append(fmt(part))
		except:
			continue
	return '\n'.join(out)+'\n' if out else ''

def rewrite(path):
	with open(path,'r',encoding='utf-8',errors='ignore') as f:
		data=f.read()
	out=convert(data)
	dirname=os.path.dirname(path) or '.'
	with tempfile.NamedTemporaryFile('w',encoding='utf-8',delete=False,dir=dirname) as tmp:
		tmp.write(out)
		tmp_path=tmp.name
	os.replace(tmp_path,path)

def main():
	if len(sys.argv)<2:
		sys.exit(1)
	for p in sys.argv[1:]:
		rewrite(p)

if __name__=='__main__':
	main()

And of course, the translated bash script, translated by ChatGPT 5.1—

kf
#!/bin/bash
[ -z "$1" ] && exit 0
for a in "$@"; do ffprobe -loglevel error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=print_section=0 "$a" | grep -i ',K' > "$(dirname "$a")/kf-$(basename "$a" .${a##*.}).txt" && python3 /usr/local/bin/kf.py "$(dirname "$a")/kf-$(basename "$a" .${a##*.}).txt"; done