File size: 9,031 Bytes
a044023
c0b6376
 
 
a044023
c0b6376
 
 
 
 
a044023
 
c0b6376
 
 
a044023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import json
import logging
import os
import subprocess
from pprint import pprint
from tempfile import _TemporaryFileWrapper

from ffmpy import FFmpeg

import gradio as gr
from functions import (Clear, CommandBuilder, change_clipbox, customBitrate, mediaChange,
                       supported_codecs, supported_presets, updateOutput, videoChange)

logging.basicConfig(level=logging.INFO)

choices = ["mp3", "ogg", "flac", "wav"]


def parse(param: json) -> dict:
    with open(param) as file:
        return json.load(file)

# global inputs,inputs_clip
data = parse("./data.json")

containers=[j.get("name") for i in data["containers"] for j in data["containers"][i]]
video_containers = [i.get("name") for i in data["containers"]["video"]]
video_codecs = [i.get("name") for i in data["codecs"]["video"]]
video_aspect_ratio = [i.get("name") for i in data["aspects"]]
video_scaling = [i.get("name") for i in data["scalings"]]
""" Audio """
audio_containers = [i.get("name") for i in data["containers"]["audio"]]
audio_codecs = [i.get("name") for i in data["codecs"]["audio"]]
audio_channels = [i.get("name") for i in data["audioChannels"]]
audio_quality = [i.get("name") for i in data["audioQualities"]]
audio_sample_rates = [i.get("name") for i in data["sampleRates"]]

""" Video & Audio Filters """
# deband=[i.get("name") for i in data["deband"]]
# deflicker=[i.get("name") for i in data["deflicker"]]
# deshake=[i.get("name") for i in data["deshake"]]
# dejudder=[i.get("name") for i in data["dejudder"]]
# denoise=[i.get("name") for i in data["denoise"]]
# deinterlace=[i.get("name") for i in data["deinterlace"]]
filters = ["deband", "deflicker", "deshake",
           "dejudder", "denoise", "deinterlace"]
vf = [{vFilter: names} for vFilter in filters for names in [
    [i for i in data[vFilter]]]]

presets = [i.get("name") for i in data["presets"]]
profiles = [i.get("name") for i in data["profiles"]]
speeds = [i.get("name") for i in data["speeds"]]

logging.info(msg=f"{video_containers}")


def convert(file: _TemporaryFileWrapper, options: str):
    stderr=""
    stdout=""
    output_file=""
    video=""
    ffmpeg=FFmpeg()
    try:
        logging.info(f"File name: {file.name}")
        new_name, _ = file.name.split(".")
        logging.info(f"New filename:{new_name}")
        output_file = f"{new_name}1.{options.lower()}"
        ffmpeg = FFmpeg(inputs={file.name: None}, outputs={
                        output_file: ffmpeg_commands.commands.split()}, global_options=["-y", "-hide_banner"])
        print(ffmpeg)
        print(ffmpeg.cmd)

        stdout, stderr=ffmpeg.run(stderr=subprocess.PIPE)
        logging.info(f"{stdout} {stderr}")
        output=f"{ffmpeg.cmd}"
        video=gr.update(label=output_file,value=output_file)

    except Exception as e:
        stderr=e
        output=f"{ffmpeg.cmd} {stderr}"

    return [output_file,output_file,video,output]



"""Helper Functions for Processing """



def get_component_instance(inputs: gr.Blocks) -> list:
    return [gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i,"children")]

# names=[{x:i} for x in ["audioChannels","audioQualities"] for i in [[i.get("name")  for i in data[x]]]]
with gr.Blocks(css="./styles.css") as dm:
    gr.Markdown("Something")
    with gr.Tabs():
        with gr.TabItem("Format"):
            # Input Buttons
            with gr.Row():
                with gr.Column() as inputs:
                    file_input = gr.File()
                    options = gr.Radio(
                        label="options", choices=containers,value=containers[0])

                    with gr.Row() as inputs_clip:
                        clip = gr.Dropdown(
                            choices=["None", "Enabled"], label="Clip:", value="None")
                        start_time = gr.Textbox(
                            label="Start Time:", placeholder="00:00", visible=False)
                        stop_time = gr.Textbox(
                            label="Stop Time:", placeholder="00:00", visible=False)
                    with gr.Row().style(mobile_collapse=False):
                        clearBtn = gr.Button("Clear")
                        convertBtn = gr.Button("Convert", variant="primary")

                # Output Buttons
                with gr.Column():
                    # media_output = gr.Audio(label="Output")
                    with gr.Row().style(mobile_collapse=False):
                        video_button=gr.Button("Video")
                        audio_button=gr.Button("Audio")
                        file_button=gr.Button("File")
                    media_output_audio = gr.Audio(label="Output",visible=True,interactive=False,source="filepath")
                    media_output_video = gr.Video(label="Output",visible=False,format="mp4")
                    media_output_file = gr.File(label="Output",visible=False)
                    output_textbox = gr.Textbox(elem_id="outputtext")
                
                resetFormat=Clear(inputs,inputs_clip)
                clearBtn.click(resetFormat.clear, inputs=resetFormat(), outputs=resetFormat())
                convertBtn.click(convert, inputs=[file_input, options], outputs=[
                    media_output_audio,media_output_file,media_output_video, output_textbox])

        with gr.TabItem("Video"):
            with gr.Row() as video_inputs:
                video_options = gr.Dropdown(
                    label="video", choices=video_codecs,value=video_codecs[-1])
                preset_options = gr.Dropdown(choices=presets, label="presets",value=presets[-1])


            with gr.Column():
                clearBtn = gr.Button("Clear")
            videoReset=Clear(video_inputs)
            clearBtn.click(videoReset.clear, videoReset(), videoReset())

        with gr.TabItem("Audio"):
            with gr.Row().style(mobile_collapse=False) as audio_inputs:
                # print(names[0])
                audio_options = gr.Dropdown(
                    label="audio", choices=audio_codecs, value=audio_codecs[-1])
                audio_bitrate=gr.Dropdown(choices=audio_quality, label="Audio Qualities",
                            value=audio_quality[0])
                custom_bitrate=gr.Number(label="Audio Qualities",visible=False)
                gr.Dropdown(choices=audio_channels,
                            label="Audio Channels", value=audio_channels[0])
                gr.Dropdown(choices=audio_sample_rates,
                            label="Sample Rates", value=audio_sample_rates[0])


            with gr.Column():
                clearBtn = gr.Button("Clear")
            audioReset=Clear(audio_inputs)
            clearBtn.click(audioReset.clear, audioReset(), audioReset())

        with gr.TabItem("Filters") as filter_inputs:
            gr.Markdown("## Video")
            with gr.Row().style(equal_height=True,mobile_collapse=False) as filter_inputs:
                for i in vf:
                    # print(i.values())
                    # values = list(i.values())
                    values=list(i.values())[0]
                    choices=[j for lst in values for j in [lst.get("name")]]
                    a=gr.Dropdown(label=list(i.keys()),
                                choices=choices, value=choices[0])
            gr.Markdown("## Audio")
            with gr.Row() as filter_inputs_1:
                acontrastSlider=gr.Slider(label="Acontrast", elem_id="aconstrast")

            with gr.Column():
                clearBtn = gr.Button("Clear")

            filterReset=Clear(filter_inputs,filter_inputs_1)
            clearBtn.click(filterReset.clear, filterReset(), filterReset())
    
    """ Format Tab change functions"""
    ffmpeg_commands=CommandBuilder(inputs_clip,video_inputs,audio_inputs,filter_inputs,filter_inputs_1)
    ffmpeg_commands.do()
    pprint(ffmpeg_commands.commands)
    ffmpeg_commands.update(output_textbox)
    file_input.change(fn=updateOutput,inputs=file_input,outputs=output_textbox)
    clip.change(fn=change_clipbox, inputs=clip,
                outputs=[start_time, stop_time])
    
    options.change(supported_codecs,[options],[video_options,audio_options])
    # options.change(mediaChange,[options],[media_output_audio,media_output_video])
    # video_button.click(fn=videoChange,inputs=media_output_file,outputs=media_output_video)
    audio_button.click(mediaChange,[audio_button],[media_output_audio,media_output_video,media_output_file])
    video_button.click(mediaChange,[video_button],[media_output_audio,media_output_video,media_output_file])
    # media_output_audio.change(lambda x:gr.update(value=x),[media_output_audio],[media_output_video])
    file_button.click(mediaChange,[file_button],[media_output_audio,media_output_video,media_output_file])
    """Video Tab change functions"""
    video_options.change(supported_presets,[video_options],[preset_options])
    """Audio Tab change functions"""
    audio_bitrate.change(customBitrate,[audio_bitrate],[custom_bitrate])
    
dm.launch()