EthanStanks commited on
Commit
e275e02
·
1 Parent(s): e52a7fb

Trying new YouTube method

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -0
  2. app_backend.py +31 -12
Dockerfile CHANGED
@@ -22,6 +22,7 @@ COPY . /app
22
 
23
  RUN pip install gradio
24
  RUN pip install yt-dlp
 
25
 
26
  EXPOSE 7860
27
  ENV PYTHONUNBUFFERED=1 \
 
22
 
23
  RUN pip install gradio
24
  RUN pip install yt-dlp
25
+ RUN pip install pytube
26
 
27
  EXPOSE 7860
28
  ENV PYTHONUNBUFFERED=1 \
app_backend.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import csv
4
  from yt_dlp import YoutubeDL
5
  from dataclasses import dataclass
 
6
 
7
  device = 'cpu'
8
  model_path = './model/best.pt'
@@ -24,24 +25,42 @@ def parse_inputs(student_name_tb, simulation_videos_tb, real_videos_tb):
24
  real_video_list = [url.strip() for url in real_videos_tb.split(',') if url.strip()]
25
  return student_name_tb.strip(), simulation_video_list, real_video_list
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def download_videos(simulation_links, real_links):
28
  simulation_paths = []
29
  real_paths = []
30
  for i, url in enumerate(simulation_links, start=1):
31
- ydl_opts = {
32
- 'format': 'best',
33
- 'outtmpl': f'{video_folder}/sim_video{i}.%(ext)s'
34
- }
35
- with YoutubeDL(ydl_opts) as ydl:
36
- ydl.download([url])
 
37
  simulation_paths.append(f'{video_folder}/sim_video{i}.mp4')
38
  for i, url in enumerate(real_links, start=1):
39
- ydl_opts = {
40
- 'format': 'best',
41
- 'outtmpl': f'{video_folder}/real_video{i}.%(ext)s'
42
- }
43
- with YoutubeDL(ydl_opts) as ydl:
44
- ydl.download([url])
 
45
  real_paths.append(f'{video_folder}/real_video{i}.mp4')
46
  return simulation_paths, real_paths
47
 
 
3
  import csv
4
  from yt_dlp import YoutubeDL
5
  from dataclasses import dataclass
6
+ import pytube
7
 
8
  device = 'cpu'
9
  model_path = './model/best.pt'
 
25
  real_video_list = [url.strip() for url in real_videos_tb.split(',') if url.strip()]
26
  return student_name_tb.strip(), simulation_video_list, real_video_list
27
 
28
+ def download_youtube_video(youtube_url,rename):
29
+ """Downloads a YouTube video, renames it to the first three characters, and returns the downloaded file path."""
30
+
31
+ try:
32
+ yt = pytube.YouTube(youtube_url)\
33
+ video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
34
+ video.download(f'{video_folder}')
35
+ original_filename = video.default_filename
36
+ os.rename(original_filename, rename)
37
+ return new_filename
38
+
39
+ except Exception as e:
40
+ print("An error occurred:", e)
41
+ return None
42
+
43
+
44
  def download_videos(simulation_links, real_links):
45
  simulation_paths = []
46
  real_paths = []
47
  for i, url in enumerate(simulation_links, start=1):
48
+ #ydl_opts = {
49
+ # 'format': 'best',
50
+ # 'outtmpl': f'{video_folder}/sim_video{i}.%(ext)s'
51
+ #}
52
+ #with YoutubeDL(ydl_opts) as ydl:
53
+ # ydl.download([url])
54
+ download_youtube_video(url,f"sim_video{i}.mp4")
55
  simulation_paths.append(f'{video_folder}/sim_video{i}.mp4')
56
  for i, url in enumerate(real_links, start=1):
57
+ #ydl_opts = {
58
+ # 'format': 'best',
59
+ # 'outtmpl': f'{video_folder}/real_video{i}.%(ext)s'
60
+ #}
61
+ #with YoutubeDL(ydl_opts) as ydl:
62
+ # ydl.download([url])
63
+ download_youtube_video(url,f"real_video{i}.mp4")
64
  real_paths.append(f'{video_folder}/real_video{i}.mp4')
65
  return simulation_paths, real_paths
66