如何使用文件名、开始和结束时间的数据框创建视频剪辑?

How to create video clips using a data frame of file names, start and end times?

这个问题已经过编辑,以简化我在此处用户的帮助下开发的具体问题。 (感谢)

我有一个包含文件路径、标识符以及开始和停止时间的数据框。我正在尝试遍历此数据帧的每一行并使用 ffmpeg 创建视频剪辑。

数据框是这样的:

Time    File_path   Behavior    Status  experiment_id   Start_time  Stop_time   Duration
130 314.287 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_05   312.787 315.787 3.0
145 784.158 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_05   782.658 785.658 3.0
140 708.906 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_05   707.406 710.406 3.0
33  507.600 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_02   506.100 509.100 3.0
75  112.690 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_03   111.190 114.190 3.0
133 344.057 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_05   342.557 345.557 3.0
26  366.215 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_02   364.715 367.715 3.0
70  84.448  H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_03   82.948  85.948  3.0
113 738.897 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_03   737.397 740.397 3.0
128 200.548 H:/My Drive/Katz-Lab_Otter-Data/Projects/Forag...   Scrunch POINT   K8_feed_111120_05   199.048 202.048 3.0

目标是使用 File_path 指定的视频制作一个使用指定时间戳的视频剪辑,并创建一个使用指定变量 ouput_path 并附加最后 18 File_pathBehavior.

左右字符

这是我尝试过的:

#loop through the lines of the data frame
for row in sampled_data.index:
    
    #print(sampled_data["File_path"][row]) 
    
    output_file = ("%(path)s\%(experiment_id)s_%(behavior)s_%(index)s.MP4" 
                   % {"path" : output_path, 
                      "experiment_id" : sampled_data["experiment_id"][row], 
                      "behavior" : sampled_data["Behavior"][row],
                      "index" : row})
    
    #print(output_file)
    
    subprocess.check_call([
        'ffmpeg',
        '-ss', str(sampled_data["Start_time"][row]),
        '-i', str(sampled_data["File_path"][row]),
        '-t', str(sampled_data["Duration"][row]),
        '-c', 'copy', 
        output_file])

编辑:我成功了!

我将重点介绍您可能遇到最多麻烦的部分:视频操作。

使用 Python 的 subprocess 模块和 ffmpeg 的 运行 子进程可能是最简单的。

import subprocess

subprocess.check_call([
    'ffmpeg',
    '-ss', str(starttime),
    '-i', inputfilepath,
    '-t', str(duration),
    '-c', 'copy', # alternatively -c:v mjpeg -q:v 2
    outputfilepath])

你可以告诉 ffmpeg 到 start decoding the input at a specific time (-ss {timecode} before/after -i {the input file}),并从视频中读取特定的时间量 (-t {duration})。剩下的只是编码器参数。

如果您的输入视频是仅帧内编码的,您甚至可以-c copy数据质量没有损失。只是提一下,以防它很重要。

#loop through the lines of the data frame
for row in sampled_data.index:
    
    #print(sampled_data["File_path"][row]) 
    
    output_file = ("%(path)s\%(experiment_id)s_%(behavior)s_%(index)s.MP4" 
                   % {"path" : output_path, 
                      "experiment_id" : sampled_data["experiment_id"][row], 
                      "behavior" : sampled_data["Behavior"][row],
                      "index" : row})
    
    #print(output_file)
    
    subprocess.check_call([
        'ffmpeg',
        '-ss', str(sampled_data["Start_time"][row]),
        '-i', str(sampled_data["File_path"][row]),
        '-t', str(sampled_data["Duration"][row]),
        '-c', 'copy', 
        output_file])

如果文件名中没有空格,此方法有效!此外,为了指定输出路径,如果您完全创建路径和文件名然后传递给 ffmpeg,它就可以工作。