File size: 2,107 Bytes
8f8f05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from pydub import AudioSegment
from moviepy.editor import VideoFileClip
import librosa
import numpy as np

# Example function to manipulate audio
def process_audio(file, instructions):
    audio = AudioSegment.from_file(file)

    # Parse instructions for manipulation (e.g., extend, overlap, etc.)
    if "extend" in instructions:
        # Extend the audio by repeating it (or other methods)
        duration = int(instructions.split("extend")[1].strip().split()[0]) * 1000  # convert to ms
        audio = audio + audio[:duration]  # Extend audio by repeating part

    if "overlap" in instructions:
        # Overlap part of the audio at the specified time
        overlap_time = int(instructions.split("overlap")[1].strip().split()[0]) * 1000  # convert to ms
        overlap_audio = audio[overlap_time:]  # Get part to overlap
        audio = audio.overlay(overlap_audio, position=overlap_time)

    # Save modified audio to output file
    output_path = "/tmp/modified_audio.wav"
    audio.export(output_path, format="wav")
    
    return output_path

def process_video(file, instructions):
    video = VideoFileClip(file)
    audio = video.audio
    audio_path = "/tmp/temp_audio.wav"
    audio.write_audiofile(audio_path)
    
    # Process the audio the same way as above
    processed_audio_path = process_audio(audio_path, instructions)
    
    # Replace audio in video
    processed_audio = AudioSegment.from_wav(processed_audio_path)
    video.set_audio(processed_audio)
    
    output_path = "/tmp/modified_video.mp4"
    video.write_videofile(output_path)
    
    return output_path

def process_upload(file, instructions):
    if file.name.endswith(".mp4") or file.name.endswith(".mov"):
        return process_video(file, instructions)
    else:
        return process_audio(file, instructions)

# Gradio interface
iface = gr.Interface(
    fn=process_upload,
    inputs=[gr.File(label="Upload Audio/Video File"), gr.Textbox(label="Instructions (e.g., extend, overlap, etc.)")],
    outputs=gr.File(label="Download Processed File"),
    live=True
)

iface.launch()