python - 使用时间戳修剪一批视频
问题描述
我想根据存储在 csv 文件中的开始和结束时间在文件夹中剪切多个视频:annotations.csv
csv文件的格式为:
视频名 | 开始时间 | 结束时间
我认为伪代码可能看起来像这样:
`for i in video_names
do: cut video according to time frame`
ffmpeg -ss 00:01:00 -i input.mp4 -to 00:02:00 -c copy output.mp4
这是我在硬编码输入名称以及开始和结束时间时为单个视频找到的。如何使这个动态与我的注释文件相对应?
解决方案
弄清楚了。在下面的代码片段中,只需添加您的路径而不是“your_file_path/annotations.csv”
pip install moviepy
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
import csv
# name of output file
naming = 'trim_'
def get_sec(time_str):
"""Get Seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
sample = open('your_file_path/annotations.csv', 'r')
csv1 = csv.reader(sample,delimiter=',')
next(csv1, None)
for eachline in csv1:
file_name = str(eachline[0])
out_file = naming + file_name
start = eachline[1]
start = get_sec(start)
end = eachline[2]
end = get_sec(end)
ffmpeg_extract_subclip(file_name, start, end, targetname=out_file)
推荐阅读
- android - 设计/文本选项卡不仅针对可绘制资源文件显示
- excel - 从多列返回多个匹配值 - Excel 2016
- ios - 将应用程序上传到 iTunes 连接时出现问题
- ibm-watson - 去偏输出是否会自动替换我模型的输出?
- emacs - Emacs:如何更改 orgmode 日历语言?
- javascript - 等效于 ES6 模块的 AMD 的 require.toUrl
- es6-promise - 如何使用 async/await 而不是 promise (.then)
- php - 如何从数据库中获取新闻并将其打印在 PHP 页面上?
- latex - 带多列的乳胶表文本换行 - 保持间距的方法?
- ios - 升级到 Xcode 版本 10.2 (10E125) 后如何更正错误