Keane Moraes commited on
Commit
ad2cac9
·
1 Parent(s): 6d16da3

added 16k sampling for faster transcription

Browse files
Files changed (1) hide show
  1. transcription.py +30 -21
transcription.py CHANGED
@@ -69,12 +69,21 @@ class DownloadAudio:
69
  current_dir = os.getcwd()
70
  print(current_dir)
71
  executable_path = os.path.join(current_dir, "exec/yt-dlp_linux")
 
72
  # Download the video as an audio file using youtube-dl
73
- result = subprocess.run([executable_path, "-x", "--audio-format", "wav", "-o", FINAL_WAV_PATH, self.link])
 
74
  if result.returncode != 0:
75
  print("Failed to download audio. Retrying...")
76
  return "FAILED"
77
 
 
 
 
 
 
 
 
78
  # Load the input .wav file
79
  audio = AudioSegment.from_wav(FINAL_WAV_PATH)
80
 
@@ -86,26 +95,26 @@ class DownloadAudio:
86
  if total_byte_size < MAX_FILE_SIZE_BYTES:
87
  return FINAL_WAV_PATH
88
 
89
- # Get the size of the wav file
90
- channels = audio.channels
91
- sample_width = audio.sample_width
92
- duration_in_sec = math.ceil(len(audio) / 1000)
93
- sample_rate = audio.frame_rate
94
- bit_rate = sample_width * 8
95
- wav_file_size = (sample_rate * bit_rate * channels * duration_in_sec) / 8
96
-
97
- # Get the length of each chunk in milliseconds and make the chunks
98
- chunk_length_in_sec = math.ceil((duration_in_sec * MAX_FILE_SIZE_BYTES ) / wav_file_size) #in sec
99
- chunk_length_ms = chunk_length_in_sec * 1000
100
- chunks = make_chunks(audio, chunk_length_ms)
101
-
102
- # Export all of the individual chunks as wav files
103
- chunk_names = []
104
- for i, chunk in enumerate(chunks):
105
- chunk_name = f"{self.YOUTUBE_VIDEO_ID}_{i}.wav"
106
- output_chunk_path = f"{pathname}/{chunk_name}"
107
- chunk_names.append(output_chunk_path)
108
- chunk.export(f"{output_chunk_path}", format="wav")
109
 
110
  return FINAL_WAV_PATH
111
 
 
69
  current_dir = os.getcwd()
70
  print(current_dir)
71
  executable_path = os.path.join(current_dir, "exec/yt-dlp_linux")
72
+
73
  # Download the video as an audio file using youtube-dl
74
+ original_download_path = f"{pathname}/audio.wav"
75
+ result = subprocess.run([executable_path, "-x", "--audio-format", "wav", "-o", original_download_path, self.link])
76
  if result.returncode != 0:
77
  print("Failed to download audio. Retrying...")
78
  return "FAILED"
79
 
80
+ sound = AudioSegment.from_wav(original_download_path)
81
+ sound.set_channels(1)
82
+ sound = sound.set_frame_rate(16000)
83
+ sound = sound.set_channels(1)
84
+ sound.export(FINAL_WAV_PATH, format="wav")
85
+ os.remove(original_download_path)
86
+
87
  # Load the input .wav file
88
  audio = AudioSegment.from_wav(FINAL_WAV_PATH)
89
 
 
95
  if total_byte_size < MAX_FILE_SIZE_BYTES:
96
  return FINAL_WAV_PATH
97
 
98
+ # # Get the size of the wav file
99
+ # channels = audio.channels
100
+ # sample_width = audio.sample_width
101
+ # duration_in_sec = math.ceil(len(audio) / 1000)
102
+ # sample_rate = audio.frame_rate
103
+ # bit_rate = sample_width * 8
104
+ # wav_file_size = (sample_rate * bit_rate * channels * duration_in_sec) / 8
105
+
106
+ # # Get the length of each chunk in milliseconds and make the chunks
107
+ # chunk_length_in_sec = math.ceil((duration_in_sec * MAX_FILE_SIZE_BYTES ) / wav_file_size) #in sec
108
+ # chunk_length_ms = chunk_length_in_sec * 1000
109
+ # chunks = make_chunks(audio, chunk_length_ms)
110
+
111
+ # # Export all of the individual chunks as wav files
112
+ # chunk_names = []
113
+ # for i, chunk in enumerate(chunks):
114
+ # chunk_name = f"{self.YOUTUBE_VIDEO_ID}_{i}.wav"
115
+ # output_chunk_path = f"{pathname}/{chunk_name}"
116
+ # chunk_names.append(output_chunk_path)
117
+ # chunk.export(f"{output_chunk_path}", format="wav")
118
 
119
  return FINAL_WAV_PATH
120