KarthickAdopleAI commited on
Commit
4d9e270
1 Parent(s): 10de3c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -48
app.py CHANGED
@@ -20,9 +20,13 @@ from speechbrain.inference.classifiers import EncoderClassifier
20
  from pydub.silence import split_on_silence
21
  from moviepy.editor import VideoFileClip
22
  import re
 
 
 
23
  nltk.download('punkt')
24
  nltk.download('stopwords')
25
 
 
26
  class VideoAnalytics:
27
  """
28
  Class for performing analytics on videos including transcription, summarization, topic generation,
@@ -165,33 +169,33 @@ class VideoAnalytics:
165
  logging.error(f"Error processing audio: {e}")
166
  return ""
167
 
168
- def transcribe_video(self, vid: str) -> str:
169
  """
170
  Transcribe the audio of the video.
171
  Args:
172
- vid (str): Path to the video file.
173
  Returns:
174
  str: Transcribed text.
175
  """
176
  try:
177
- # Load the video file and extract audio
178
- video = VideoFileClip(vid)
179
- audio = video.audio
180
 
181
- # Write audio to a temporary file
182
- audio.write_audiofile("output_audio.mp3")
183
 
184
 
185
- # Replace 'input.mp3' and 'output.wav' with your file paths
186
- audio_filename = self.mp3_to_wav("output_audio.mp3", 'output.wav')
187
- segments = self.split_audio(audio_filename)
188
  splitted_audio_filename = segments[0].export("segment_for_1_min.wav",format="wav")
189
 
190
  # for detect lang
191
  signal = self.language_id.load_audio(splitted_audio_filename.name)
192
  prediction = self.language_id.classify_batch(signal)
193
  lang = [prediction[3][0].split(":")][0][0]
194
- text = self.get_large_audio_transcription_on_silence(audio_filename,lang)
195
  # Update the transcribed_text attribute with the transcription result
196
  self.transcribed_text = text
197
  # Update the translation text into english_text
@@ -514,34 +518,31 @@ class VideoAnalytics:
514
  except Exception as e:
515
  logging.error(f"Error writing text to file: {e}")
516
 
517
- def Download(self, link: str) -> str:
518
- """
519
- Download a video from YouTube.
520
- Args:
521
- link (str): YouTube video link.
522
- Returns:
523
- str: Path to the downloaded video file.
524
- """
525
  try:
526
- # Initialize YouTube object with the provided link
527
- youtubeObject = YouTube(link)
 
 
 
 
 
 
528
 
529
- # Get the highest resolution stream
530
- youtubeObject = youtubeObject.streams.get_highest_resolution()
531
- try:
532
- # Attempt to download the video
533
- file_name = youtubeObject.download()
534
- return file_name
535
- except:
536
- # Log any errors that occur during video download
537
- logging.info("An error has occurred")
538
 
539
- logging.info("Download is completed successfully")
 
 
 
 
 
 
540
 
541
  except Exception as e:
542
- # Log any errors that occur during initialization of YouTube object
543
- logging.error(f"Error downloading video: {e}")
544
- return ""
545
 
546
  def save_audio_with_gtts(self, text: str, filename: str) -> str:
547
  """
@@ -579,23 +580,23 @@ class VideoAnalytics:
579
  try:
580
  # Download the video if input_path is provided, otherwise use the provided video path
581
  if input_path:
582
- input_path = self.Download(input_path)
583
- video_ = VideoFileClip(input_path)
584
- duration = video_.duration
585
- video_.close()
586
- if round(duration) <= 6*600:
587
- text = self.transcribe_video(input_path)
588
- else:
589
- return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","","",None,None,None
590
  elif video:
591
  video_ = VideoFileClip(video)
592
  duration = video_.duration
593
  video_.close()
594
- if round(duration) <= 6*600:
595
- text = self.transcribe_video(video)
596
- input_path = video
597
- else:
598
- return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","","",None,None,None
599
  overall_summary = ""
600
  # Generate summary, important sentences, and topics
601
  summary = self.generate_video_summary(model)
@@ -661,7 +662,7 @@ class VideoAnalytics:
661
  result = gr.Textbox(label='Answer',lines=10)
662
  submit_btn.click(self.main,[video,yt_link,model_selection],[summary,Important_Sentences,Topics,summary_audio,important_sentence_audio,topics_audio])
663
  question.submit(self.video_qa,[question,model],result)
664
- demo.launch(debug=True)
665
 
666
  if __name__ == "__main__":
667
  video_analytics = VideoAnalytics()
 
20
  from pydub.silence import split_on_silence
21
  from moviepy.editor import VideoFileClip
22
  import re
23
+ from moviepy.editor import AudioFileClip
24
+ import subprocess
25
+
26
  nltk.download('punkt')
27
  nltk.download('stopwords')
28
 
29
+
30
  class VideoAnalytics:
31
  """
32
  Class for performing analytics on videos including transcription, summarization, topic generation,
 
169
  logging.error(f"Error processing audio: {e}")
170
  return ""
171
 
172
+ def transcribe_video(self, audio_path: str) -> str:
173
  """
174
  Transcribe the audio of the video.
175
  Args:
176
+ audio_path (str): Path to the audio file.
177
  Returns:
178
  str: Transcribed text.
179
  """
180
  try:
181
+ # # Load the video file and extract audio
182
+ # video = VideoFileClip(vid)
183
+ # audio = video.audio
184
 
185
+ # # Write audio to a temporary file
186
+ # audio.write_audiofile("output_audio.mp3")
187
 
188
 
189
+ # # Replace 'input.mp3' and 'output.wav' with your file paths
190
+ # audio_filename = self.mp3_to_wav("output_audio.mp3", 'output.wav')
191
+ segments = self.split_audio(audio_path)
192
  splitted_audio_filename = segments[0].export("segment_for_1_min.wav",format="wav")
193
 
194
  # for detect lang
195
  signal = self.language_id.load_audio(splitted_audio_filename.name)
196
  prediction = self.language_id.classify_batch(signal)
197
  lang = [prediction[3][0].split(":")][0][0]
198
+ text = self.get_large_audio_transcription_on_silence(audio_path,lang)
199
  # Update the transcribed_text attribute with the transcription result
200
  self.transcribed_text = text
201
  # Update the translation text into english_text
 
518
  except Exception as e:
519
  logging.error(f"Error writing text to file: {e}")
520
 
521
+ def download_youtube_audio(self,url, output_filename="audio.wav"):
 
 
 
 
 
 
 
522
  try:
523
+ # Step 1: Download the audio using yt-dlp
524
+ audio_filename = "downloaded_audio"
525
+ subprocess.run([
526
+ "yt-dlp",
527
+ "-x", "--audio-format", "mp3", # Extract audio in mp3 format
528
+ "-o", f"{audio_filename}.%(ext)s", # Save the audio with the specified name
529
+ url
530
+ ], check=True)
531
 
532
+ # Step 2: Convert the downloaded audio file to .wav format
533
+ audio = AudioSegment.from_file(f"{audio_filename}.mp3", format="mp3")
534
+ output_path = f"{output_filename}"
 
 
 
 
 
 
535
 
536
+ audio.export(output_path, format="wav")
537
+ print(f"Audio downloaded and saved as {output_path}")
538
+
539
+ # Step 3: Cleanup - remove the original downloaded file if needed
540
+ os.remove(f"{audio_filename}.mp3")
541
+
542
+ return output_path
543
 
544
  except Exception as e:
545
+ print(f"Error: {e}")
 
 
546
 
547
  def save_audio_with_gtts(self, text: str, filename: str) -> str:
548
  """
 
580
  try:
581
  # Download the video if input_path is provided, otherwise use the provided video path
582
  if input_path:
583
+ input_path = self.download_youtube_audio(input_path)
584
+ audio_ = AudioFileClip(input_path)
585
+ duration = audio_.duration
586
+ audio_.close()
587
+ # if round(duration) <= 6*600:
588
+ text = self.transcribe_video(input_path)
589
+ # else:
590
+ # return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","","",None,None,None
591
  elif video:
592
  video_ = VideoFileClip(video)
593
  duration = video_.duration
594
  video_.close()
595
+ # if round(duration) <= 6*600:
596
+ text = self.transcribe_video(video)
597
+ input_path = video
598
+ # else:
599
+ # return "Video Duration Above 10 Minutes,Try Below 10 Minutes Video","","",None,None,None
600
  overall_summary = ""
601
  # Generate summary, important sentences, and topics
602
  summary = self.generate_video_summary(model)
 
662
  result = gr.Textbox(label='Answer',lines=10)
663
  submit_btn.click(self.main,[video,yt_link,model_selection],[summary,Important_Sentences,Topics,summary_audio,important_sentence_audio,topics_audio])
664
  question.submit(self.video_qa,[question,model],result)
665
+ demo.launch()
666
 
667
  if __name__ == "__main__":
668
  video_analytics = VideoAnalytics()