Jeffgold commited on
Commit
f97ecc6
1 Parent(s): 8412930

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -29
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import logging
2
  import requests
3
  from urllib.parse import urlparse, quote_plus
4
  import subprocess
@@ -52,23 +51,25 @@ def get_ip_address():
52
  s.close()
53
  return IP
54
 
55
- def upload_video_to_s3(video_file): # UPDATED CODE
56
- s3_key = video_file.name
57
- s3.upload_fileobj(video_file, AWS_S3_BUCKET, s3_key)
58
- return f"s3://{AWS_S3_BUCKET}/{s3_key}"
59
-
60
- def download_s3_video_to_local(input_path): # UPDATED CODE
61
- fs.download(input_path, str(output_dir / Path(input_path).name))
62
-
63
- def get_input_path(video_file=None, video_url=None): # UPDATED CODE
64
- if video_file:
65
- return upload_video_to_s3(video_file)
 
 
66
  elif video_url:
67
  url_path = urlparse(video_url).path
68
  file_name = Path(url_path).name
69
  destination = output_dir / file_name
70
  download_file(video_url, destination)
71
- return upload_video_to_s3(destination)
72
  else:
73
  raise ValueError("No input was provided.")
74
 
@@ -91,29 +92,45 @@ def create_master_playlist(output_paths, input_filename):
91
  f.writelines(content)
92
  return master_playlist_path
93
 
94
- def convert_video(video_file, quality, aspect_ratio, video_url): # UPDATED CODE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Ensure either a file or a URL is provided
96
  if not video_file and not video_url:
97
  raise ValueError("Please provide either a video file or a video URL.")
98
 
99
- # Get S3 path and download to local
100
  input_path = get_input_path(video_file, video_url)
101
- local_input_path = download_s3_video_to_local(input_path)
102
-
103
- st.write(f"Input file path: {local_input_path}") # Added line to print input file path
104
- video = VideoFileClip(str(local_input_path))
105
 
106
  # Get the original height to avoid upscaling
107
  original_height = video.size[1]
108
- output_paths = []
109
 
 
110
  for res in reversed(standard_resolutions):
111
  if res > original_height:
112
  continue
 
113
  scale = "-1:" + str(res)
114
  output_path = get_output_path(input_path, str(res) + 'p')
 
 
115
  ffmpeg_command = [
116
- "ffmpeg", "-i", str(local_input_path), "-c:v", "libx264", "-crf", str(quality),
117
  "-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2",
118
  "-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",
119
  str(output_dir / f"{output_path.stem}_%03d.ts"), str(output_path)
@@ -124,23 +141,20 @@ def convert_video(video_file, quality, aspect_ratio, video_url): # UPDATED CODE
124
 
125
  output_paths.append(output_path)
126
 
127
- master_playlist_path = create_master_playlist(output_paths, Path(local_input_path).stem)
128
  output_paths.append(master_playlist_path)
129
 
130
  output_links = []
131
  for path in output_paths:
132
- s3_path = f"s3://{AWS_S3_BUCKET}/{Path(local_input_path).stem}/{Path(path).name}"
133
  upload_file_to_s3(path, s3_path)
134
- output_links.append(f'<a href="https://{AWS_S3_BUCKET}.s3.amazonaws.com/{Path(local_input_path).stem}/{quote_plus(Path(path).name)}" target="_blank">Download {Path(path).stem}</a>')
135
 
136
  # upload ts files to s3
137
- for path in output_dir.glob(f"{Path(local_input_path).stem}_*p_*.ts"):
138
- s3_path = f"s3://{AWS_S3_BUCKET}/{Path(local_input_path).stem}/{Path(path).name}"
139
  upload_file_to_s3(path, s3_path)
140
 
141
- # Delete local downloaded file after job is done
142
- os.remove(local_input_path)
143
-
144
  output_html = "<br>".join(output_links)
145
  return output_html
146
 
 
 
1
  import requests
2
  from urllib.parse import urlparse, quote_plus
3
  import subprocess
 
51
  s.close()
52
  return IP
53
 
54
+ def download_file(url, destination):
55
+ response = requests.get(url)
56
+ response.raise_for_status()
57
+ with open(destination, 'wb') as f:
58
+ f.write(response.content)
59
+
60
+ def get_input_path(video_file, video_url):
61
+ if video_file is not None:
62
+ # Save the uploaded file to the output directory
63
+ input_path = output_dir / video_file.name
64
+ with open(input_path, "wb") as f:
65
+ f.write(video_file.getbuffer())
66
+ return input_path
67
  elif video_url:
68
  url_path = urlparse(video_url).path
69
  file_name = Path(url_path).name
70
  destination = output_dir / file_name
71
  download_file(video_url, destination)
72
+ return destination
73
  else:
74
  raise ValueError("No input was provided.")
75
 
 
92
  f.writelines(content)
93
  return master_playlist_path
94
 
95
+ def upload_file_to_s3(local_path, s3_key):
96
+ s3 = boto3.client(
97
+ "s3",
98
+ aws_access_key_id=st.secrets["AWS_ACCESS_KEY_ID"],
99
+ aws_secret_access_key=st.secrets["AWS_SECRET_ACCESS_KEY"],
100
+ region_name=st.secrets["AWS_REGION"],
101
+ )
102
+
103
+ bucket_name = st.secrets["AWS_S3_BUCKET"]
104
+
105
+ with open(local_path, "rb") as file:
106
+ s3.upload_fileobj(file, bucket_name, s3_key)
107
+
108
+ return f"https://{bucket_name}.s3.amazonaws.com/{s3_key}"
109
+
110
+
111
+ def convert_video(video_file, quality, aspect_ratio, video_url):
112
  # Ensure either a file or a URL is provided
113
  if not video_file and not video_url:
114
  raise ValueError("Please provide either a video file or a video URL.")
115
 
 
116
  input_path = get_input_path(video_file, video_url)
117
+ st.write(f"Input file path: {input_path}") # Added line to print input file path
118
+ video = VideoFileClip(str(input_path))
 
 
119
 
120
  # Get the original height to avoid upscaling
121
  original_height = video.size[1]
 
122
 
123
+ output_paths = []
124
  for res in reversed(standard_resolutions):
125
  if res > original_height:
126
  continue
127
+
128
  scale = "-1:" + str(res)
129
  output_path = get_output_path(input_path, str(res) + 'p')
130
+
131
+ # use a lower CRF value for better quality
132
  ffmpeg_command = [
133
+ "ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
134
  "-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2",
135
  "-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",
136
  str(output_dir / f"{output_path.stem}_%03d.ts"), str(output_path)
 
141
 
142
  output_paths.append(output_path)
143
 
144
+ master_playlist_path = create_master_playlist(output_paths, Path(input_path).stem)
145
  output_paths.append(master_playlist_path)
146
 
147
  output_links = []
148
  for path in output_paths:
149
+ s3_path = f"s3://{AWS_S3_BUCKET}/{Path(input_path).stem}/{Path(path).name}"
150
  upload_file_to_s3(path, s3_path)
151
+ output_links.append(f'<a href="https://{AWS_S3_BUCKET}.s3.amazonaws.com/{Path(input_path).stem}/{quote_plus(Path(path).name)}" target="_blank">Download {Path(path).stem}</a>')
152
 
153
  # upload ts files to s3
154
+ for path in output_dir.glob(f"{Path(input_path).stem}_*p_*.ts"):
155
+ s3_path = f"s3://{AWS_S3_BUCKET}/{Path(input_path).stem}/{Path(path).name}"
156
  upload_file_to_s3(path, s3_path)
157
 
 
 
 
158
  output_html = "<br>".join(output_links)
159
  return output_html
160