Files
akmon/video_splitter/processor.py
2026-01-20 08:04:15 +08:00

59 lines
1.9 KiB
Python

import ffmpeg
import os
import sys
class VideoProcessor:
def __init__(self):
pass
def get_video_info(self, input_path: str):
try:
probe = ffmpeg.probe(input_path)
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
return video_stream
except ffmpeg.Error as e:
print(f"Error probing video: {e.stderr}", file=sys.stderr)
return None
def split_video(self, input_path: str, start_time: str, end_time: str, output_path: str):
"""
使用 FFmpeg 切割视频
Args:
input_path: 输入视频路径
start_time: 开始时间 (HH:MM:SS)
end_time: 结束时间 (HH:MM:SS)
output_path: 输出文件路径
"""
print(f"[*] 正在切割视频: {start_time} - {end_time} -> {output_path}")
try:
(
ffmpeg
.input(input_path, ss=start_time, to=end_time)
.output(output_path, c='copy') # Stream copy for speed and quality
.overwrite_output()
.run(capture_stdout=True, capture_stderr=True)
)
print(f"[+] 切割成功: {output_path}")
return True
except ffmpeg.Error as e:
print(f"[-] 切割失败: {e.stderr.decode('utf8')}", file=sys.stderr)
return False
def extract_audio(self, input_path: str, output_path: str):
"""
提取音频用于 ASR
"""
try:
(
ffmpeg
.input(input_path)
.output(output_path, ac=1, ar=16000) # Mono, 16k for ASR
.overwrite_output()
.run(quiet=True)
)
return True
except ffmpeg.Error as e:
print(f"[-] 音频提取失败: {e.stderr.decode('utf8')}")
return False