代码
import subprocess from datetime import timedelta def parse_time(time_str): """将时间字符串解析为秒""" # 将时间字符串分割为小时、分钟和秒 hours, minutes, seconds = map(int, time_str.split(':')) # 计算总秒数 return timedelta(hours=hours, minutes=minutes, seconds=seconds).total_seconds() def ffmpeg_trim(input_path, output_path, start_time, end_time): # 将时间字符串转换为秒 start_time_sec = parse_time(start_time) end_time_sec = parse_time(end_time) # 构建ffmpeg命令 cmd = [ 'ffmpeg', '-i', input_path, # 输入文件 '-ss', str(start_time_sec), # 开始时间 '-to', str(end_time_sec), # 结束时间 '-c', 'copy', # 复制编解码器设置,避免重新编码 output_path # 输出文件 ] # 执行ffmpeg命令 try: subprocess.run(cmd, check=True) except subprocess.CalledProcessError as e: print(f"An error occurred while processing the video: {e}") # 要裁剪的视频文件路径 video_path = r'E:\edge下载\80-\80-难度等级_keyong.mp4' # 裁剪后的视频文件路径 output_path = r'E:\edge下载\80-\80-难度等级_qutou.mp4' # 开始时间(HH:MM:SS格式) start_time = '00:00:08' # 视频的第8秒开始 # 结束时间(HH:MM:SS格式) end_time = '00:03:38' # 视频的第3分37秒结束 # 调用函数进行时间裁剪 ffmpeg_trim(video_path, output_path, start_time, end_time)
标签:视频,end,ffmpeg,裁切,output,python,start,time,path From: https://www.cnblogs.com/jingzaixin/p/18148170