【问题标题】:How to create video clips using a data frame of file names, start and end times?如何使用文件名、开始和结束时间的数据框创建视频剪辑?
【发布时间】:2022-01-09 10:57:30
【问题描述】:

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

我有一个包含文件路径、标识符以及开始和停止时间的数据框。我正在尝试遍历此数据帧的每一行并使用 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 并附加 File_path 的最后 18 个左右字符的输出文件和Behavior

这是我尝试过的:

#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 以“非零退出状态”返回,所以它确实运行了,但不喜欢这个参数......

标签: python video ffmpeg


【解决方案1】:

我将专注于您可能最麻烦的部分:视频处理。

使用 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 质量没有损失的数据。只是提一下,以防它很重要。

【讨论】:

  • 我认为我不需要为此使用子流程。
  • 你不这么认为吗?那你会用什么?比简单的 ffmpeg 执行更复杂的东西?
  • 我最后的编辑是我想要做的。我希望它不需要那么复杂。
  • 我的回答显示了如何将 ffmpeg 作为子进程调用。我没有看到问题。
  • 我不知道为什么我需要为此使用子进程?以及它将如何与 for 循环交互。
【解决方案2】:
#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,它会起作用。

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 2013-12-16
    • 2021-07-26
    • 2021-03-04
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2018-11-03
    • 2020-09-21
    相关资源
    最近更新 更多