File size: 2,926 Bytes
d8237e6
 
6f76902
aec01ff
d8237e6
4b959ff
0fdf15f
 
00111e4
 
 
 
4b959ff
e1a05e8
67bbbbc
 
e1a05e8
d8237e6
00111e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aec01ff
d8237e6
00111e4
 
 
 
 
 
 
6f76902
 
 
00111e4
 
 
 
4b959ff
00111e4
d0e5ac1
d8237e6
 
 
 
4b959ff
d8237e6
aec01ff
d8237e6
4b959ff
 
d8237e6
4b959ff
009280d
4b959ff
 
d8237e6
 
 
 
4b959ff
 
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
import gradio as gr
from PIL import Image
import os
import ffmpeg

# Atur tema Gradio
gr.themes.Soft()

def delete_temp_dir(directory, delay=900):  # 15 minutes = 900 seconds
    timer = threading.Timer(delay, shutil.rmtree, [directory])
    timer.start()
    
# Format gambar yang didukung
supported_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
                                                  'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
                                                  'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
                                                  'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF']

CACHE_DIR = tempfile.mkdtemp()
delete_temp_dir(CACHE_DIR, delay=900)  # Clean up cache after 15 minutes

def sanitize_filename(filename):
    """Sanitize filename by removing special characters and spaces."""
    return re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)

def clean_cache():
    """Remove files in the cache directory older than 15 minutes."""
    now = time.time()
    for file in os.listdir(CACHE_DIR):
        file_path = os.path.join(CACHE_DIR, file)
        if os.path.isfile(file_path) and now - os.path.getmtime(file_path) > 900:  # 15 minutes
            os.remove(file_path)
            
def convert_image_ffmpeg(image, target_format):
    try:
        # Create a temporary directory for the uploaded file
        temp_dir = tempfile.mkdtemp()

        # Sanitize the filename and save the uploaded video to the temp directory
        base_name = os.path.splitext(os.path.basename(image.name))[0]
        sanitized_base_name = sanitize_filename(base_name)
        image_path = os.path.join(temp_dir, f"{sanitized_base_name}.png")  # Saving as mp4 by default for now
        
        # Nama file keluaran dengan awalan yang diinginkan
        output_file = f"flowly_ai_image_converter_{base_name}.{target_format.lower()}"

        with open(image_path, "wb") as f:
            f.write(image.getbuffer())  # Save the uploaded video to a local file
            
        # Gunakan ffmpeg untuk konversi
        ffmpeg.input(image_path).output(output_file, format=target_format).overwrite_output().run(capture_stdout=True, capture_stderr=True)

        return output_file
    except Exception as e:
        return f"Error: {e}"

# Antarmuka Gradio
interface = gr.Interface(
    fn=convert_image_ffmpeg,
    inputs=[
        gr.Image(label="Upload Image", type="filepath", height=512),  # Input gambar
        gr.Dropdown(label="Select Target Format", choices=supported_formats)  # Pilih format
    ],
    outputs=gr.File(label="Converted Image"),  # Output file hasil
    title="Image Format Converter",
    description="Upload an image and select any target format for conversion using FFmpeg.",
    css="footer {visibility: hidden}"  # Sembunyikan footer
)

# Jalankan aplikasi
if __name__ == "__main__":
    interface.launch()