-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_detection_bjjmatch.py
160 lines (128 loc) · 3.95 KB
/
audio_detection_bjjmatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Created on May 15 2020
@author: neongreen13
Example:
python audio_detection_bjjmatch.py
"""
import warnings
warnings.filterwarnings('ignore')
import argparse
import youtube_dl
from moviepy.editor import *
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.video.io.ffmpeg_tools import ffmpeg_movie_from_frames
from moviepy.editor import VideoFileClip, concatenate_videoclips
import librosa
import speech_recognition as sr
import IPython.display as ipd
import imageio
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import time
import datetime as dt
from os import getcwd
import glob
import os
from natsort import natsorted
import sys
from os.path import isfile
import json
path = 'set path'
url = 'add url to match'
def download_url(url):
options = {}
os.chdir(path)
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([url])
videoname = [os.path.basename(x) for x in glob.glob(path + '*.mp4')]
return videoname
def video_to_audio(videoname):
videoclip = VideoFileClip(path + videoname[0])
audioclip = videoclip.audio
audioclip.write_audiofile(path + "match_audio.wav")
audio_file = [os.path.basename(x) for x in glob.glob(path + '*.wav')]
return audio_file
def audio_energy(audio_file):
filename = path + audio_file[0]
x, sr = librosa.load(filename,sr=16000)
int(librosa.get_duration(x, sr)/60)
max_slice=5
window_length = max_slice * sr
energy = np.array([sum(abs(x[i:i+window_length]**2)) for i in range(0, len(x), window_length)])
energy_90 = np.percentile(energy, 90)
df=pd.DataFrame(columns=['energy','start','end'])
thresh = energy_90
row_index=0
for i in range(len(energy)):
value=energy[i]
if(value>=thresh):
i=np.where(energy == value)[0]
df.loc[row_index,'energy']=value
df.loc[row_index,'start']=i[0] * 5
df.loc[row_index,'end']=(i[0]+1) * 5
row_index= row_index + 1
temp=[]
i=0
j=0
n=len(df) - 2
m=len(df) - 1
while(i<=n):
j=i+1
while(j<=m):
if(df['end'][i] == df['start'][j]):
df.loc[i,'end'] = df.loc[j,'end']
temp.append(j)
j=j+1
else:
i=j
break
df.drop(temp,axis=0,inplace=True)
df.to_csv(path + 'energy_file.csv',index=False)
return df
def make_clips(df,videoname):
clips_path = path + '/clips_folder/'
if os.path.isdir(clips_path) == False:
os.mkdir(clips_path)
df = pd.read_csv(path + 'energy_file.csv')
start=np.array(df['start'])
end=np.array(df['end'])
for i in range(len(df)):
if(i!=0):
start_lim = start[i] - 5
else:
start_lim = start[i]
end_lim = end[i]
filename="highlight" + str(i+1) + ".mp4"
clip = VideoFileClip(path + videoname[0]).subclip(start_lim,end_lim)
clip.write_videofile(clips_path + filename)
return clips_path
def clips_to_highlight():
clips_path = path + '/clips_folder/'
L =[]
for root, dirs, files in os.walk(clips_path):
files = natsorted(files)
for file in files:
if os.path.splitext(file)[1] == '.mp4':
filePath = os.path.join(root, file)
video = VideoFileClip(filePath)
L.append(video)
final_clip = concatenate_videoclips(L)
final_clip.to_videofile(path + "match_highlights.mp4", fps=24, remove_temp=False)
return True
def main(path,url):
videoname = download_url(url)
audio_file = video_to_audio(videoname)
df = audio_energy(audio_file)
make_clips(df,videoname)
clips_to_highlight()
return True
if __name__ == "__main__":
try:
finished = main(path,url)
except FileNotFoundError:
print("The file does not exist in the current working directory.")
exit()
if finished:
print("\nDone.")