Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ from moviepy import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip
|
|
| 13 |
import pysrt
|
| 14 |
import instaloader
|
| 15 |
import time
|
|
|
|
| 16 |
api_key = "268976:66f4f58a2a905"
|
| 17 |
|
| 18 |
|
|
@@ -40,8 +41,51 @@ def download_file(url):
|
|
| 40 |
except requests.exceptions.RequestException as e:
|
| 41 |
print(f"An error occurred: {e}")
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def one_youtube(link):
|
| 45 |
|
| 46 |
# Fetch video ID
|
| 47 |
video_id_url = f"https://one-api.ir/youtube/?token={api_key}&action=getvideoid&link={link}"
|
|
@@ -63,8 +107,15 @@ def one_youtube(link):
|
|
| 63 |
video_name = f'{file_name}.mp4'
|
| 64 |
audio_name = f'{file_name}.mp3'
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if not download_id or not audio_id:
|
| 69 |
return None, None
|
| 70 |
|
|
@@ -81,8 +132,9 @@ def one_youtube(link):
|
|
| 81 |
vid_str=video_link+"#"+video_name
|
| 82 |
audio_str=audio_link+"#"+audio_name
|
| 83 |
# Download video and audio files
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
return video_name, audio_name
|
| 88 |
|
|
@@ -312,7 +364,7 @@ def process_video(url, type):
|
|
| 312 |
input_video_name = input_video.replace(".mp4", "")
|
| 313 |
input_audio = extract_audio(input_video)
|
| 314 |
elif type=="youtube":
|
| 315 |
-
input_video, input_audio = one_youtube(url)
|
| 316 |
input_video_name = input_video.replace(".mp4", "")
|
| 317 |
segments = transcribe(audio=input_audio)
|
| 318 |
language = "fa"
|
|
|
|
| 13 |
import pysrt
|
| 14 |
import instaloader
|
| 15 |
import time
|
| 16 |
+
import concurrent.futures
|
| 17 |
api_key = "268976:66f4f58a2a905"
|
| 18 |
|
| 19 |
|
|
|
|
| 41 |
except requests.exceptions.RequestException as e:
|
| 42 |
print(f"An error occurred: {e}")
|
| 43 |
|
| 44 |
+
def download_chunk(url, start, end, filename, index):
|
| 45 |
+
headers = {'Range': f'bytes={start}-{end}'}
|
| 46 |
+
response = requests.get(url, headers=headers, stream=True)
|
| 47 |
+
response.raise_for_status()
|
| 48 |
+
chunk_filename = f'{filename}.part{index}'
|
| 49 |
+
with open(chunk_filename, 'wb') as file:
|
| 50 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 51 |
+
if chunk:
|
| 52 |
+
file.write(chunk)
|
| 53 |
+
return chunk_filename
|
| 54 |
+
|
| 55 |
+
def merge_files(filename, num_parts):
|
| 56 |
+
with open(filename, 'wb') as output_file:
|
| 57 |
+
for i in range(num_parts):
|
| 58 |
+
part_filename = f'{filename}.part{i}'
|
| 59 |
+
with open(part_filename, 'rb') as part_file:
|
| 60 |
+
output_file.write(part_file.read())
|
| 61 |
+
# Optionally, delete the part file after merging
|
| 62 |
+
# os.remove(part_filename)
|
| 63 |
+
|
| 64 |
+
def download_file_in_parallel(link, size, num_threads=4):
|
| 65 |
+
url = link.split("#")[0]
|
| 66 |
+
filename = link.split("#")[1]
|
| 67 |
+
print(url+" filename: "+filename)
|
| 68 |
+
response = requests.head(url)
|
| 69 |
+
#file_size = int(response.headers['Content-Length'])
|
| 70 |
+
chunk_size = size // num_threads
|
| 71 |
+
|
| 72 |
+
ranges = [(i * chunk_size, (i + 1) * chunk_size - 1) for i in range(num_threads)]
|
| 73 |
+
ranges[-1] = (ranges[-1][0], size - 1) # Adjust the last range to the end of the file
|
| 74 |
+
|
| 75 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
|
| 76 |
+
futures = [
|
| 77 |
+
executor.submit(download_chunk, url, start, end, filename, i)
|
| 78 |
+
for i, (start, end) in enumerate(ranges)
|
| 79 |
+
]
|
| 80 |
+
for future in concurrent.futures.as_completed(futures):
|
| 81 |
+
future.result() # Ensure all threads complete
|
| 82 |
+
|
| 83 |
+
merge_files(filename, num_threads)
|
| 84 |
+
print(f'Downloaded successfully: {filename}')
|
| 85 |
+
|
| 86 |
+
|
| 87 |
|
| 88 |
+
def one_youtube(link, api_key):
|
| 89 |
|
| 90 |
# Fetch video ID
|
| 91 |
video_id_url = f"https://one-api.ir/youtube/?token={api_key}&action=getvideoid&link={link}"
|
|
|
|
| 107 |
video_name = f'{file_name}.mp4'
|
| 108 |
audio_name = f'{file_name}.mp3'
|
| 109 |
|
| 110 |
+
for f in formats_list:
|
| 111 |
+
if f["format_note"] == "360p":
|
| 112 |
+
download_id = f["id"]
|
| 113 |
+
video_size = f["filesize"]
|
| 114 |
+
for f in formats_list:
|
| 115 |
+
if f["format_note"] == "medium":
|
| 116 |
+
audio_id = f["id"]
|
| 117 |
+
audio_size = f["filesize"]
|
| 118 |
+
|
| 119 |
if not download_id or not audio_id:
|
| 120 |
return None, None
|
| 121 |
|
|
|
|
| 132 |
vid_str=video_link+"#"+video_name
|
| 133 |
audio_str=audio_link+"#"+audio_name
|
| 134 |
# Download video and audio files
|
| 135 |
+
print(video_size , audio_size)
|
| 136 |
+
download_file_in_parallel(vid_str, video_size)
|
| 137 |
+
download_file_in_parallel(audio_str, audio_size)
|
| 138 |
|
| 139 |
return video_name, audio_name
|
| 140 |
|
|
|
|
| 364 |
input_video_name = input_video.replace(".mp4", "")
|
| 365 |
input_audio = extract_audio(input_video)
|
| 366 |
elif type=="youtube":
|
| 367 |
+
input_video, input_audio = one_youtube(url, api_key)
|
| 368 |
input_video_name = input_video.replace(".mp4", "")
|
| 369 |
segments = transcribe(audio=input_audio)
|
| 370 |
language = "fa"
|