File size: 4,120 Bytes
6863518
 
 
 
 
 
 
 
 
 
a0fae27
 
 
6863518
 
 
 
 
 
 
 
004f095
 
6863518
 
 
 
 
a0fae27
 
 
 
6863518
 
 
 
a0fae27
 
 
6863518
 
 
 
 
 
 
 
 
 
 
a0fae27
 
 
 
6863518
 
 
a0fae27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6863518
a0fae27
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
import gradio as gr
from PIL import Image
from PIL import ImageSequence
import numpy as np
from apng import APNG
import os
import subprocess


def convert_audio(audio_file, output_format, change_bitrate, bitrate):
    if audio_file is None:
        return "Error: No audio file uploaded."

    # Get the base name of the input file
    base_name = os.path.splitext(os.path.basename(audio_file))[0]

    # Convert the audio file to the selected format
    output_audio_file = f'{base_name}.{output_format}'

    # Create the ffmpeg command
    command = ['ffmpeg', '-y', '-i', audio_file]
    if output_format == "m4r":
        command.extend(['-f', 'mp4'])
    if change_bitrate:
        command.extend(['-b:a', f'{bitrate}k'])
    command.append(output_audio_file)

    # Run the command
    try:
        subprocess.run(command, check=True)
    except subprocess.CalledProcessError as e:
        return f"Error in conversion: {str(e)}"
    
    return output_audio_file

def convert_image(input_image, output_format, change_resolution, width, height):
    if input_image is None:
        return "Error: No image file uploaded."
    
    base_name = os.path.splitext(os.path.basename(input_image))[0]
    
    out_file = f'{base_name}.{output_format}'
    
    command = ['ffmpeg', '-y', '-i', input_image, '-vframes', '1']

    if change_resolution:
        command.extend(['-vf', f'scale={width}:{height}'])

    command.append(out_file)

    try:
        subprocess.run(command, check=True)
    except subprocess.CalledProcessError as e:
        return f"Error in conversion: {str(e)}"

    return out_file



def main():
    # Gradio Interface
    with gr.Blocks() as app:
        gr.Markdown(
            """
            # <div align="center"> Ilaria Converter 💖 </div>
            File conversion Software by Ilaria, support her on [Ko-Fi!](https://ko-fi.com/ilariaowo)  
    
            Need help with AI? [Join AI Hub!](https://discord.gg/aihub)
            """
        )
        
        with gr.Tab('Audio Conversion'):
            with gr.Row():
                audio_input = gr.Audio(type='filepath', label="Upload Audio File")
                with gr.Column():
                    audio_output_format = gr.Dropdown(["wav", "flac", "ogg", "mp3", "aac", "m4a", "m4r"], label="Audio Output Format", info="Choose the desired output format for the audio file.")
                    change_bitrate = gr.Checkbox(label="Change Bitrate?")
                    audio_bitrate = gr.Dropdown(["128", "256", "320"], label="Audio Bitrate", info="Choose the bitrate for the audio file.")
                convert_audio_butt = gr.Button(value='Convert Audio', variant='primary')
                audio_output = gr.File(label="Download Converted Audio")
                    
        convert_audio_butt.click(fn=convert_audio, inputs=[audio_input, audio_output_format, change_bitrate, audio_bitrate], outputs=audio_output)
            
        with gr.Tab('Image Conversion'):
            with gr.Row():
                image_input = gr.Image(type='filepath', image_mode='RGBA', label="Upload Image")
                with gr.Column():
                    image_output_format = gr.Dropdown(["png", "jpg", "tiff", "bmp", "webp", "gif", "apng"], label="Image Output Format", info="Choose the desired output format for the image file.")
                    change_resolution = gr.Checkbox(label="Change Resolution?")
                    image_width = gr.Number(label="Image Width", value=1024, visible=False)
                    image_height = gr.Number(label="Image Height", value=768, visible=False)
                    
                convert_image_butt = gr.Button(value='Convert Image', variant='primary')
                image_output = gr.File(label="Download Converted Image")
                
        convert_image_butt.click(fn=convert_image, inputs=[image_input, image_output_format, change_resolution, image_width, image_height], outputs=image_output)
        with gr.Tab(('')):
            gr.Markdown("•••---•••")
    app.queue(max_size=1022).launch(share=False)


# Create the Gradio interface
main()