Skip to content

Instantly share code, notes, and snippets.

Last active February 25, 2023 06:23
  • Star 64 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A python script to automatically summarize soccer videos based on the crowd's reactions
# This Python script makes a summary of a football game by cutting
# the video around the 10 % loudest moments, which generally
# include the goals and other important events.
# For more details, see this blog post:
# 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
clip = VideoFileClip("soccer_game.mp4")
cut = lambda 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(]
averaged_volumes = np.array([sum(volumes[i:i+10])/10
for i in range(len(volumes)-10)])
increases = np.diff(averaged_volumes)[:-1]>=0
decreases = np.diff(averaged_volumes)[1:]<=0
peaks_times = (increases * decreases).nonzero()[0]
peaks_vols = averaged_volumes[peaks_times]
peaks_times = peaks_times[peaks_vols>np.percentile(peaks_vols,90)]
for t in peaks_times:
if (t - final_times[-1]) < 60:
if averaged_volumes[t] > averaged_volumes[final_times[-1]]:
final_times[-1] = t
final = concatenate([clip.subclip(max(t-5,0),min(t+5, clip.duration))
for t in final_times])
final.to_videofile('soccer_cuts.mp4') # low quality is the default
Copy link

paul8620 commented Jul 8, 2014

And automatically share:)

Copy link

What about mkv files? I just tried with a mkv and couldn't run it

Copy link

Hi, Im not familiar with python, what version I need? thanks

Copy link

Incredible. How do you plot the volume using matlibplot? I've got a video with lots of dead air and I'd like to cut the dead air while preserving the audio around the times the speaker is speaking.

Copy link

spolo96 commented Jan 8, 2022

@paul8620 I'm currently working on that at the moment 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment