现在人工智能AI这么方便,这种需求可以考虑使用AI来提供脚本处理。
[Python] 纯文本查看 复制代码 import subprocess
def get_video_duration(video_file):
# 获取视频时长
command = ['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=duration', '-of', 'default=noprint_wrappers=1:nokey=1', video_file]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if process.returncode != 0:
raise Exception(f"Error occurred: {error.decode('utf-8')}")
duration = output.decode('utf-8').strip()
return duration
def split_video(video_file, output_prefix):
# 获取视频时长
duration = get_video_duration(video_file)
print(f"Video duration: {duration}")
# 分割视频
parts = []
current_time = 0
while current_time < float(duration):
part_duration = 3600 # 每个部分时长为1小时(3600秒)
if current_time + part_duration > float(duration):
part_duration = int(float(duration) - current_time)
start_time = current_time
end_time = start_time + part_duration
part_name = f"{output_prefix}_{start_time:02d}_{end_time:02d}.mp4"
command = ['ffmpeg', '-i', video_file, '-ss', f"{start_time}s", '-t', f"{part_duration}s", part_name]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
if process.returncode != 0:
raise Exception(f"Error occurred: {error.decode('utf-8')}")
parts.append(part_name)
current_time += part_duration
return parts
if __name__ == '__main__':
video_file = 'input.mp4' # 替换为你的视频文件路径
output_prefix = 'output_' # 输出文件的前缀名
try:
parts = split_video(video_file, output_prefix)
print("Video parts:", parts)
except Exception as e:
print("Error:", e) |