FarhadMadadzade commited on
Commit
4c56b36
1 Parent(s): 43e6a3b

added error handling

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -13,36 +13,40 @@ pipe = pipeline("automatic-speech-recognition", model="Artanis1551/whisper_swedi
13
 
14
 
15
  def process_video1(date):
16
- video_path = download_video1(date)
 
17
 
18
- # Get the duration of the video
19
- video = VideoFileClip(video_path)
20
- duration = video.duration
21
 
22
- # If the video is longer than 30 seconds, only take the first 30 seconds
23
- if duration > 30:
24
- video_path = f"short_{date}.mp4"
25
- ffmpeg_extract_subclip(video_path, 0, 30, targetname=video_path)
26
 
27
- # Extract audio from the video
28
- audio_path = f"audio_{date}.wav"
29
- AudioFileClip(video_path).write_audiofile(audio_path)
30
 
31
- # Split the audio into chunks
32
- audio = AudioSegment.from_wav(audio_path)
33
- chunks = split_on_silence(audio, min_silence_len=500, silence_thresh=-40)
34
 
35
- # Transcribe each chunk
36
- transcription = ""
37
- for i, chunk in enumerate(chunks):
38
- chunk.export(f"chunk{i}.wav", format="wav")
39
- with open(f"chunk{i}.wav", "rb") as audio_file:
40
- audio = audio_file.read()
41
- transcription += pipe(audio)["text"] + "\n "
42
- os.remove(f"chunk{i}.wav")
43
 
44
- # Remove the audio file
45
- os.remove(audio_path)
 
 
 
46
 
47
  return video_path, transcription
48
 
 
13
 
14
 
15
  def process_video1(date):
16
+ try:
17
+ video_path = download_video1(date)
18
 
19
+ # Get the duration of the video
20
+ video = VideoFileClip(video_path)
21
+ duration = video.duration
22
 
23
+ # If the video is longer than 30 seconds, only take the first 30 seconds
24
+ if duration > 30:
25
+ video_path = f"short_{date}.mp4"
26
+ ffmpeg_extract_subclip(video_path, 0, 30, targetname=video_path)
27
 
28
+ # Extract audio from the video
29
+ audio_path = f"audio_{date}.wav"
30
+ AudioFileClip(video_path).write_audiofile(audio_path)
31
 
32
+ # Split the audio into chunks
33
+ audio = AudioSegment.from_wav(audio_path)
34
+ chunks = split_on_silence(audio, min_silence_len=500, silence_thresh=-40)
35
 
36
+ # Transcribe each chunk
37
+ transcription = ""
38
+ for i, chunk in enumerate(chunks):
39
+ chunk.export(f"chunk{i}.wav", format="wav")
40
+ with open(f"chunk{i}.wav", "rb") as audio_file:
41
+ audio = audio_file.read()
42
+ transcription += pipe(audio)["text"] + "\n "
43
+ os.remove(f"chunk{i}.wav")
44
 
45
+ # Remove the audio file
46
+ os.remove(audio_path)
47
+ except:
48
+ video_path = ""
49
+ transcription = "No decision was made on this date."
50
 
51
  return video_path, transcription
52