Spaces:
Edmond98
/
Running on A100

Afrinetwork7 commited on
Commit
1388ad6
1 Parent(s): 830d524

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -17
app.py CHANGED
@@ -10,10 +10,8 @@ import torch
10
  import librosa
11
  from transformers import Wav2Vec2ForCTC, AutoProcessor
12
  from pathlib import Path
13
- from moviepy.editor import VideoFileClip
14
  import magic # For MIME type detection
15
- import tempfile
16
- import os
17
 
18
  # Import functions from other modules
19
  from asr import transcribe, ASR_LANGUAGES
@@ -46,20 +44,11 @@ def extract_audio(input_bytes):
46
 
47
  if mime_type.startswith('audio/'):
48
  return sf.read(io.BytesIO(input_bytes))
49
- elif mime_type.startswith('video/'):
50
- with tempfile.NamedTemporaryFile(delete=False, suffix='.webm') as temp_file:
51
- temp_file.write(input_bytes)
52
- temp_file_path = temp_file.name
53
-
54
- try:
55
- video = VideoFileClip(temp_file_path)
56
- audio = video.audio
57
- audio_array = audio.to_soundarray()
58
- sample_rate = audio.fps
59
- video.close()
60
- return audio_array, sample_rate
61
- finally:
62
- os.unlink(temp_file_path)
63
  else:
64
  raise ValueError(f"Unsupported MIME type: {mime_type}")
65
 
 
10
  import librosa
11
  from transformers import Wav2Vec2ForCTC, AutoProcessor
12
  from pathlib import Path
 
13
  import magic # For MIME type detection
14
+ from pydub import AudioSegment
 
15
 
16
  # Import functions from other modules
17
  from asr import transcribe, ASR_LANGUAGES
 
44
 
45
  if mime_type.startswith('audio/'):
46
  return sf.read(io.BytesIO(input_bytes))
47
+ elif mime_type.startswith('video/webm'):
48
+ audio = AudioSegment.from_file(io.BytesIO(input_bytes), format="webm")
49
+ audio_array = np.array(audio.get_array_of_samples())
50
+ sample_rate = audio.frame_rate
51
+ return audio_array, sample_rate
 
 
 
 
 
 
 
 
 
52
  else:
53
  raise ValueError(f"Unsupported MIME type: {mime_type}")
54