Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Torvaney committed Aug 25, 2016
0 parents commit 319c6f0
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore contents of video dir (due to size)
video/*.mp4
video/*.wav

# PyCharm hidden folders/files
.idea
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Automated MotD highlights extractor
This repo contains a small python (3) module for automatically removing the banal and uninsightful commentary from Match of the day, leaving you with just footage of the actual football you wanted to watch in the firrst place.


## Example

```python
import scoreboard_slicer as motd

my_clip = motd.load_video('full_fat_motd.mp4')
motd.extract_highlights(my_clip, 'highlights_only.mp4')
```

The module contains two primary functions:

* `load_video` for loading the video (obviously). This is effectively just a wrapper around `moviepy.editor.VideoFileClip`.
* `extract_highlights` for identifying, trimming and joining back together the match highlights.

And that's more or less all there is to it.

134 changes: 134 additions & 0 deletions Scoreboard method demo.ipynb

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions scoreboard_slicer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import moviepy.editor as mpy
import numpy as np
import progressbar
from skimage.color import rgb2grey
from skimage.feature import corner_harris, corner_peaks


# Load video
def load_video(filename='motd-sample.mp4', in_folder=True):
video_loc = './video/' if in_folder else ''
clip = mpy.VideoFileClip(video_loc + filename)
# change resolution to standardise incoming video & speed up image processing
return clip


# Get clip's resolution (pixels)
def get_resolution(clip):
sample_frame = clip.get_frame(0)
return len(sample_frame[0]), len(sample_frame)


# Take a frame of a movie and return number of corners
def find_corners(image, min_dist=5):
bw_image = rgb2grey(image)
corners = corner_peaks(corner_harris(bw_image), min_distance=min_dist)
return len(corners)


def moving_average(values, window):
weights = np.repeat(1.0, window) / window
sma = np.convolve(values, weights, 'same')
return sma


def extract_highlights(clip, file_name='output.mp4',
xlim=[0.085, 0.284], ylim=[0.05, 0.1],
sampling_rate=1, minimum_clip=60,
buffer_length=5):

resolution = get_resolution(clip)

# Crop to top left corner
box_clip = clip.crop(x1=xlim[0] * resolution[0], x2=xlim[1] * resolution[0],
y1=ylim[0] * resolution[1], y2=ylim[1] * resolution[1])

# Get number of 'corners' for each frame at given sampling rate
frame_times = np.arange(0, box_clip.duration, 1 / sampling_rate)
bar = progressbar.ProgressBar()
n_corners = [find_corners(box_clip.get_frame(t)) for t in bar(frame_times)]

rolling_corners = moving_average(n_corners, 30 * sampling_rate)
is_highlights = np.where([rolling_corners > np.mean(rolling_corners)], 1, 0)[0]

changes = np.diff(is_highlights)
starts = np.where(changes == 1)[0]
stops = np.where(changes == -1)[0]

start_stop = [(starts[i], stops[i]) for i in range(len(starts)) if (stops[i] - starts[i]) >= minimum_clip]

# get highlights in a list
highlights = [clip.subclip(t_start=t[0] - buffer_length, t_end=t[1] + buffer_length) for t in start_stop]
# add fade in/out (half buffer length?)
highlights = [h.fadein(buffer_length / 2) for h in highlights]
highlights = [h.fadeout(buffer_length / 2) for h in highlights]

# join videos together into one and write to file
final_clip = mpy.concatenate_videoclips(highlights, method='compose')
final_clip.write_videofile('./output/' + file_name)


2 changes: 2 additions & 0 deletions video/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Video
Directory for motd video files

0 comments on commit 319c6f0

Please sign in to comment.