Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## split a WAV file into 8-minute parts ##
- from pydub import AudioSegment
- import math
- import os
- def split_wav(input_file, output_folder, chunk_length_ms=480000): # 480000 ms = 8 minutes
- # max length with google Speech Recognition: 8 min = 480000ms OK
- # Load the audio file
- audio = AudioSegment.from_wav(input_file)
- # Get the total duration of the audio in milliseconds
- total_duration = len(audio)
- # Calculate the number of chunks
- num_chunks = math.ceil(total_duration / chunk_length_ms)
- # Create output folder if it doesn't exist
- os.makedirs(output_folder, exist_ok=True)
- # Split the audio and export
- for i in range(num_chunks):
- start_time = i * chunk_length_ms
- end_time = min((i + 1) * chunk_length_ms, total_duration)
- chunk = audio[start_time:end_time]
- # Generate output file name
- output_file = os.path.join(output_folder, f"chunk_{i + 1}.wav")
- print(f"Exporting chunk {i + 1} of {num_chunks}...")
- chunk.export(output_file, format="wav")
- print("Splitting complete!")
- # Usage
- input_file = "r:\\sandi.wav"
- output_folder = "r:\\output"
- split_wav(input_file, output_folder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement