Spaces:
Paused
Paused
Update app.py
Browse files123447 unmodified lines525354555657585960616263646566676869707172737416 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181194 unmodified lines124125126127128129130131132133134135136137138139140141142143144145146import loggingimport requestsfrom urllib.parse import urlparse, quote_plusimport subprocess47 unmodified liness.close()return IPdef upload_video_to_s3(video_file): # UPDATED CODEs3_key = video_file.names3.upload_fileobj(video_file, AWS_S3_BUCKET, s3_key)return f"s3://{AWS_S3_BUCKET}/{s3_key}"def download_s3_video_to_local(input_path): # UPDATED CODEfs.download(input_path, str(output_dir / Path(input_path).name))def get_input_path(video_file=None, video_url=None): # UPDATED CODEif video_file:return upload_video_to_s3(video_file)elif video_url:url_path = urlparse(video_url).pathfile_name = Path(url_path).namedestination = output_dir / file_namedownload_file(video_url, destination)return upload_video_to_s3(destination)else:raise ValueError("No input was provided.")16 unmodified linesf.writelines(content)return master_playlist_pathdef convert_video(video_file, quality, aspect_ratio, video_url): # UPDATED CODE# Ensure either a file or a URL is providedif not video_file and not video_url:raise ValueError("Please provide either a video file or a video URL.")# Get S3 path and download to localinput_path = get_input_path(video_file, video_url)local_input_path = download_s3_video_to_local(input_path)st.write(f"Input file path: {local_input_path}") # Added line to print input file pathvideo = VideoFileClip(str(local_input_path))# Get the original height to avoid upscalingoriginal_height = video.size[1]output_paths = []for res in reversed(standard_resolutions):if res > original_height:continuescale = "-1:" + str(res)output_path = get_output_path(input_path, str(res) + 'p')ffmpeg_command = ["ffmpeg", "-i", str(local_input_path), "-c:v", "libx264", "-crf", str(quality),"-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2","-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",str(output_dir / f"{output_path.stem}_%03d.ts"), str(output_path)4 unmodified linesoutput_paths.append(output_path)master_playlist_path = create_master_playlist(output_paths, Path(local_input_path).stem)output_paths.append(master_playlist_path)output_links = []for path in output_paths:s3_path = f"s3://{AWS_S3_BUCKET}/{Path(local_input_path).stem}/{Path(path).name}"upload_file_to_s3(path, s3_path)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>')# upload ts files to s3for path in output_dir.glob(f"{Path(local_input_path).stem}_*p_*.ts"):s3_path = f"s3://{AWS_S3_BUCKET}/{Path(local_input_path).stem}/{Path(path).name}"upload_file_to_s3(path, s3_path)# Delete local downloaded file after job is doneos.remove(local_input_path)output_html = "<br>".join(output_links)return output_html12347 unmodified lines5152535455565758596061626364656667686970717273747516 unmodified lines92939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351364 unmodified lines141142143144145146147148149150151152153154155156157158159160import requestsfrom urllib.parse import urlparse, quote_plusimport subprocess47 unmodified liness.close()return IPdef download_file(url, destination):response = requests.get(url)response.raise_for_status()with open(destination, 'wb') as f:f.write(response.content)def get_input_path(video_file, video_url):if video_file is not None:# Save the uploaded file to the output directoryinput_path = output_dir / video_file.namewith open(input_path, "wb") as f:f.write(video_file.getbuffer())return input_pathelif video_url:url_path = urlparse(video_url).pathfile_name = Path(url_path).namedestination = output_dir / file_namedownload_file(video_url, destination)return destinationelse:raise ValueError("No input was provided.")16 unmodified linesf.writelines(content)return master_playlist_pathdef upload_file_to_s3(local_path, s3_key):s3 = boto3.client("s3",aws_access_key_id=st.secrets["AWS_ACCESS_KEY_ID"],aws_secret_access_key=st.secrets["AWS_SECRET_ACCESS_KEY"],region_name=st.secrets["AWS_REGION"],)bucket_name = st.secrets["AWS_S3_BUCKET"]with open(local_path, "rb") as file:s3.upload_fileobj(file, bucket_name, s3_key)return f"https://{bucket_name}.s3.amazonaws.com/{s3_key}"def convert_video(video_file, quality, aspect_ratio, video_url):# Ensure either a file or a URL is providedif not video_file and not video_url:raise ValueError("Please provide either a video file or a video URL.")input_path = get_input_path(video_file, video_url)st.write(f"Input file path: {input_path}") # Added line to print input file pathvideo = VideoFileClip(str(input_path))# Get the original height to avoid upscalingoriginal_height = video.size[1]output_paths = []for res in reversed(standard_resolutions):if res > original_height:continuescale = "-1:" + str(res)output_path = get_output_path(input_path, str(res) + 'p')# use a lower CRF value for better qualityffmpeg_command = ["ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),"-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2","-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",str(output_dir / f"{output_path.stem}_%03d.ts"), str(output_path)4 unmodified linesoutput_paths.append(output_path)master_playlist_path = create_master_playlist(output_paths, Path(input_path).stem)output_paths.append(master_playlist_path)output_links = []for path in output_paths:s3_path = f"s3://{AWS_S3_BUCKET}/{Path(input_path).stem}/{Path(path).name}"upload_file_to_s3(path, s3_path)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>')# upload ts files to s3for path in output_dir.glob(f"{Path(input_path).stem}_*p_*.ts"):s3_path = f"s3://{AWS_S3_BUCKET}/{Path(input_path).stem}/{Path(path).name}"upload_file_to_s3(path, s3_path)output_html = "<br>".join(output_links)return output_html
app.py+43-29