从 json 使用 unixtime 从视频创建多个子剪辑?
Creating multiple subclips from a video with unixtime from a json?
问题:
我想 trim 基于 json 数据的开始时间和结束时间的视频文件。有多个开始和结束时间,视频需要 trimmed 然后附加最后部分。
我尝试通过将 unix 时间戳分配给 var 然后使用日期时间转换为 MM:SS 来使用 MoviePy。
我如何为多个值执行此操作,因为脚本遍历所有命令并根据数据 trim 播放视频?
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)
这是我解决这个问题的方法,我希望有一天它能帮助那里的人:)
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")
问题: 我想 trim 基于 json 数据的开始时间和结束时间的视频文件。有多个开始和结束时间,视频需要 trimmed 然后附加最后部分。
我尝试通过将 unix 时间戳分配给 var 然后使用日期时间转换为 MM:SS 来使用 MoviePy。
我如何为多个值执行此操作,因为脚本遍历所有命令并根据数据 trim 播放视频?
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)
这是我解决这个问题的方法,我希望有一天它能帮助那里的人:)
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")