Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
-
from datasets import load_dataset
|
3 |
import subprocess
|
4 |
import os
|
5 |
import tempfile
|
6 |
-
import urllib.request
|
7 |
from pathlib import Path
|
8 |
|
9 |
-
def convert_ts_to_mp4(
|
10 |
"""
|
11 |
-
|
12 |
-
converts it to .mp4 using ffmpeg, and returns the path
|
13 |
-
to the .mp4 file. Handles both public and private datasets.
|
14 |
|
15 |
Args:
|
16 |
-
|
17 |
-
file_name (str): The name of the .ts file within the dataset.
|
18 |
-
It should be just the filename, not the full path.
|
19 |
-
hf_token (str): The Hugging Face token. If None or empty,
|
20 |
-
it's assumed the dataset is public.
|
21 |
|
22 |
Returns:
|
23 |
str: The path to the converted .mp4 file, or None on error.
|
24 |
"""
|
25 |
try:
|
26 |
-
# 1.
|
27 |
-
if
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
#
|
35 |
-
|
36 |
-
# that the dataset provides the files in a way that we can
|
37 |
-
# access them directly.
|
38 |
-
file_url = None
|
39 |
-
for split in dataset.keys(): # Iterate through the splits
|
40 |
-
for example in dataset[split]:
|
41 |
-
if "file" in example and os.path.basename(example["file"]) == file_name:
|
42 |
-
file_url = example["file"]
|
43 |
-
print(file_url)
|
44 |
-
break
|
45 |
-
elif isinstance(example, dict): # Check for nested file paths.
|
46 |
-
for key, value in example.items():
|
47 |
-
if isinstance(value, str) and os.path.basename(value) == file_name:
|
48 |
-
file_url = value;
|
49 |
-
break
|
50 |
-
if file_url:
|
51 |
-
break
|
52 |
-
|
53 |
-
if not file_url:
|
54 |
-
return "Error: File not found in the dataset."
|
55 |
-
|
56 |
-
# 3. Download the .ts file to a temporary location
|
57 |
-
with tempfile.NamedTemporaryFile(suffix=".ts", delete=True) as ts_file:
|
58 |
-
# Use a simple download mechanism. For more robust
|
59 |
-
# downloading, especially with large files, consider
|
60 |
-
# using 'requests' with streaming.
|
61 |
try:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
# ffmpeg failed. Return the error message.
|
87 |
-
error_message = f"FFmpeg conversion failed: {e.stderr.decode('utf-8')}"
|
88 |
-
print(error_message) # Print to console for debugging in Spaces
|
89 |
-
return error_message
|
90 |
-
|
91 |
-
# 5. Return the path to the .mp4 file
|
92 |
-
return mp4_file.name
|
93 |
|
94 |
except Exception as e:
|
95 |
return f"An error occurred: {e}"
|
@@ -101,42 +61,22 @@ def gradio_interface():
|
|
101 |
Defines the Gradio interface for the application.
|
102 |
"""
|
103 |
inputs = [
|
104 |
-
gr.
|
105 |
-
label="
|
106 |
-
|
107 |
-
),
|
108 |
-
gr.Textbox(
|
109 |
-
label="TS File Name (within the dataset)",
|
110 |
-
placeholder="e.g., 'file_name.ts'",
|
111 |
-
),
|
112 |
-
gr.Textbox(
|
113 |
-
label="Hugging Face Token (for private datasets)",
|
114 |
-
placeholder="(Optional) Enter your Hugging Face token here, or set it as HF_TOKEN in Space settings",
|
115 |
-
type="password",
|
116 |
),
|
117 |
]
|
118 |
-
outputs = gr.File(label="Converted MP4 File")
|
119 |
|
120 |
title = "TS to MP4 Converter"
|
121 |
-
description =
|
122 |
-
"Convert .ts video files from Hugging Face datasets to .mp4 format. "
|
123 |
-
"Provide the dataset name and the name of the .ts file. The converted "
|
124 |
-
".mp4 file will be available for download. "
|
125 |
-
"For private datasets, you *must* provide a Hugging Face token, either directly in the input box, or, preferably, by setting the `HF_TOKEN` secret in your Space's settings."
|
126 |
-
)
|
127 |
|
128 |
-
# Example Usage (Corrected)
|
129 |
article = """
|
130 |
Example Usage:
|
131 |
|
132 |
-
1.
|
133 |
-
|
134 |
-
|
135 |
-
2. For a private dataset, enter the dataset name (e.g., 'my-org/my-private-dataset')
|
136 |
-
and the .ts file name. Enter your Hugging Face token in the "Hugging Face Token" field
|
137 |
-
*or*, preferably, add your token as a secret named `HF_TOKEN` in your Space's settings.
|
138 |
-
3. Click the 'Submit' button.
|
139 |
-
4. The converted .mp4 file will be processed, and a download link will be provided.
|
140 |
"""
|
141 |
|
142 |
return gr.Interface(
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import subprocess
|
3 |
import os
|
4 |
import tempfile
|
|
|
5 |
from pathlib import Path
|
6 |
|
7 |
+
def convert_ts_to_mp4(file_path):
|
8 |
"""
|
9 |
+
Converts a .ts video file to .mp4 using ffmpeg.
|
|
|
|
|
10 |
|
11 |
Args:
|
12 |
+
file_path (str): The path to the .ts file.
|
|
|
|
|
|
|
|
|
13 |
|
14 |
Returns:
|
15 |
str: The path to the converted .mp4 file, or None on error.
|
16 |
"""
|
17 |
try:
|
18 |
+
# 1. Check if the file exists
|
19 |
+
if not os.path.exists(file_path):
|
20 |
+
return "Error: File not found."
|
21 |
+
|
22 |
+
# 2. Check if the file has the correct extension
|
23 |
+
if not file_path.lower().endswith(".ts"):
|
24 |
+
return "Error: Invalid file type. Please upload a .ts file."
|
25 |
+
|
26 |
+
# 3. Convert the .ts file to .mp4 using ffmpeg in a temporary location
|
27 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as mp4_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
try:
|
29 |
+
subprocess.run(
|
30 |
+
[
|
31 |
+
"ffmpeg",
|
32 |
+
"-i",
|
33 |
+
file_path,
|
34 |
+
"-c:v",
|
35 |
+
"libx264", # Use libx264 for H.264 encoding (common)
|
36 |
+
"-c:a",
|
37 |
+
"aac", # Use AAC for audio encoding (common)
|
38 |
+
"-y", # Overwrite output file if it exists
|
39 |
+
mp4_file.name,
|
40 |
+
],
|
41 |
+
check=True, # Raise an exception on non-zero exit code
|
42 |
+
stdout=subprocess.PIPE,
|
43 |
+
stderr=subprocess.PIPE,
|
44 |
+
)
|
45 |
+
except subprocess.CalledProcessError as e:
|
46 |
+
# ffmpeg failed. Return the error message.
|
47 |
+
error_message = f"FFmpeg conversion failed: {e.stderr.decode('utf-8')}"
|
48 |
+
print(error_message) # Print to console for debugging in Spaces
|
49 |
+
return error_message
|
50 |
+
|
51 |
+
# 4. Return the path to the .mp4 file
|
52 |
+
return mp4_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
except Exception as e:
|
55 |
return f"An error occurred: {e}"
|
|
|
61 |
Defines the Gradio interface for the application.
|
62 |
"""
|
63 |
inputs = [
|
64 |
+
gr.File(
|
65 |
+
label="Upload .TS File",
|
66 |
+
file_types=[".ts"], # Restrict to .ts files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
),
|
68 |
]
|
69 |
+
outputs = gr.File(label="Converted MP4 File")
|
70 |
|
71 |
title = "TS to MP4 Converter"
|
72 |
+
description = "Convert .ts video files to .mp4 format. Upload a .ts file, and the converted .mp4 file will be available for download."
|
|
|
|
|
|
|
|
|
|
|
73 |
|
|
|
74 |
article = """
|
75 |
Example Usage:
|
76 |
|
77 |
+
1. Click the 'Upload .TS File' button to upload a .ts video file from your local machine.
|
78 |
+
2. Click the 'Submit' button.
|
79 |
+
3. The converted .mp4 file will be processed, and a download link will be provided.
|
|
|
|
|
|
|
|
|
|
|
80 |
"""
|
81 |
|
82 |
return gr.Interface(
|