Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
+
from moviepy.editor import VideoFileClip
|
| 4 |
+
import librosa
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Example function to manipulate audio
|
| 8 |
+
def process_audio(file, instructions):
|
| 9 |
+
audio = AudioSegment.from_file(file)
|
| 10 |
+
|
| 11 |
+
# Parse instructions for manipulation (e.g., extend, overlap, etc.)
|
| 12 |
+
if "extend" in instructions:
|
| 13 |
+
# Extend the audio by repeating it (or other methods)
|
| 14 |
+
duration = int(instructions.split("extend")[1].strip().split()[0]) * 1000 # convert to ms
|
| 15 |
+
audio = audio + audio[:duration] # Extend audio by repeating part
|
| 16 |
+
|
| 17 |
+
if "overlap" in instructions:
|
| 18 |
+
# Overlap part of the audio at the specified time
|
| 19 |
+
overlap_time = int(instructions.split("overlap")[1].strip().split()[0]) * 1000 # convert to ms
|
| 20 |
+
overlap_audio = audio[overlap_time:] # Get part to overlap
|
| 21 |
+
audio = audio.overlay(overlap_audio, position=overlap_time)
|
| 22 |
+
|
| 23 |
+
# Save modified audio to output file
|
| 24 |
+
output_path = "/tmp/modified_audio.wav"
|
| 25 |
+
audio.export(output_path, format="wav")
|
| 26 |
+
|
| 27 |
+
return output_path
|
| 28 |
+
|
| 29 |
+
def process_video(file, instructions):
|
| 30 |
+
video = VideoFileClip(file)
|
| 31 |
+
audio = video.audio
|
| 32 |
+
audio_path = "/tmp/temp_audio.wav"
|
| 33 |
+
audio.write_audiofile(audio_path)
|
| 34 |
+
|
| 35 |
+
# Process the audio the same way as above
|
| 36 |
+
processed_audio_path = process_audio(audio_path, instructions)
|
| 37 |
+
|
| 38 |
+
# Replace audio in video
|
| 39 |
+
processed_audio = AudioSegment.from_wav(processed_audio_path)
|
| 40 |
+
video.set_audio(processed_audio)
|
| 41 |
+
|
| 42 |
+
output_path = "/tmp/modified_video.mp4"
|
| 43 |
+
video.write_videofile(output_path)
|
| 44 |
+
|
| 45 |
+
return output_path
|
| 46 |
+
|
| 47 |
+
def process_upload(file, instructions):
|
| 48 |
+
if file.name.endswith(".mp4") or file.name.endswith(".mov"):
|
| 49 |
+
return process_video(file, instructions)
|
| 50 |
+
else:
|
| 51 |
+
return process_audio(file, instructions)
|
| 52 |
+
|
| 53 |
+
# Gradio interface
|
| 54 |
+
iface = gr.Interface(
|
| 55 |
+
fn=process_upload,
|
| 56 |
+
inputs=[gr.File(label="Upload Audio/Video File"), gr.Textbox(label="Instructions (e.g., extend, overlap, etc.)")],
|
| 57 |
+
outputs=gr.File(label="Download Processed File"),
|
| 58 |
+
live=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
iface.launch()
|