Instantly share code, notes, and snippets.

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

This comment has been minimized.

ghost commented Jul 5, 2014

Great stuff!


This comment has been minimized.

Margaruga commented Jul 5, 2014

Amazing example of the Python's power.


This comment has been minimized.

kmonsoor commented Jul 6, 2014



This comment has been minimized.

rtaibah commented Jul 7, 2014

The next step is to take each peak and convert it to a GIF for social sharing.


This comment has been minimized.

paul8620 commented Jul 8, 2014

And automatically share:)


This comment has been minimized.

diegoaguilar commented Nov 6, 2014

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


This comment has been minimized.

lautarobock commented Jul 28, 2017

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

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