Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Found all videos from directories, then add opening and ending, then remove silent parts.
#!/usr/bin/python
#
# This Python script adds opening and ending on Roleplaying game videos then it cuts silent parts.
# Silent parts lasts 1 seconds at least of full silence.
# For more details, see this blog post:
# http://blog.rolisteam.org
#
# LICENCE: Creative Commons 0 - Public Domain
# I, the author of this script, wave any rights and place this work in the public domain.
#
import numpy as np # for numerical operations
from moviepy.editor import VideoFileClip, concatenate_videoclips
import os
import sys
opening = VideoFileClip("/home/renaud/documents/jdr/03_videos/L5ROpening.mp4")
ending = VideoFileClip("/home/renaud/documents/jdr/03_videos/EndingTsuruchiLess.mp4")
rootFolder = "/home/renaud/documents/jdr/03_videos/L5R"
for subfolder in sorted(os.listdir(rootFolder)):
if("_done" not in subfolder):
for subfile in os.listdir(os.path.join(rootFolder,subfolder)):
if(("mp4" in subfile)and("Partie" in subfile)):
link = os.path.join(rootFolder,subfolder)
video = os.path.join(link,subfile)
dest = subfile.replace(".mp4","_ending.mp4",1)
destination = os.path.join(link,dest)
videoclip = VideoFileClip(video)
#concatenation of opening, video and ending
finalclip = concatenate_videoclips([opening,videoclip,ending])
finalclip.write_videofile(destination,fps=25)
#split resulting video in audio subclip
clip = VideoFileClip(destination)
cut = lambda i: clip.audio.subclip(i,i+1).to_soundarray(fps=22000)
volume = lambda array: np.sqrt(((1.0*array)**2).mean())
volumes = [volume(cut(i)) for i in range(0,int(clip.audio.duration-2))]
final_times= []
i = 1
duo = []
start = -1
end = 0
sumVideo = 0
#identify all part with sounds. What we keep.
for vol in volumes:
if(( vol == 0.0 )and (start!=-1)):
end = i-1
duo = [start,end]
final_times.append(duo)
sumVideo += (end-start)
start = -1
if((start == -1)and (vol>0.0)):
start=i
i=i+1
finalpath = destination.replace("_ending","_cutted")
print finalpath
print final_times
#concatenate all kept parts.
final = concatenate_videoclips([clip.subclip(t[0],t[1])
for t in final_times])
#write the file
final.write_videofile(finalpath,fps=25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.