Problem: I want to trim a video file based on start-time and end-time from json data. There are multiple start & end times and the video needs to be trimmed and then the final parts appended.
I tried using MoviePy by assigning the unix timestamps to a var and then converting to MM:SS using datetime.
How can i do this for multiple values as such the script goes thru all the dicts and trims the video as per the data?
Is MoviePY good enough for this job or is there any other efficient lib / way out there?
What i tried:
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)
Sample Json Data:
[
{
"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
}
]
Update: I tried doing something else, i am getting there but i need help in mass conversion of these timestamps as moviepy accepts only HH:MM:SS and not unixtime, and with creating subclips on basis of this.
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)
Here is how i fixed this, I hope it helps someone out there someday :)
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")