Skip to content

Commit

Permalink
(2.3.0): Refactor Input Formatting (#24)
Browse files Browse the repository at this point in the history
* move utility files to a seperate folder

* moved to utils

* remove playlists and local buttons and functionality as it is no longer needed

* update requirements as deprecated youtubesearchpython library is no longer used

* fix local video file handlging

* refactor youtube check functions and rename files
  • Loading branch information
Allan Cao authored Oct 21, 2022
1 parent 41ff45c commit e55d1c2
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 88 deletions.
4 changes: 1 addition & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ def input():
form = request.form

input_vars = [
'local',
'playlist',
'graphs',
'minimap',
'lightweight'
Expand All @@ -80,7 +78,7 @@ def input():
for var in input_vars:
input_args[var] = var in form

run_video_thread = threading.Thread(
threading.Thread(
target=lava.execute,
name="tracker",
kwargs=input_args
Expand Down
71 changes: 12 additions & 59 deletions assets/lava.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from numpy.linalg import norm
from sympy.geometry import Point, Circle, intersection, Line
from sympy import N
from youtubesearchpython import Playlist, Video, StreamURLFetcher
from assets.utils import utils
from assets.utils.ytHelper import is_valid_youtube_url, parse_youtube_url
from assets.utils.videoFileHelper import parse_local_files

from assets.graphing import GraphsOperator
from assets import utils, constants

import assets.utils.constants as constants

class LAVA(GraphsOperator):
def __init__(self):
Expand All @@ -29,37 +30,28 @@ def __init__(self):

def execute(self,
url="",
local=False,
playlist=False,
output_file='positions.csv',
skip=0,
minimap=False,
graphs=False,
lightweight=True):
df = self.gather_data(
url,
local,
output_file,
playlist,
skip,
minimap,
lightweight
)

if graphs is True:
self.draw_graphs(df, df.video.iloc[0])

print(f'Video {df.video.iloc[0]} complete')

def gather_data(self,
url="",
local=False,
output_file='positions.csv',
playlist=False,
skip=0,
minimap=False,
lightweight=True):
videos = self.parse_url(url, local, playlist, skip)
videos = self.parse_url(url)
full_data = pd.DataFrame()

self.lightweight = lightweight
Expand Down Expand Up @@ -119,9 +111,8 @@ def gather_data(self,
cv2.destroyAllWindows()

return full_data

def gather_info(self, url="", local=False, playlist=False, skip=0):
videos = self.parse_url(url, local, playlist, skip)
def gather_info(self, url=""):
videos = self.parse_url(url)

full_data = {}

Expand Down Expand Up @@ -150,49 +141,11 @@ def gather_info(self, url="", local=False, playlist=False, skip=0):
full_data[video] = champs
return full_data

def parse_url(self, url="", local=False, playlist=False, skip=0):
videos = {}

if local is False:
fetcher = StreamURLFetcher()

if playlist is True:
playlist_data = Playlist.getVideos(url)
videos_list = [x['link'] for x in playlist_data['videos']]
videos_list = videos_list[skip:]
else:
if isinstance(url, list):
videos_list = url
else:
videos_list = [url]

for video_url in videos_list:
video = Video.get(video_url)
url = fetcher.get(video, 22)

video = utils.clean_for_directory(video['title'])

videos[video] = url
# If pulling from local
def parse_url(self, url=""):
if is_valid_youtube_url(url):
return parse_youtube_url(url)
else:
if playlist is True:
videos_list = os.listdir('input')
videos_list.remove('.gitkeep')
videos_list = videos_list[skip:]
else:
if isinstance(url, list):
videos_list = url
else:
videos_list = [url]

for video_file in videos_list:
video_path = video_file

video = os.path.splitext(video_file)[0]

videos[video] = video_path

return videos
return parse_local_files(url)

def headers(self):
headers_path = os.path.join(
Expand Down
2 changes: 2 additions & 0 deletions assets/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
1 change: 1 addition & 0 deletions assets/constants.py → assets/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cv2


tracking_folder = os.path.join(
"assets",
"tracking")
Expand Down
9 changes: 0 additions & 9 deletions assets/utils.py → assets/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
import cv2


def clean_for_directory(video_title):
for ch in ['*', '.', '"', '/', '\\', ':', ';', '|', ',']:
if ch in video_title:
video_title = video_title.replace(ch, '')
video_title = video_title.replace(' ', '_')

return video_title


def get_header_borders(frame_shape):
frame_height, frame_width, _ = frame_shape
header_height = frame_height // 15
Expand Down
31 changes: 31 additions & 0 deletions assets/utils/video_file_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os

VALID_VIDEO_FORMATS = ["WEBM", "MPG", "MP2", "MPEG", "MPE", "MPV", "OGG", "MP4", "M4P", "M4V", "AVI", "WMV", "MOV", "QT", "FLV", "SWF", "AVCHD"]

def clean_for_directory(video_title : str) -> str:
for ch in ['*', '.', '"', '/', '\\', ':', ';', '|', ',']:
if ch in video_title:
video_title = video_title.replace(ch, '')
video_title = video_title.replace(' ', '_')

return video_title

def parse_local_files(url : str) -> dict or None:
videos = {}
print(url)
if os.path.isdir(url):
for file in os.listdir(url):
if file.split('.')[-1].upper() in VALID_VIDEO_FORMATS:
# get the file's name
video_name = clean_for_directory(file)
videos[video_name] = os.path.join(url, file)
else:
print(f'Invalid file format: {file}')
elif os.path.isfile(url):
if url.split('.')[-1].upper() in VALID_VIDEO_FORMATS:
video_name = clean_for_directory(url)
videos[video_name] = url
return videos
else:
print(f'Invalid file format: {url}')
return None
27 changes: 27 additions & 0 deletions assets/utils/youtube_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import yt_dlp
from assets.utils.video_file_helper import clean_for_directory

def is_valid_youtube_url(url) -> bool:
return 'youtube' in url and '/watch?v=' in url

def is_youtube_playlist(url) -> bool:
return '&list=' in url or '/playlist?list=' in url

def parse_youtube_url(url : str) -> dict:
videos = {}
ydl_opts = {
'format': '22',
'ignoreerrors': True,
'no_warnings': True
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
video_info = ydl.sanitize_info(info)
if video_info['_type'] == 'playlist':
for video in video_info['entries']:
video_name = clean_for_directory(video['title'])
videos[video_name] = video_info['formats'][-1]['url']
else:
video_name = clean_for_directory(video_info['title'])
videos[video_name] = video_info['formats'][-1]['url']
return videos
15 changes: 0 additions & 15 deletions assets/webui_templates/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,12 @@ <h3>I'm aware of the issues with leagues that have strange overlays, working on

<div class="row justify-content-md-center w-100">
<div class="input-group input-group-mb-3">
<div class="input-group-prepend">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-info">
<input type="radio" id="option1" name="youtube" value="false" checked=""> YouTube
</label>
<label class="btn btn-info">
<input type="radio" id="option2" name="local" value="true"> Local
</label>
</div>

</div>
<input type="text" class="form-control" aria-label="Text input with radio button" name="url" value="" placeholder="URL/Video filename">
</div>
</div>

<div class="row justify-content-md-center w-100">
<div class="form-check">
<label class="btn btn-info">
<input type="checkbox" class="btn-check" id="btn-check2" autocomplete="off" name="playlist" value="true"> Playlist
</label>

<label class="btn btn-info">
<input type="checkbox" class="btn-check" id="btn-check2" autocomplete="off" name="graphs" value="true"> Draw Graphs
</label>
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ plotly==4.14.3
pytube==10.8.1
scipy==1.9.1
sympy==1.8
youtube-search-python==1.6.6
yt-dlp==2022.6.22.1
yt-dlp==2022.10.4

0 comments on commit e55d1c2

Please sign in to comment.