SPACERUNNER99 commited on
Commit
0835fb6
·
verified ·
1 Parent(s): f285624

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -376
app.py CHANGED
@@ -1,394 +1,54 @@
1
- from pytubefix import YouTube
2
- from pytubefix.cli import on_progress
3
- import time
4
- import math
5
- import gradio as gr
6
- import ffmpeg
7
- from faster_whisper import WhisperModel
8
- import requests
9
- import json
10
- import arabic_reshaper # pip install arabic-reshaper
11
- from bidi.algorithm import get_display # pip install python-bidi
12
- from moviepy import VideoFileClip, TextClip, CompositeVideoClip, AudioFileClip
13
- import pysrt
14
- import instaloader
15
- import time
16
- import concurrent.futures
17
- api_key = "268976:66f4f58a2a905"
18
 
19
 
20
-
21
-
22
- def fetch_data(url):
23
- try:
24
- response = requests.get(url)
25
- response.raise_for_status()
26
- return response.json()
27
- except requests.exceptions.RequestException as e:
28
- print(f"An error occurred: {e}")
29
- return None
30
-
31
- def download_file(url):
32
- try:
33
- response = requests.get(url.split("#")[0], stream=True)
34
- response.raise_for_status()
35
- print(url.split("#")[1])
36
- with open(url.split("#")[1], 'wb') as file:
37
- for chunk in response.iter_content(chunk_size=8192):
38
- if chunk:
39
- file.write(chunk)
40
- print(f"Downloaded successfully: {url.split('#')[1]}")
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}"
92
- video_data = fetch_data(video_id_url)
93
- if not video_data:
94
- return None, None
95
-
96
- video_id = video_data["result"]
97
-
98
- # Fetch video data
99
- filter_option = "" # Replace with your filter option
100
- video_data_url = f"https://youtube.one-api.ir/?token={api_key}&action=fullvideo&id={video_id}&filter={filter_option}"
101
- video_data_2 = fetch_data(video_data_url)
102
- if not video_data_2:
103
- return None, None
104
-
105
- formats_list = video_data_2["result"]["formats"]
106
- file_name = video_data_2["result"]["title"]
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
-
122
- # Fetch video and audio links
123
- video_link_url = f"https://youtube.one-api.ir/?token={api_key}&action=download&id={download_id}"
124
- audio_link_url = f"https://youtube.one-api.ir/?token={api_key}&action=download&id={audio_id}"
125
- video_link_data = fetch_data(video_link_url)
126
- audio_link_data = fetch_data(audio_link_url)
127
- if not video_link_data or not audio_link_data:
128
- return None, None
129
-
130
- video_link = video_link_data["result"]["link"]
131
- audio_link = audio_link_data["result"]["link"]
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
-
141
-
142
- # Define your functions here
143
- def yt_download(url):
144
- yt = YouTube(url)
145
- print(yt.title)
146
- video_path = f"{yt.title}.mp4"
147
- ys = yt.streams.get_highest_resolution()
148
- print(ys)
149
- ys.download()
150
- return video_path, yt.title
151
-
152
- def insta_oneapi(url, api_key):
153
- shortcode = url.split("/")[-1]
154
- print(shortcode)
155
- url_one="https://api.one-api.ir/instagram/v1/post/?shortcode="+shortcode
156
- request_body = [{"shortcode": shortcode},]
157
- headers = {"one-api-token": api_key, "Content-Type": "application/json"}
158
- response = requests.get(url_one, headers=headers)
159
- print(response)
160
- if response.status_code == 200:
161
-
162
- result = response.json()
163
- try:
164
- time.sleep(10)
165
- response = requests.get(result["result"]['media'][0]["url"], stream=True)
166
- response.raise_for_status()
167
- with open("video.mp4", 'wb') as file:
168
- for chunk in response.iter_content(chunk_size=8192):
169
- if chunk:
170
- file.write(chunk)
171
- print(f"Downloaded successfully")
172
- return "video.mp4"
173
- except requests.exceptions.RequestException as e:
174
- print(f"An error occurred: {e}")
175
- else:
176
- print(f"Error: {response.status_code}, {response.text}")
177
- return None
178
-
179
- def insta_download(permalink):
180
- # Create an instance of Instaloader
181
- L = instaloader.Instaloader()
182
-
183
- try:
184
- # Extract the shortcode from the permalink
185
- if "instagram.com/reel/" in permalink:
186
- shortcode = permalink.split("instagram.com/reel/")[-1].split("/")[0]
187
- elif "instagram.com/p/" in permalink:
188
- shortcode = permalink.split("instagram.com/p/")[-1].split("/")[0]
189
- else:
190
- raise ValueError("Invalid permalink format")
191
-
192
- # Load the post using the shortcode
193
- post = instaloader.Post.from_shortcode(L.context, shortcode)
194
-
195
- # Check if the post is a video
196
- if not post.is_video:
197
- raise ValueError("The provided permalink is not a video.")
198
-
199
- # Get the video URL
200
- video_url = post.video_url
201
-
202
- # Extract the filename from the URL
203
- filename = video_url.split("/")[-1]
204
- # Remove query parameters
205
- filename = filename.split("?")[0]
206
-
207
- # Download the video using requests
208
- response = requests.get(video_url, stream=True)
209
- response.raise_for_status() # Raise an error for bad responses
210
-
211
- # Save the content to a file
212
- with open(filename, 'wb') as file:
213
- for chunk in response.iter_content(chunk_size=8192):
214
- file.write(chunk)
215
-
216
- print(f"Downloaded video {filename} successfully.")
217
- return filename
218
- except Exception as e:
219
- print(f"Failed to download video from {permalink}: {e}")
220
-
221
  def extract_audio(input_video_name):
222
  # Define the input video file and output audio file
223
  mp3_file = "audio.mp3"
224
-
225
  # Load the video clip
226
  video_clip = VideoFileClip(input_video_name)
227
 
228
  # Extract the audio from the video clip
229
  audio_clip = video_clip.audio
230
-
 
231
  # Write the audio to a separate file
232
- audio_clip.write_audiofile(mp3_file)
233
 
234
  # Close the video and audio clips
235
  audio_clip.close()
236
  video_clip.close()
237
 
238
  print("Audio extraction successful!")
239
- return mp3_file
240
-
241
- def transcribe(audio):
242
- model = WhisperModel("tiny")
243
- segments, info = model.transcribe(audio)
244
- segments = list(segments)
245
- for segment in segments:
246
- print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
247
- return segments
248
-
249
- def format_time(seconds):
250
- hours = math.floor(seconds / 3600)
251
- seconds %= 3600
252
- minutes = math.floor(seconds / 60)
253
- seconds %= 60
254
- milliseconds = round((seconds - math.floor(seconds)) * 1000)
255
- seconds = math.floor(seconds)
256
- formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:01d},{milliseconds:03d}"
257
- return formatted_time
258
-
259
- def generate_subtitle_file(language, segments, input_video_name):
260
- subtitle_file = f"sub-{input_video_name}.{language}.srt"
261
- text = ""
262
- for index, segment in enumerate(segments):
263
- segment_start = format_time(segment.start)
264
- segment_end = format_time(segment.end)
265
- text += f"{str(index+1)} \n"
266
- text += f"{segment_start} --> {segment_end} \n"
267
- text += f"{segment.text} \n"
268
- text += "\n"
269
- f = open(subtitle_file, "w", encoding='utf8')
270
- f.write(text)
271
- f.close()
272
- return subtitle_file
273
-
274
- def read_srt_file(file_path):
275
- try:
276
- with open(file_path, 'r', encoding='utf-8') as file:
277
- srt_content = file.read()
278
- return srt_content
279
- except FileNotFoundError:
280
- print(f"The file {file_path} was not found.")
281
- except Exception as e:
282
- print(f"An error occurred: {e}")
283
-
284
- def enhance_text(api_key, text, google):
285
- url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
286
-
287
- # Prepare the request body
288
- request_body = [{
289
- "role": "user",
290
- "content": f" i have a main English text that has been translated, use the main text and the translations to write a better translation of the main text in persian. main text: {text}"
291
- },
292
- {
293
- "role": "assistant",
294
- "content": "ok"
295
- },
296
- {
297
- "role": "user",
298
- "content": f"tarnslation: {google} in response only return the better version of the translation will maintaining the srt format of the text"
299
- },]
300
 
301
- # Add the API key to the request
302
- headers = {
303
- "one-api-token": api_key,
304
- "Content-Type": "application/json"
305
- }
306
-
307
- # Make the POST request
308
- response = requests.post(url, headers=headers, json=request_body)
309
-
310
- # Check the response status
311
- if response.status_code == 200:
312
- result = response.json()
313
- print("result: ")
314
- print(result["result"][0])
315
- return result["result"][0]
316
- else:
317
- print(f"Error: {response.status_code}, {response.text}")
318
- return None
319
-
320
- def translate_text(api_key, source_lang, target_lang, text):
321
- url = "https://api.one-api.ir/translate/v1/google/"
322
- request_body = {"source": source_lang, "target": target_lang, "text": text}
323
- headers = {"one-api-token": api_key, "Content-Type": "application/json"}
324
- response = requests.post(url, headers=headers, json=request_body)
325
- if response.status_code == 200:
326
- result = response.json()
327
- enhanced_text = enhance_text(api_key, text, result['result'])
328
- return enhanced_text
329
- else:
330
- print(f"Error: {response.status_code}, {response.text}")
331
- return None
332
-
333
- def write_google(google_translate):
334
- google = "google_translate.srt"
335
- with open(google, 'w', encoding="utf-8") as f:
336
- f.write(google_translate)
337
-
338
- def time_to_seconds(time_obj):
339
- return time_obj.hours * 3600 + time_obj.minutes * 60 + time_obj.seconds + time_obj.milliseconds / 1000
340
-
341
- def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
342
- subtitle_clips = []
343
- for subtitle in subtitles:
344
- start_time = time_to_seconds(subtitle.start)
345
- end_time = time_to_seconds(subtitle.end)
346
- duration = end_time - start_time
347
- video_width, video_height = videosize
348
- max_width = video_width * 0.8
349
- max_height = video_height * 0.2
350
- #reshaped_text = arabic_reshaper.reshape(subtitle.text)
351
- #bidi_text = get_display(reshaped_text)
352
- 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, bg_color='black', method='caption').with_start(start_time).with_duration(duration)
353
- subtitle_x_position = 'center'
354
- subtitle_y_position = video_height * 0.8
355
- text_position = (subtitle_x_position, subtitle_y_position)
356
- subtitle_clips.append(text_clip.with_position(text_position))
357
- return subtitle_clips
358
-
359
-
360
- def process_video(url, type):
361
-
362
- if type=="insta":
363
- input_video=insta_oneapi(url, api_key)
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"
371
- subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
372
- source_language = "en"
373
- target_language = "fa"
374
- srt_string = read_srt_file(subtitle_file)
375
- google_translate = translate_text(api_key, source_language, target_language, srt_string)
376
- write_google(google_translate)
377
- video = VideoFileClip(input_video)
378
- audio = AudioFileClip(input_audio)
379
- video = video.with_audio(audio)
380
- print(video)
381
- subtitles = pysrt.open("google_translate.srt", encoding="utf-8")
382
- output_video_file = input_video_name + '_subtitled' + ".mp4"
383
- subtitle_clips = create_subtitle_clips(subtitles, video.size, 24, 'arial.ttf', 'yellow', False)
384
- final_video = CompositeVideoClip([video] + subtitle_clips)
385
- final_video.write_videofile(output_video_file, codec="libx264", audio_codec="aac", logger=None)
386
- print('final')
387
- return output_video_file
388
-
389
- #def download_file(file_path):
390
- # return gr.File.update(file_path)
391
-
392
- iface = gr.Interface(fn=process_video, inputs=["text" ,gr.Dropdown(["insta", "youtube"])], outputs="file")
393
-
394
- iface.launch(debug=True)
 
1
+ from transcribe import transcribe
2
+ from moviepy import *
3
+ from translate import translate
4
+ from edite_video import video_edit
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def extract_audio(input_video_name):
8
  # Define the input video file and output audio file
9
  mp3_file = "audio.mp3"
 
10
  # Load the video clip
11
  video_clip = VideoFileClip(input_video_name)
12
 
13
  # Extract the audio from the video clip
14
  audio_clip = video_clip.audio
15
+ duration = audio_clip.duration
16
+ print(duration)
17
  # Write the audio to a separate file
18
+ audio_clip.write_audiofile("./src/media/"+mp3_file)
19
 
20
  # Close the video and audio clips
21
  audio_clip.close()
22
  video_clip.close()
23
 
24
  print("Audio extraction successful!")
25
+ return mp3_file, duration
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ def main(video):
28
+
29
+ mp3_file, duration = extract_audio(video)
30
+ srt_list = transcribe(mp3_file)
31
+ subtitle_file = translate(srt_list)
32
+ output_video_file = video_edit(subtitle_file, "video.mp4", input_audio= "audio.mp3")
33
+
34
+
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("Start typing below and then click **Run** to see the output.")
37
+ with gr.Column():
38
+ video_file_input = gr.Video(label="Upload Video File")
39
+ clip_type = gr.Dropdown(["auto edit", "default"], label="Clip Type")
40
+ btn = gr.Button("create")
41
+ video_file_output = gr.Video(label="result: ")
42
+ btn.click(fn=main, inputs=[video_file_input, clip_type], outputs=video_file_output)
43
+ """ with gr.Row():
44
+ vid_out = gr.Video()
45
+ srt_file = gr.File()
46
+ btn2 = gr.Button("transcribe")
47
+ gr.on(
48
+ triggers=[btn2.click],
49
+ fn=write_google,
50
+ inputs=out,
51
+ ).then(video_edit, [out, video_path_output, audio_path_output], outputs=[vid_out, srt_file])"""
52
+
53
+
54
+ demo.launch(debug=True)