这是我使用的,只需几秒钟即可运行:
ffmpeg -i input.mp4 -ss 01:19:27 -to 02:18:51 -c:v copy -c:a copy output.mp4
参考:Alexander Refsum Jensenius使用 FFmpeg 修剪视频文件。
生成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)]]