Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,6 +83,14 @@ def create_master_playlist(output_paths):
|
|
| 83 |
f.write(f'{Path(path).name}\n')
|
| 84 |
return master_playlist_path
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def convert_video(video_file, quality, aspect_ratio, video_url):
|
| 87 |
input_path = get_input_path(video_file, video_url)
|
| 88 |
|
|
@@ -99,17 +107,22 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
|
|
| 99 |
|
| 100 |
scale = "-1:" + str(res)
|
| 101 |
output_path = get_output_path(input_path, str(res) + 'p')
|
|
|
|
| 102 |
|
| 103 |
ffmpeg_command = [
|
| 104 |
"ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
|
| 105 |
"-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
| 106 |
"-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",
|
| 107 |
-
str(
|
| 108 |
]
|
| 109 |
|
| 110 |
logging.info("Running ffmpeg command: " + ' '.join(ffmpeg_command))
|
| 111 |
subprocess.run(ffmpeg_command, check=True)
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
output_paths.append(output_path)
|
| 114 |
|
| 115 |
master_playlist_path = create_master_playlist(output_paths)
|
|
@@ -117,19 +130,16 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
|
|
| 117 |
|
| 118 |
server_ip = get_ip_address()
|
| 119 |
output_links = []
|
|
|
|
|
|
|
| 120 |
for path in output_paths:
|
| 121 |
output_links.append(f'<a href="http://{server_ip}:8000{path}" target="_blank">Download {Path(path).stem}</a>')
|
| 122 |
-
|
| 123 |
-
with open(path, 'rb') as file:
|
| 124 |
-
file_content = file.read()
|
| 125 |
-
print("Upload " + Path(path).stem + " to AWS")
|
| 126 |
-
# fs.mv(str(output_dir / f"{output_path.stem}_%03d.ts"), f"s3://{AWS_S3_BUCKET}/{Path(path).stem}",)
|
| 127 |
-
with fs.open(f"s3://{AWS_S3_BUCKET}/{Path(path).stem}", 'wb') as file:
|
| 128 |
-
file.write(file_content)
|
| 129 |
|
| 130 |
output_html = "<br>".join(output_links)
|
| 131 |
return output_html
|
| 132 |
|
|
|
|
| 133 |
video_file = gr.inputs.File(label="Your video file")
|
| 134 |
quality = gr.inputs.Slider(minimum=1, maximum=50, label="Quality (1:Speed - 50:Quality)")
|
| 135 |
aspect_ratio = gr.inputs.Dropdown(["16:9", "4:3", "1:1", "3:4", "9:16", "1:1", "2:1", "1:2"], label="Aspect Ratio", default="16:9")
|
|
|
|
| 83 |
f.write(f'{Path(path).name}\n')
|
| 84 |
return master_playlist_path
|
| 85 |
|
| 86 |
+
def upload_file_to_s3(file_path, s3_path):
|
| 87 |
+
print(f"Upload {file_path} to AWS")
|
| 88 |
+
with open(file_path, 'rb') as file:
|
| 89 |
+
file_content = file.read()
|
| 90 |
+
|
| 91 |
+
with fs.open(s3_path, 'wb') as file:
|
| 92 |
+
file.write(file_content)
|
| 93 |
+
|
| 94 |
def convert_video(video_file, quality, aspect_ratio, video_url):
|
| 95 |
input_path = get_input_path(video_file, video_url)
|
| 96 |
|
|
|
|
| 107 |
|
| 108 |
scale = "-1:" + str(res)
|
| 109 |
output_path = get_output_path(input_path, str(res) + 'p')
|
| 110 |
+
output_file_stem = output_dir / f"{output_path.stem}_%03d.ts"
|
| 111 |
|
| 112 |
ffmpeg_command = [
|
| 113 |
"ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
|
| 114 |
"-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
| 115 |
"-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",
|
| 116 |
+
str(output_file_stem), str(output_path)
|
| 117 |
]
|
| 118 |
|
| 119 |
logging.info("Running ffmpeg command: " + ' '.join(ffmpeg_command))
|
| 120 |
subprocess.run(ffmpeg_command, check=True)
|
| 121 |
|
| 122 |
+
# Upload TS files
|
| 123 |
+
for ts_file in output_dir.glob(f"{output_file_stem.stem}*.ts"):
|
| 124 |
+
upload_file_to_s3(ts_file, f"s3://{AWS_S3_BUCKET}/{ts_file.name}")
|
| 125 |
+
|
| 126 |
output_paths.append(output_path)
|
| 127 |
|
| 128 |
master_playlist_path = create_master_playlist(output_paths)
|
|
|
|
| 130 |
|
| 131 |
server_ip = get_ip_address()
|
| 132 |
output_links = []
|
| 133 |
+
|
| 134 |
+
# Upload M3U8 files
|
| 135 |
for path in output_paths:
|
| 136 |
output_links.append(f'<a href="http://{server_ip}:8000{path}" target="_blank">Download {Path(path).stem}</a>')
|
| 137 |
+
upload_file_to_s3(path, f"s3://{AWS_S3_BUCKET}/{path.name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
output_html = "<br>".join(output_links)
|
| 140 |
return output_html
|
| 141 |
|
| 142 |
+
|
| 143 |
video_file = gr.inputs.File(label="Your video file")
|
| 144 |
quality = gr.inputs.Slider(minimum=1, maximum=50, label="Quality (1:Speed - 50:Quality)")
|
| 145 |
aspect_ratio = gr.inputs.Dropdown(["16:9", "4:3", "1:1", "3:4", "9:16", "1:1", "2:1", "1:2"], label="Aspect Ratio", default="16:9")
|