SPACERUNNER99 commited on
Commit
8bec4e8
·
verified ·
1 Parent(s): da7713e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -19
app.py CHANGED
@@ -11,6 +11,7 @@ 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
  api_key = "268976:66f4f58a2a905"
15
 
16
 
@@ -95,24 +96,67 @@ def yt_download(url):
95
  ys.download()
96
  return video_path, yt.title
97
 
98
- def insta_download(shortcode, id_1):
99
- url = "https://api.one-api.ir/instagram/v1/post/"
100
- request_body = {"shortcode": shortcode, "id": id_1}
101
- headers = {"one-api-token": api_key, "Content-Type": "application/json"}
102
- response = requests.post(url, headers=headers, json=request_body)
103
- if response.status_code == 200:
104
- result = response.json()
105
- return result['result']
106
- else:
107
- print(f"Error: {response.status_code}, {response.text}")
108
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  def extract_audio(input_video_name):
111
- extracted_audio = f"audio-{input_video_name}.wav"
112
- stream = ffmpeg.input(input_video)
113
- stream = ffmpeg.output(stream, extracted_audio)
114
- ffmpeg.run(stream, overwrite_output=True)
115
- return extracted_audio
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  def transcribe(audio):
118
  model = WhisperModel("tiny")
@@ -198,8 +242,13 @@ def create_subtitle_clips(subtitles, videosize, fontsize, font, color, debug):
198
 
199
  def process_video(url):
200
 
201
- input_video, input_audio = one_youtube(url)
202
- input_video_name = input_video.replace(".mp4", "")
 
 
 
 
 
203
  segments = transcribe(audio=input_audio)
204
  language = "fa"
205
  subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
@@ -223,6 +272,6 @@ def process_video(url):
223
  #def download_file(file_path):
224
  # return gr.File.update(file_path)
225
 
226
- iface = gr.Interface(fn=process_video, inputs=["text"], outputs="file")
227
 
228
  iface.launch(debug=True)
 
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
  api_key = "268976:66f4f58a2a905"
16
 
17
 
 
96
  ys.download()
97
  return video_path, yt.title
98
 
99
+ def insta_download(permalink):
100
+ # Create an instance of Instaloader
101
+ L = instaloader.Instaloader()
102
+
103
+ try:
104
+ # Extract the shortcode from the permalink
105
+ if "instagram.com/reel/" in permalink:
106
+ shortcode = permalink.split("instagram.com/reel/")[-1].split("/")[0]
107
+ elif "instagram.com/p/" in permalink:
108
+ shortcode = permalink.split("instagram.com/p/")[-1].split("/")[0]
109
+ else:
110
+ raise ValueError("Invalid permalink format")
111
+
112
+ # Load the post using the shortcode
113
+ post = instaloader.Post.from_shortcode(L.context, shortcode)
114
+
115
+ # Check if the post is a video
116
+ if not post.is_video:
117
+ raise ValueError("The provided permalink is not a video.")
118
+
119
+ # Get the video URL
120
+ video_url = post.video_url
121
+
122
+ # Extract the filename from the URL
123
+ filename = video_url.split("/")[-1]
124
+ # Remove query parameters
125
+ filename = filename.split("?")[0]
126
+
127
+ # Download the video using requests
128
+ response = requests.get(video_url, stream=True)
129
+ response.raise_for_status() # Raise an error for bad responses
130
+
131
+ # Save the content to a file
132
+ with open(filename, 'wb') as file:
133
+ for chunk in response.iter_content(chunk_size=8192):
134
+ file.write(chunk)
135
+
136
+ print(f"Downloaded video {filename} successfully.")
137
+ return filename
138
+ except Exception as e:
139
+ print(f"Failed to download video from {permalink}: {e}")
140
 
141
  def extract_audio(input_video_name):
142
+ # Define the input video file and output audio file
143
+ mp3_file = "audio.mp3"
144
+
145
+ # Load the video clip
146
+ video_clip = VideoFileClip(input_video_name)
147
+
148
+ # Extract the audio from the video clip
149
+ audio_clip = video_clip.audio
150
+
151
+ # Write the audio to a separate file
152
+ audio_clip.write_audiofile(mp3_file)
153
+
154
+ # Close the video and audio clips
155
+ audio_clip.close()
156
+ video_clip.close()
157
+
158
+ print("Audio extraction successful!")
159
+ return mp3_file
160
 
161
  def transcribe(audio):
162
  model = WhisperModel("tiny")
 
242
 
243
  def process_video(url):
244
 
245
+ if type=="insta":
246
+ input_video=insta_download(url)
247
+ input_video_name = input_video.replace(".mp4", "")
248
+ input_audio = extract_audio(input_video)
249
+ elif type=="youtube":
250
+ input_video, input_audio = one_youtube(url)
251
+ input_video_name = input_video.replace(".mp4", "")
252
  segments = transcribe(audio=input_audio)
253
  language = "fa"
254
  subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
 
272
  #def download_file(file_path):
273
  # return gr.File.update(file_path)
274
 
275
+ iface = gr.Interface(fn=process_video, inputs=["text" ,gr.Dropdown(["insta", "youtube"])], outputs="file")
276
 
277
  iface.launch(debug=True)