Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ import threading
|
|
11 |
import socket
|
12 |
import boto3
|
13 |
import s3fs
|
14 |
-
import os
|
15 |
|
16 |
# Define output directory
|
17 |
output_dir = Path.cwd() / "output"
|
@@ -27,6 +27,9 @@ session = boto3.Session(
|
|
27 |
region_name='us-east-1'
|
28 |
)
|
29 |
|
|
|
|
|
|
|
30 |
logging.basicConfig(level=logging.INFO)
|
31 |
|
32 |
standard_resolutions = [4320, 2160, 1440, 1080, 720, 480, 360, 240]
|
@@ -49,25 +52,23 @@ def get_ip_address():
|
|
49 |
s.close()
|
50 |
return IP
|
51 |
|
52 |
-
def
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
f.write(video_file.getbuffer())
|
64 |
-
return input_path
|
65 |
elif video_url:
|
66 |
url_path = urlparse(video_url).path
|
67 |
file_name = Path(url_path).name
|
68 |
destination = output_dir / file_name
|
69 |
download_file(video_url, destination)
|
70 |
-
return destination
|
71 |
else:
|
72 |
raise ValueError("No input was provided.")
|
73 |
|
@@ -90,48 +91,29 @@ def create_master_playlist(output_paths, input_filename):
|
|
90 |
f.writelines(content)
|
91 |
return master_playlist_path
|
92 |
|
93 |
-
def
|
94 |
-
s3 = boto3.client(
|
95 |
-
"s3",
|
96 |
-
aws_access_key_id=st.secrets["AWS_ACCESS_KEY_ID"],
|
97 |
-
aws_secret_access_key=st.secrets["AWS_SECRET_ACCESS_KEY"],
|
98 |
-
region_name=st.secrets["AWS_REGION"],
|
99 |
-
)
|
100 |
-
|
101 |
-
bucket_name = st.secrets["AWS_S3_BUCKET"]
|
102 |
-
|
103 |
-
with open(local_path, "rb") as file:
|
104 |
-
s3.upload_fileobj(file, bucket_name, s3_key)
|
105 |
-
|
106 |
-
return f"https://{bucket_name}.s3.amazonaws.com/{s3_key}"
|
107 |
-
|
108 |
-
def convert_video(video_file, quality, aspect_ratio, video_url):
|
109 |
# Ensure either a file or a URL is provided
|
110 |
if not video_file and not video_url:
|
111 |
raise ValueError("Please provide either a video file or a video URL.")
|
112 |
|
113 |
-
#
|
114 |
-
if video_file is not None and video_file.size > max_upload_size:
|
115 |
-
raise ValueError("Maximum file size exceeded. Please upload a smaller file.")
|
116 |
-
|
117 |
input_path = get_input_path(video_file, video_url)
|
118 |
-
|
119 |
-
|
|
|
|
|
120 |
|
121 |
# Get the original height to avoid upscaling
|
122 |
original_height = video.size[1]
|
123 |
-
|
124 |
output_paths = []
|
|
|
125 |
for res in reversed(standard_resolutions):
|
126 |
if res > original_height:
|
127 |
continue
|
128 |
-
|
129 |
scale = "-1:" + str(res)
|
130 |
output_path = get_output_path(input_path, str(res) + 'p')
|
131 |
-
|
132 |
-
# use a lower CRF value for better quality
|
133 |
ffmpeg_command = [
|
134 |
-
"ffmpeg", "-i", str(
|
135 |
"-vf", f"scale={scale}:force_original_aspect_ratio=decrease,pad=ceil(iw/2)*2:ceil(ih/2)*2",
|
136 |
"-hls_time", "10", "-hls_playlist_type", "vod", "-hls_segment_filename",
|
137 |
str(output_dir / f"{output_path.stem}_%03d.ts"), str(output_path)
|
@@ -142,27 +124,30 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
|
|
142 |
|
143 |
output_paths.append(output_path)
|
144 |
|
145 |
-
master_playlist_path = create_master_playlist(output_paths, Path(
|
146 |
output_paths.append(master_playlist_path)
|
147 |
|
148 |
output_links = []
|
149 |
for path in output_paths:
|
150 |
-
s3_path = f"s3://{AWS_S3_BUCKET}/{Path(
|
151 |
upload_file_to_s3(path, s3_path)
|
152 |
-
output_links.append(f'<a href="https://{AWS_S3_BUCKET}.s3.amazonaws.com/{Path(
|
153 |
|
154 |
# upload ts files to s3
|
155 |
-
for path in output_dir.glob(f"{Path(
|
156 |
-
s3_path = f"s3://{AWS_S3_BUCKET}/{Path(
|
157 |
upload_file_to_s3(path, s3_path)
|
158 |
|
|
|
|
|
|
|
159 |
output_html = "<br>".join(output_links)
|
160 |
return output_html
|
161 |
|
162 |
st.title("NEAR Hub Video Transcoder to m3u8")
|
163 |
st.markdown("Convert video files to m3u8 for VOD streaming in nearhub.club.")
|
164 |
|
165 |
-
video_file = st.file_uploader("Your video file"
|
166 |
quality = st.slider("Quality (0: Highest Quality - 50: Fastest)", min_value=0, max_value=51, value=23)
|
167 |
aspect_ratio = st.selectbox("Aspect Ratio", ["16:9", "4:3", "1:1", "3:4", "9:16", "1:1", "2:1", "1:2"], index=0)
|
168 |
video_url = st.text_input("Video URL", "")
|
|
|
11 |
import socket
|
12 |
import boto3
|
13 |
import s3fs
|
14 |
+
import os
|
15 |
|
16 |
# Define output directory
|
17 |
output_dir = Path.cwd() / "output"
|
|
|
27 |
region_name='us-east-1'
|
28 |
)
|
29 |
|
30 |
+
s3 = session.client('s3')
|
31 |
+
fs = s3fs.S3FileSystem(anon=False, client_kwargs={'region_name': 'us-east-1'})
|
32 |
+
|
33 |
logging.basicConfig(level=logging.INFO)
|
34 |
|
35 |
standard_resolutions = [4320, 2160, 1440, 1080, 720, 480, 360, 240]
|
|
|
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 |
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 |
|
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 |
|
147 |
st.title("NEAR Hub Video Transcoder to m3u8")
|
148 |
st.markdown("Convert video files to m3u8 for VOD streaming in nearhub.club.")
|
149 |
|
150 |
+
video_file = st.file_uploader("Your video file")
|
151 |
quality = st.slider("Quality (0: Highest Quality - 50: Fastest)", min_value=0, max_value=51, value=23)
|
152 |
aspect_ratio = st.selectbox("Aspect Ratio", ["16:9", "4:3", "1:1", "3:4", "9:16", "1:1", "2:1", "1:2"], index=0)
|
153 |
video_url = st.text_input("Video URL", "")
|