下面是我使用的,只需要几秒钟就可以运行:
ffmpeg -i input.mp4 -ss 01:19:27 -to 02:18:51 -c:v copy -c:a copy output.mp4
参考:使用FFmpeg由Alexander Refsum Jensenius修剪视频文件。
生成的mp4文件也可以在iMovie中使用。有关使用get_duration(input_video)模型获取完整持续时间的更多信息。
如果你想连接多个过场动画,你可以使用以下Python脚本:
#!/usr/bin/env python3
import subprocess
def get_duration(input_video):
cmd = ["ffprobe", "-i", input_video, "-show_entries", "format=duration",
"-v", "quiet", "-sexagesimal", "-of", "csv=p=0"]
return subprocess.check_output(cmd).decode("utf-8").strip()
def main():
name = "input.mkv"
times = []
times.append(["00:00:00", "00:00:10"])
times.append(["00:06:00", "00:07:00"])
# times = [["00:00:00", get_duration(name)]]
if len(times) == 1:
time = times[0]
cmd = ["ffmpeg", "-i", name, "-ss", time[0], "-to", time[1], "-c:v", "copy", "-c:a", "copy", "output.mp4"]
subprocess.check_output(cmd)
else:
open('concatenate.txt', 'w').close()
for idx, time in enumerate(times):
output_filename = f"output{idx}.mp4"
cmd = ["ffmpeg", "-i", name, "-ss", time[0], "-to", time[1], "-c:v", "copy", "-c:a", "copy", output_filename]
subprocess.check_output(cmd)
with open("concatenate.txt", "a") as myfile:
myfile.write(f"file {output_filename}\n")
cmd = ["ffmpeg", "-f", "concat", "-i", "concatenate.txt", "-c", "copy", "output.mp4"]
output = subprocess.check_output(cmd).decode("utf-8").strip()
print(output)
if __name__ == "__main__":
main()
示例脚本将在00:00:00 - 00:00:10和00:06:00 - 00:07:00之间剪切和合并场景。
如果你想剪切完整的视频(如果你想将mkv格式转换为mp4格式),只需取消下面这一行的注释:
# times = [["00:00:00", get_duration(name)]]