feat: 增加视频截帧功能,支持--video参数自动截帧+OCR
This commit is contained in:
@@ -216,6 +216,50 @@ class OCRProcessor:
|
||||
print(f"Merged text saved to {text_path}")
|
||||
|
||||
|
||||
def extract_frames(video_path: str, output_dir: str, fps: float = 1.0) -> str:
|
||||
"""
|
||||
使用 ffmpeg 从视频中截取帧图片
|
||||
|
||||
Args:
|
||||
video_path: 视频文件路径
|
||||
output_dir: 输出目录
|
||||
fps: 每秒截取帧数 (默认: 1)
|
||||
|
||||
Returns:
|
||||
帧图片目录路径
|
||||
"""
|
||||
import subprocess
|
||||
|
||||
output_dir = Path(output_dir)
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# ffmpeg 命令: -i 输入 -vf fps=fps 输出格式
|
||||
cmd = [
|
||||
"ffmpeg", "-y",
|
||||
"-i", video_path,
|
||||
"-vf", f"fps={fps}",
|
||||
"-q:v", "2", # 质量等级 (2=高质量)
|
||||
str(output_dir / "%04d.jpg")
|
||||
]
|
||||
|
||||
print(f"🔄 截取视频帧: {video_path}")
|
||||
print(f" 输出目录: {output_dir}")
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if result.returncode == 0:
|
||||
# 统计截取的帧数
|
||||
frame_count = len(list(output_dir.glob("*.jpg")))
|
||||
print(f"✅ 截取完成: {frame_count} 帧")
|
||||
return str(output_dir)
|
||||
else:
|
||||
print(f"❌ ffmpeg 错误: {result.stderr}")
|
||||
return ""
|
||||
except FileNotFoundError:
|
||||
print("❌ 错误: 未找到 ffmpeg,请先安装: apt install ffmpeg")
|
||||
return ""
|
||||
|
||||
|
||||
def main():
|
||||
"""命令行入口"""
|
||||
parser = argparse.ArgumentParser(
|
||||
@@ -225,35 +269,56 @@ def main():
|
||||
示例:
|
||||
# 使用 EasyOCR 处理帧目录
|
||||
python ocr_processor.py --input ./frames/ --output ./ocr.json
|
||||
|
||||
|
||||
# 直接从视频截帧并 OCR
|
||||
python ocr_processor.py --video ./video.flv --output ./ocr.json
|
||||
|
||||
# 使用 Tesseract 处理
|
||||
python ocr_processor.py --input ./frames/ --output ./ocr.json --engine tesseract
|
||||
|
||||
# 指定采样帧率
|
||||
python ocr_processor.py --input ./frames/ --output ./ocr.json --fps 0.5
|
||||
|
||||
|
||||
# 指定截帧帧率
|
||||
python ocr_processor.py --video ./video.flv --output ./ocr.json --fps 2
|
||||
|
||||
# 同时生成合并的文本文件
|
||||
python ocr_processor.py --input ./frames/ --output ./ocr.json --merge
|
||||
"""
|
||||
)
|
||||
|
||||
parser.add_argument("--input", "-i", required=True, help="输入帧图片目录")
|
||||
|
||||
# 输入源:帧目录 或 视频文件(二选一)
|
||||
input_group = parser.add_mutually_exclusive_group(required=True)
|
||||
input_group.add_argument("--input", "-i", help="输入帧图片目录")
|
||||
input_group.add_argument("--video", "-v", help="输入视频文件(会自动截帧)")
|
||||
|
||||
parser.add_argument("--output", "-o", required=True, help="输出 JSON 文件路径")
|
||||
parser.add_argument("--fps", "-f", type=float, default=0.1, help="采样帧率 (默认: 0.1)")
|
||||
parser.add_argument("--fps", "-f", type=float, default=1.0, help="截帧帧率 (默认: 1帧/秒)")
|
||||
parser.add_argument("--engine", "-e", choices=["easyocr", "tesseract"],
|
||||
default="easyocr", help="OCR 引擎 (默认: easyocr)")
|
||||
parser.add_argument("--merge", action="store_true", help="同时保存合并的文本文件")
|
||||
parser.add_argument("--languages", "-l", nargs="+",
|
||||
default=["ch_sim", "en"], help="语言列表")
|
||||
parser.add_argument("--progress", "-p", help="可选:进度文件路径(会实时更新)")
|
||||
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
# 如果指定了视频文件,先截帧
|
||||
input_dir = args.input
|
||||
if args.video:
|
||||
if not os.path.exists(args.video):
|
||||
print(f"❌ 视频文件不存在: {args.video}")
|
||||
return 1
|
||||
|
||||
# 创建临时帧目录
|
||||
frames_dir = Path(args.video).stem + "_frames"
|
||||
input_dir = extract_frames(args.video, frames_dir, fps=args.fps)
|
||||
|
||||
if not input_dir:
|
||||
return 1
|
||||
|
||||
# 初始化处理器
|
||||
processor = OCRProcessor(engine=args.engine, languages=args.languages)
|
||||
|
||||
|
||||
# 处理帧
|
||||
results = processor.process_frames(args.input, fps=args.fps, progress_file=args.progress)
|
||||
results = processor.process_frames(input_dir, fps=args.fps, progress_file=args.progress)
|
||||
|
||||
if results:
|
||||
# 保存结果
|
||||
|
||||
Reference in New Issue
Block a user