sub_make1 / app.py
kavehtaheri's picture
Update app.py
3f7a46a verified
from pytubefix import YouTube
from pytubefix.cli import on_progress
import time
import math
import gradio as gr
import ffmpeg
from faster_whisper import WhisperModel
import requests
import json
import arabic_reshaper # pip install arabic-reshaper
from bidi.algorithm import get_display # pip install python-bidi
from moviepy import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip, ImageClip
import pysrt
import instaloader
import time
import concurrent.futures
import re
from io import BytesIO
from PIL import Image
import yt_dlp
api_key = "268976:66f4f58a2a905"
def fetch_data(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
def download_file(url):
try:
response = requests.get(url.split("#")[0], stream=True)
response.raise_for_status()
print(url.split("#")[1])
with open(url.split("#")[1], 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
file.close()
print(f"Downloaded successfully: {url.split('#')[1]}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
def download_chunk(url, start, end, filename, index):
headers = {'Range': f'bytes={start}-{end}'}
response = requests.get(url, headers=headers, stream=True)
response.raise_for_status()
chunk_filename = f'{filename}.part{index}'
with open(chunk_filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
file.close()
return chunk_filename
def merge_files(filename, num_parts):
with open(filename, 'wb') as output_file:
for i in range(num_parts):
part_filename = f'{filename}.part{i}'
with open(part_filename, 'rb') as part_file:
output_file.write(part_file.read())
# Optionally, delete the part file after merging
# os.remove(part_filename)
part_file.close()
def download_file_in_parallel(link, size, num_threads=4):
url = link.split("#")[0]
filename = link.split("#")[1]
print(url+" filename: "+filename)
response = requests.head(url)
#file_size = int(response.headers['Content-Length'])
chunk_size = size // num_threads
ranges = [(i * chunk_size, (i + 1) * chunk_size - 1) for i in range(num_threads)]
ranges[-1] = (ranges[-1][0], size - 1) # Adjust the last range to the end of the file
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [
executor.submit(download_chunk, url, start, end, filename, i)
for i, (start, end) in enumerate(ranges)
]
for future in concurrent.futures.as_completed(futures):
future.result() # Ensure all threads complete
merge_files(filename, num_threads)
print(f'Downloaded successfully: {filename}')
def one_youtube(link):
"""
Downloads a YouTube video to the current directory.
Args:
link (str): The URL of the YouTube video to download.
Returns:
str: The file path of the downloaded video, or None if an error occurs.
"""
ydl_opts = {
'format': 'best', # Downloads the best quality format with both video and audio
'outtmpl': '/tmp/%(title)s.%(ext)s', # Saves the file with its title and extension
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(link, download=False) # Get video info without downloading
file_path = ydl.prepare_filename(info) # Determine the output file path
ydl.download([link]) # Download the video
print(f'Video downloaded successfully to {file_path}')
return file_path
except Exception as e:
raise ValueError(f"Failed to download YouTube video: {str(e)}")
return None
# Define your functions here
def yt_download(url):
yt = YouTube(url)
print(yt.title)
video_path = f"{yt.title}.mp4"
ys = yt.streams.get_highest_resolution()
print(ys)
ys.download()
return video_path, yt.title
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 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 insta_download(permalink):
# Create an instance of Instaloader
L = instaloader.Instaloader()
try:
# Extract the shortcode from the permalink
if "instagram.com/reel/" in permalink:
shortcode = permalink.split("instagram.com/reel/")[-1].split("/")[0]
elif "instagram.com/p/" in permalink:
shortcode = permalink.split("instagram.com/p/")[-1].split("/")[0]
else:
raise ValueError("Invalid permalink format")
# Load the post using the shortcode
post = instaloader.Post.from_shortcode(L.context, shortcode)
# Check if the post is a video
if not post.is_video:
raise ValueError("The provided permalink is not a video.")
# Get the video URL
video_url = post.video_url
# Extract the filename from the URL
filename = video_url.split("/")[-1]
# Remove query parameters
filename = filename.split("?")[0]
# Download the video using requests
response = requests.get(video_url, stream=True)
response.raise_for_status() # Raise an error for bad responses
# Save the content to a file
with open(filename, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded video {filename} successfully.")
return filename
except Exception as e:
print(f"Failed to download video from {permalink}: {e}")
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
# 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
def transcribe(audio):
model = WhisperModel("tiny")
segments, info = model.transcribe(audio)
segments = list(segments)
for segment in segments:
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
return segments
def format_time(seconds):
hours = math.floor(seconds / 3600)
seconds %= 3600
minutes = math.floor(seconds / 60)
seconds %= 60
milliseconds = round((seconds - math.floor(seconds)) * 1000)
seconds = math.floor(seconds)
formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:01d},{milliseconds:03d}"
return formatted_time
def generate_subtitle_file(language, segments, input_video_name):
subtitle_file = f"sub-{input_video_name}.{language}.srt"
text = ""
for index, segment in enumerate(segments):
segment_start = format_time(segment.start)
segment_end = format_time(segment.end)
text += f"{str(index+1)} \n"
text += f"{segment_start} --> {segment_end} \n"
text += f"{segment.text} \n"
text += "\n"
f = open(subtitle_file, "w", encoding='utf8')
f.write(text)
f.close()
return subtitle_file
def read_srt_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
srt_content = file.read()
file.close()
return srt_content
except FileNotFoundError:
print(f"The file {file_path} was not found.")
except Exception as e:
print(f"An error occurred: {e}")
def clean_text(text):
# Remove 'srt ' from the start of each line
# Remove ''' from the start and end
text = re.sub(r"^```|```$", '', text)
text = re.sub(r'^srt', '', text, flags=re.MULTILINE)
return text
def enhance_text(api_key, text, google):
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
# Prepare the request body
request_body = [{
"role": "user",
"content": f"{text} ROLE: Expert translator for Persian motivational anime subtitles. TASK: Translate input English SRT (`[X.XXs -> Y.YYs] English text`) to Persian SRT. REQUIREMENTS: 1. STRICT FORMAT: Output MUST be `[X.XXs -> Y.YYs] Persian Text` per line. 2. TIMESTAMPS: CRITICAL - NEVER change timestamps; copy EXACTLY. 3. TRANSLATION: Create motivational, touching, natural, understandable Persian; capture spirit, avoid literalism. 4. LENGTH: Ensure Persian text is concise & readable in time. AVOID: Format errors, timestamp changes, unnatural translation, overly long text. in respose dont add any thing exept for the srt formated translation.Now translate:"
},]
# Add the API key to the request
headers = {
"one-api-token": api_key,
"Content-Type": "application/json"
}
# Make the POST request
response = requests.post(url, headers=headers, json=request_body)
# Check the response status
if response.status_code == 200:
result = response.json()
clean_text(result["result"][0])
last = clean_text(result["result"][0])
print("result: ")
print(last)
return last
else:
print(f"Error: {response.status_code}, {response.text}")
return None
def translate_text(api_key, source_lang, target_lang, text):
url = "https://api.one-api.ir/translate/v1/google/"
request_body = {"source": source_lang, "target": target_lang, "text": text}
headers = {"one-api-token": api_key, "Content-Type": "application/json"}
response = requests.post(url, headers=headers, json=request_body)
if response.status_code == 200:
result = response.json()
enhanced_text = enhance_text(api_key, text, result['result'])
return enhanced_text
else:
print(f"Error: {response.status_code}, {response.text}")
return None
def write_google(google_translate):
google = "google_translate.srt"
with open(google, 'w', encoding="utf-8") as f:
f.write(google_translate)
f.close()
def time_to_seconds(time_obj):
return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
subtitle_clips = []
for subtitle in subtitles:
start_time = time_to_seconds(subtitle.start) # Add 2 seconds offset
end_time = time_to_seconds(subtitle.end)
duration = end_time - start_time
video_width, video_height = videosize
max_width = video_width * 0.8
max_height = video_height * 0.2
#reshaped_text = arabic_reshaper.reshape(subtitle.text)
#bidi_text = get_display(reshaped_text)
text_clip = TextClip(font, subtitle.text, font_size=fontsize, size=(int(video_width * 0.8), int(video_height * 0.2)) ,text_align="center" ,color=color, method='caption').with_start(start_time).with_duration(duration)
subtitle_x_position = 'center'
subtitle_y_position = video_height * 0.68
text_position = (subtitle_x_position, subtitle_y_position)
subtitle_clips.append(text_clip.with_position(text_position))
return subtitle_clips
def process_video(url, type):
if type=="insta":
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)
input_audio = extract_audio(input_video)
# **********************************************
elif type == "youtube":
input_video = one_youtube(url)
if input_video is None:
raise ValueError("Failed to download YouTube video")
input_audio = extract_audio(input_video)
input_video_name = os.path.splitext(input_video)[0]
# Get the current local time
t = time.localtime()
# Format the time as a string
current_time = time.strftime("%H:%M:%S", t)
print("Current Time =", current_time)
segments = transcribe(audio=input_audio)
language = "fa"
subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
source_language = "en"
target_language = "fa"
srt_string = read_srt_file(subtitle_file)
google_translate = translate_text(api_key, source_language, target_language, srt_string)
write_google(google_translate)
video = VideoFileClip(input_video)
audio = AudioFileClip(input_audio)
video = video.with_audio(audio)
print(video)
subtitles = pysrt.open("google_translate.srt", encoding="utf-8")
output_video_file = input_video_name + '_subtitled' + ".mp4"
subtitle_clips = create_subtitle_clips(subtitles, video.size, 24, 'arial.ttf', 'yellow', False)
final_video = CompositeVideoClip([video] + subtitle_clips)
final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None)
video.close()
audio.close()
print('final')
# Get the current local time
t = time.localtime()
# Format the time as a string
current_time = time.strftime("%H:%M:%S", t)
print("Current Time =", current_time)
# Generate the URL for the file
return output_video_file
def download_file(file_path):
return gr.File.update(file_path)
iface = gr.Interface(fn=process_video, inputs=[gr.Text(),gr.Dropdown(["insta","youtube"])], outputs="file")
iface.launch(debug=True)