Skip to content

Instantly share code, notes, and snippets.

Embed
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:
# http://zulko.github.io/blog/2014/07/04/automatic-soccer-highlights-compilations-with-python/
#
# 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: 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))]
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)]
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('soccer_cuts.mp4') # low quality is the default
@ghost

This comment has been minimized.

Copy link

commented Jul 5, 2014

Great stuff!

@Margaruga

This comment has been minimized.

Copy link

commented Jul 5, 2014

Amazing example of the Python's power.

@kmonsoor

This comment has been minimized.

Copy link

commented Jul 6, 2014

awesome

@rtaibah

This comment has been minimized.

Copy link

commented Jul 7, 2014

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

@paul8620

This comment has been minimized.

Copy link

commented Jul 8, 2014

And automatically share:)

@diegoaguilar

This comment has been minimized.

Copy link

commented Nov 6, 2014

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

@lautarobock

This comment has been minimized.

Copy link

commented Jul 28, 2017

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

@jackson-sandland

This comment has been minimized.

Copy link

commented May 3, 2019

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.

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.