Spaces:
Build error
Build error
| 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() | |