Last active
March 21, 2019 14:47
-
-
Save amad/33079c97d42be9e4b58f06907052f265 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.signal import find_peaks | |
from moviepy.editor import VideoFileClip, concatenate | |
import matplotlib.pyplot as plt | |
import sys | |
import argparse | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-v", "--video", help="path to the video file") | |
args = vars(ap.parse_args()) | |
if args.get("video", None) is None: | |
raise Exception('--video is required') | |
clip = VideoFileClip(args["video"]) | |
cut = lambda i: clip.audio.subclip(i,i+1).to_soundarray(fps=22) | |
volume = lambda array: np.sqrt(((1.0*array)**2).mean()) | |
volumes = [volume(cut(i)) for i in range(0,int(clip.duration-1))] | |
duration = 10 | |
averaged_volumes = np.array([sum(volumes[i:i+duration])/duration for i in range(len(volumes))]) | |
peaks_times, _ = find_peaks(averaged_volumes, distance=20) | |
final_times=[peaks_times[0]] | |
for t in peaks_times: | |
if (t - final_times[-1]) < 60: | |
if averaged_volumes[t] > averaged_volumes[final_times[-1]]: | |
final_times[-1] = t | |
else: | |
final_times.append(t) | |
final = concatenate([clip.subclip(max(t-5,0),min(t+5, clip.duration)) | |
for t in final_times]) | |
final.to_videofile('cuts.mp4') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment