使用时间戳修剪一批视频

2024-03-28 21:10:29 发布

您现在位置: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

这是我在硬编码输入名称以及开始和结束时间时为单个视频找到的。 如何使此动态文件与注释文件相对应


Tags: 文件csvto代码in文件夹名称for
1条回答
网友
1楼 · 发布于 2024-03-28 21:10:29

我明白了。在下面的代码片段中,只需添加路径而不是“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)

相关问题 更多 >