File size: 7,495 Bytes
3e89400
 
 
 
8f82d17
3e89400
 
84f2262
e3c6b27
3e89400
fc5053d
 
 
 
5d5514b
9d2ec4b
e3c6b27
3e89400
9d2ec4b
 
5d5514b
 
e3c6b27
5d5514b
 
 
 
3e89400
 
 
 
9d2ec4b
 
e3c6b27
 
3e89400
9d2ec4b
 
 
5d5514b
9d2ec4b
e3c6b27
3e89400
5d5514b
 
e3c6b27
 
3e89400
 
e3c6b27
3e89400
 
 
5d5514b
 
e3c6b27
 
 
 
 
7c82cab
e3c6b27
 
ef13ebb
9d2ec4b
ef13ebb
5d5514b
 
 
 
ef13ebb
5d5514b
3e89400
e3c6b27
5d5514b
e3c6b27
 
 
 
 
 
 
5d5514b
 
 
e3c6b27
 
 
 
 
 
 
5d5514b
e3c6b27
 
 
5d5514b
 
e3c6b27
 
 
 
 
5d5514b
e3c6b27
5d5514b
e3c6b27
 
 
 
 
 
 
 
 
5d5514b
 
e3c6b27
 
 
 
 
 
 
 
5d5514b
e3c6b27
fc5053d
e3c6b27
 
5d5514b
e3c6b27
fc5053d
5d5514b
e3c6b27
 
 
 
 
 
5d5514b
 
e3c6b27
 
5d5514b
e3c6b27
5d5514b
 
e3c6b27
 
 
5d5514b
e3c6b27
fc5053d
5d5514b
9d2ec4b
e3c6b27
5d5514b
e3c6b27
 
5d5514b
 
e3c6b27
 
 
5d5514b
 
e3c6b27
 
5d5514b
e3c6b27
 
 
 
5d5514b
 
e3c6b27
 
5d5514b
e3c6b27
 
 
 
 
3e89400
 
5d5514b
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import uuid
import zipfile
import gradio as gr
from tqdm import tqdm
from datetime import datetime
from pydub import AudioSegment
from moviepy import VideoFileClip


audio_formats = ["mp3", "wav", "flac", "ogg", "aac", "m4a", "aiff", "wma", "opus"]
video_formats = ["mp4", "webm", "mkv", "avi", "mov", "flv"]


# ---------- AUDIO PROCESSING ----------
def convert_audio(input_files, output_format, session_id, merge_files, gap_duration):
    """Converts or merges a list of audio files."""
    output_files = []
    merged_audio = AudioSegment.silent(duration=0)

    os.makedirs(session_id, exist_ok=True)

    for input_file in tqdm(input_files, desc="Converting audio files"):
        file_path = input_file if isinstance(input_file, str) else input_file.name
        audio = AudioSegment.from_file(file_path)

        base_name = os.path.splitext(os.path.basename(file_path))[0]
        output_filename = f"{base_name}.{output_format}"
        output_path = os.path.join(session_id, output_filename)
        audio.export(output_path, format=output_format)

        if merge_files:
            merged_audio += audio + AudioSegment.silent(duration=gap_duration)
        else:
            output_files.append(output_path)

    if merge_files:
        merged_output_path = os.path.join(session_id, f"merged_output.{output_format}")
        merged_audio.export(merged_output_path, format=output_format)
        return [merged_output_path]

    return output_files


# ---------- ZIP CREATION ----------
def create_zip(files_to_zip, session_id):
    """Creates a ZIP archive from a list of files."""
    zip_filename = f"{session_id}.zip"
    with zipfile.ZipFile(zip_filename, 'w') as zipf:
        for file in tqdm(files_to_zip, desc="Creating ZIP archive"):
            zipf.write(file, os.path.basename(file))
    return zip_filename


# ---------- AUDIO HANDLER ----------
def process_audio_files(files, output_format, merge_files, gap_duration, progress=gr.Progress(track_tqdm=True)):
    """Main handler function for audio processing."""
    if not files:
        raise gr.Error("Please upload at least one audio file!")

    session_id = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "_" + str(uuid.uuid4())[:8]
    print(f"\nStarting audio session: {session_id}")
    print(f"Files to convert: {len(files)} to format {output_format}")

    output_files = convert_audio(files, output_format, session_id, merge_files, gap_duration)

    if len(output_files) > 1:
        print("Creating ZIP archive...")
        zip_filename = create_zip(output_files, session_id)
        return zip_filename

    return output_files[0]


# ---------- VIDEO HANDLER ----------
def process_video(input_video, conversion_type, output_format, progress=gr.Progress(track_tqdm=True)):
    """Converts a video to another format or extracts its audio."""
    if not input_video:
        raise gr.Error("Please upload a video file first!")

    session_id = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "_" + str(uuid.uuid4())[:8]
    os.makedirs(session_id, exist_ok=True)

    input_path = input_video if isinstance(input_video, str) else input_video.name
    base_name = os.path.splitext(os.path.basename(input_path))[0]
    output_filename = f"{base_name}_converted.{output_format}"
    output_path = os.path.join(session_id, output_filename)

    print(f"\nStarting video session: {session_id}")
    print(f"Conversion type: {conversion_type}, Output format: {output_format}")

    try:
        clip = VideoFileClip(input_path)

        if conversion_type == "Video to Video":
            print("Converting video to video...")
            clip.write_videofile(output_path, codec='libx264', audio_codec='aac', logger=None)

        elif conversion_type == "Video to Audio":
            print("Extracting audio from video...")
            if clip.audio is None:
                raise gr.Error("The uploaded video file does not contain an audio track.")
            audio_clip = clip.audio
            audio_clip.write_audiofile(output_path, logger=None)
            audio_clip.close()

        clip.close()

    except Exception as e:
        print(f"An error occurred: {e}")
        raise gr.Error(f"An error occurred during processing: {e}")

    print("Video processing complete!")
    return output_path


# ---------- FORMAT CHOICES ----------
def update_format_choices(conversion_type):
    """Dynamically updates the format dropdown based on the conversion type."""
    if conversion_type == "Video to Video":
        return gr.Dropdown(choices=video_formats, value="mp4", label="Output Video Format")
    else:
        return gr.Dropdown(choices=audio_formats, value="mp3", label="Output Audio Format")


# ---------- UI ----------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.HTML("<center><h1>Universal Media Converter 🎧🎬</h1></center>")

    with gr.Tabs():
        # AUDIO TAB
        with gr.TabItem("🎢 Audio Converter"):
            gr.HTML("<center>Upload one or more audio files and select the output format. You can also merge all files into a single track.</center>")

            with gr.Row():
                with gr.Column(scale=2):
                    audio_file_input = gr.Files(label="Upload Audio Files", file_types=["audio"])
                with gr.Column(scale=1):
                    audio_format_choice = gr.Dropdown(choices=audio_formats, label="Output Format", value="mp3")
                    merge_files_checkbox = gr.Checkbox(label="Merge all files into one")
                    gap_slider = gr.Slider(minimum=0, maximum=5000, step=100, value=500, label="Gap between files (ms)")

            audio_submit_button = gr.Button("πŸš€ Convert", variant="primary")
            audio_output_file = gr.File(label="Download Result")

            audio_submit_button.click(
                fn=process_audio_files,
                inputs=[audio_file_input, audio_format_choice, merge_files_checkbox, gap_slider],
                outputs=audio_output_file
            )

        # VIDEO TAB
        with gr.TabItem("🎬 Video Converter"):
            gr.HTML("<center>Upload a video file, then choose whether to convert it to another video format or to extract its audio track.</center>")

            with gr.Row():
                with gr.Column(scale=2):
                    video_input = gr.File(label="Upload Video File", file_types=["video"])
                with gr.Column(scale=1):
                    conversion_type_radio = gr.Radio(
                        choices=["Video to Video", "Video to Audio"],
                        label="Conversion Type",
                        value="Video to Video"
                    )
                    video_format_dropdown = gr.Dropdown(
                        choices=video_formats,
                        label="Output Video Format",
                        value="mp4"
                    )

            video_submit_button = gr.Button("πŸš€ Convert", variant="primary")
            video_output_file = gr.File(label="Download Result")

            conversion_type_radio.change(
                fn=update_format_choices,
                inputs=conversion_type_radio,
                outputs=video_format_dropdown
            )

            video_submit_button.click(
                fn=process_video,
                inputs=[video_input, conversion_type_radio, video_format_dropdown],
                outputs=video_output_file
            )

if __name__ == "__main__":
    demo.launch(debug=True)