Advertisement
Guest User

Untitled

a guest
Sep 18th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. ## split a WAV file into 8-minute parts ##
  2. from pydub import AudioSegment
  3. import math
  4. import os
  5. def split_wav(input_file, output_folder, chunk_length_ms=480000): # 480000 ms = 8 minutes
  6. # max length with google Speech Recognition: 8 min = 480000ms OK
  7. # Load the audio file
  8. audio = AudioSegment.from_wav(input_file)
  9.  
  10. # Get the total duration of the audio in milliseconds
  11. total_duration = len(audio)
  12.  
  13. # Calculate the number of chunks
  14. num_chunks = math.ceil(total_duration / chunk_length_ms)
  15.  
  16. # Create output folder if it doesn't exist
  17. os.makedirs(output_folder, exist_ok=True)
  18.  
  19. # Split the audio and export
  20. for i in range(num_chunks):
  21. start_time = i * chunk_length_ms
  22. end_time = min((i + 1) * chunk_length_ms, total_duration)
  23.  
  24. chunk = audio[start_time:end_time]
  25.  
  26. # Generate output file name
  27. output_file = os.path.join(output_folder, f"chunk_{i + 1}.wav")
  28.  
  29. print(f"Exporting chunk {i + 1} of {num_chunks}...")
  30. chunk.export(output_file, format="wav")
  31.  
  32. print("Splitting complete!")
  33. # Usage
  34. input_file = "r:\\sandi.wav"
  35. output_folder = "r:\\output"
  36. split_wav(input_file, output_folder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement