Spaces:
Paused
Paused
File size: 2,966 Bytes
c2e8a2e 5379aa1 c2e8a2e efc6117 c2e8a2e efc6117 c2e8a2e efc6117 c2e8a2e efc6117 c2e8a2e efc6117 c2e8a2e be74542 c2e8a2e efc6117 be74542 c2e8a2e efc6117 c2e8a2e efc6117 c2e8a2e efc6117 c2e8a2e be74542 c2e8a2e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 87 88 89 90 91 92 93 94 95 |
import gradio as gr
import subprocess
import os
import tempfile
from pathlib import Path
def convert_ts_to_mp4(file_path):
"""
Converts a .ts video file to .mp4 using ffmpeg.
Args:
file_path (str): The path to the .ts file.
Returns:
str: The path to the converted .mp4 file, or None on error.
"""
try:
# 1. Check if the file exists
if not os.path.exists(file_path):
return "Error: File not found."
# 2. Check if the file has the correct extension
if not file_path.lower().endswith(".ts"):
return "Error: Invalid file type. Please upload a .ts file."
# 3. Convert the .ts file to .mp4 using ffmpeg in a temporary location
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as mp4_file:
try:
subprocess.run(
[
"ffmpeg",
"-i",
file_path,
"-c:v",
"libx264", # Use libx264 for H.264 encoding (common)
"-c:a",
"aac", # Use AAC for audio encoding (common)
"-y", # Overwrite output file if it exists
mp4_file.name,
],
check=True, # Raise an exception on non-zero exit code
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError as e:
# ffmpeg failed. Return the error message.
error_message = f"FFmpeg conversion failed: {e.stderr.decode('utf-8')}"
print(error_message) # Print to console for debugging in Spaces
return error_message
# 4. Return the path to the .mp4 file
return mp4_file.name
except Exception as e:
return f"An error occurred: {e}"
def gradio_interface():
"""
Defines the Gradio interface for the application.
"""
inputs = [
gr.File(
label="Upload .TS File",
file_types=[".ts"], # Restrict to .ts files
),
]
outputs = gr.File(label="Converted MP4 File")
title = "TS to MP4 Converter"
description = "Convert .ts video files to .mp4 format. Upload a .ts file, and the converted .mp4 file will be available for download."
article = """
Example Usage:
1. Click the 'Upload .TS File' button to upload a .ts video file from your local machine.
2. Click the 'Submit' button.
3. The converted .mp4 file will be processed, and a download link will be provided.
"""
return gr.Interface(
fn=convert_ts_to_mp4,
inputs=inputs,
outputs=outputs,
title=title,
description=description,
article=article,
)
if __name__ == "__main__":
gradio_interface().launch()
|