2

问题:我想根据 json 数据的开始时间和结束时间修剪视频文件。有多个开始和结束时间,需要修剪视频,然后附加最后的部分。

我尝试通过将 unix 时间戳分配给 var 然后使用 datetime 转换为 MM:SS 来使用 MoviePy。

我如何为多个值执行此操作,因为脚本会遍历所有字典并根据数据修剪视频?

MoviePY 是否足以胜任这项工作,还是有其他有效的库/出路?

我尝试了什么:

from moviepy.editor import *
import datetime


start_time = int(datetime.datetime.fromtimestamp(int("1456229360")).strftime('%S'))
end_time =  int(datetime.datetime.fromtimestamp(int("1456229334")).strftime('%S'))
print start_time
print end_time


my_clip = VideoFileClip("sample.mp4")

#Trims the video as per the processed timestamps
clip1 = my_clip.subclip(start_time,end_time) #and select the subclip 00:00:00 - 00:00:00

#Append the trimmed parts.

#final_video = concatenate([clip1,clip2,clip3]) #How to do this ?

processed_vid = clip1.write_videofile("final_sample.mp4")

with open('output.json', 'r') as f:
    timestamps = json.load(f)

示例 Json 数据:

[
    {
        "accl": 1.5899999999999999, 
        "duration": 19, 
        "end_time": 1434367730, 
        "start_time": 1434367711
    }, 
    {
        "accl": 0.7670000000000012, 
        "duration": 14, 
        "end_time": 1434368618, 
        "start_time": 1434368604
    }, 
    {
        "accl": 0.7129999999999992, 
        "duration": 11, 
        "end_time": 1434368692, 
        "start_time": 1434368681
    }, 
    {
        "accl": 0.5970000000000013, 
        "duration": 13, 
        "end_time": 1434367605, 
        "start_time": 1434367592
    }
]

更新:我尝试做其他事情,我到了那里,但我需要帮助大规模转换这些时间戳,因为moviepy 只接受 HH:MM:SS 而不是 unixtime,并在此基础上创建子剪辑。

from moviepy.editor import *
import datetime
import json

clips_array = []

video= VideoFileClip('sample.mov')


with open('output.json', 'r') as f:
    timestamps = json.load(f)
    for timestamps in f:
        clip = full_vid.subclip(start_time, end_time)
        clips_array.append(clip)
4

1 回答 1

1

这是我解决这个问题的方法,我希望有一天它可以帮助那里的人:)

with open('output.json', 'r') as f:
    timestamps = json.load(f) 
for i in timestamps:
    starttime = int(datetime.datetime.fromtimestamp(i["start_time"]).strftime('%S')) 
    endtime = int(datetime.datetime.fromtimestamp(i["end_time"]).strftime('%S'))
    clip = video.subclip(starttime, endtime)
    clips_array.append(clip)
final_clip = concatenate_videoclips(clips_array)
final_clip.write_videofile("Out_File.mp4")
于 2016-03-10T17:59:48.663 回答