Spaces:
Sleeping
Sleeping
| from transcribe import transcribe | |
| from moviepy import VideoFileClip, CompositeVideoClip, ImageClip | |
| from translate import translate | |
| from edite_video import video_edit | |
| import gradio as gr | |
| import requests | |
| import os | |
| from dub import dub | |
| import time | |
| from io import BytesIO | |
| from PIL import Image | |
| api_key = "268976:66f4f58a2a905" | |
| def download_image(url, save_path='downloaded_image.jpg'): | |
| response = requests.get(url) | |
| image = Image.open(BytesIO(response.content)) | |
| image.save(save_path) | |
| return save_path | |
| def extract_audio(input_video_name): | |
| # Define the input video file and output audio file | |
| mp3_file = "audio.mp3" | |
| # Load the video clip | |
| video_clip = VideoFileClip(input_video_name) | |
| # Extract the audio from the video clip | |
| audio_clip = video_clip.audio | |
| duration = audio_clip.duration | |
| print(f"Audio duration: {duration}") | |
| # Write the audio to a separate file | |
| audio_clip.write_audiofile(mp3_file) | |
| # Close the video and audio clips | |
| audio_clip.close() | |
| video_clip.close() | |
| print("Audio extraction successful!") | |
| return mp3_file, duration | |
| def insta_oneapi(url, api_key): | |
| shortcode = url.split("/")[-2] | |
| print(shortcode) | |
| url_one="https://api.one-api.ir/instagram/v1/post/?shortcode="+shortcode | |
| request_body = [{"shortcode": shortcode},] | |
| headers = {"one-api-token": api_key, "Content-Type": "application/json"} | |
| response = requests.get(url_one, headers=headers) | |
| print(response) | |
| if response.status_code == 200: | |
| result = response.json() | |
| try: | |
| time.sleep(10) | |
| response = requests.get(result["result"]['media'][0]["url"], stream=True) | |
| response.raise_for_status() | |
| with open("video.mp4", 'wb') as file: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| if chunk: | |
| file.write(chunk) | |
| file.close() | |
| print(f"Downloaded successfully") | |
| image_url = result["result"]['media'][0]["cover"] | |
| image_file_path = download_image(image_url) | |
| return "video.mp4", image_file_path | |
| except requests.exceptions.RequestException as e: | |
| print(f"An error occurred: {e}") | |
| else: | |
| print(f"Error: {response.status_code}, {response.text}") | |
| return None | |
| def main(url,type): | |
| input_video, image_path=insta_oneapi(url, api_key) | |
| input_video_name = input_video.replace(".mp4", "") | |
| video = VideoFileClip(input_video) | |
| image_clip = ImageClip(image_path).with_duration(1) | |
| # Set the position and size of the image (optional) | |
| image_clip = image_clip.with_position(("center", "center")).resized(height=video.size[1]) | |
| first_video = CompositeVideoClip([video.with_start(1), image_clip]) | |
| input_video = input_video_name+"_cover.mp4" | |
| input_video_name = input_video.replace(".mp4", "") | |
| first_video.write_videofile(input_video, codec="libx264", audio_codec="aac", logger=None) | |
| video.close() | |
| mp3_file, duration = extract_audio(input_video) | |
| srt_list = transcribe(mp3_file) | |
| subtitle_file = translate(srt_list) | |
| output_video_file = video_edit(subtitle_file, input_video, "yellow", "yekan", input_audio="audio.mp3") | |
| os.remove(subtitle_file) | |
| return output_video_file | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Start typing below and then click **Run** to see the progress and final output.") | |
| with gr.Column(): | |
| url = gr.Textbox(label="url", visible=False) | |
| btn = gr.Button("Create") | |
| video_file_output = gr.Video(label="Result Video") | |
| btn.click( | |
| fn=main, | |
| inputs=url, | |
| outputs=video_file_output, | |
| ) | |
| demo.launch(debug=True) | |