|
import os |
|
import argparse |
|
from tqdm import tqdm |
|
|
|
START = 00 |
|
FOLDER = "chunks" |
|
DEBUG = True |
|
|
|
def seconds_to_hms(seconds): |
|
hour = 00 |
|
minute = 00 |
|
second = seconds |
|
|
|
while second >= 60: |
|
minute += 1 |
|
second -= 60 |
|
while minute >= 60: |
|
hour += 1 |
|
minute -= 60 |
|
|
|
return hour, minute, second |
|
|
|
def hms_to_seconds(hour, minute, second): |
|
return hour*3600 + minute*60 + second |
|
|
|
def slice_audio(input_audio_path, output_folder, chunks_seconds, chunk_overlap_seconds): |
|
""" |
|
Slice audio into chunks with specified duration and overlap. |
|
|
|
Args: |
|
input_audio_path (str): Path to input audio file |
|
output_folder (str): Path to output folder |
|
chunks_seconds (int): Duration of each chunk in seconds |
|
chunk_overlap_seconds (int): Overlap between chunks in seconds |
|
""" |
|
_, filename = os.path.split(input_audio_path) |
|
name, extension = os.path.splitext(filename) |
|
|
|
|
|
duration = float(os.popen(f'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {input_audio_path}').read()) |
|
hour, minute, second = seconds_to_hms(int(duration)) |
|
print(f"\tDuration ({duration} seconds): {hour:02d}:{minute:02d}:{second:02d}") |
|
|
|
|
|
effective_chunk = chunks_seconds - chunk_overlap_seconds |
|
|
|
|
|
if effective_chunk > 0: |
|
num_chunks = -(-int(duration - chunk_overlap_seconds) // effective_chunk) |
|
else: |
|
raise ValueError("Overlap duration must be less than chunk duration") |
|
|
|
|
|
output_files = [] |
|
progress_bar = tqdm(total=num_chunks, desc="Slice audio into chunks progress") |
|
|
|
for chunk in range(num_chunks): |
|
|
|
start_time = chunk * effective_chunk |
|
end_time = min(start_time + chunks_seconds, duration) |
|
|
|
|
|
hour_start, minute_start, second_start = seconds_to_hms(start_time) |
|
|
|
|
|
chunk_duration = end_time - start_time |
|
hour_duration, minute_duration, second_duration = seconds_to_hms(chunk_duration) |
|
|
|
|
|
output = f"{output_folder}/{name}_chunk{chunk:003d}{extension}" |
|
|
|
if DEBUG: |
|
if os.path.exists(output): |
|
output_files.append(output) |
|
progress_bar.update(1) |
|
continue |
|
|
|
|
|
if chunk == num_chunks - 1: |
|
command = f'ffmpeg -y -i {input_audio_path} -ss {hour_start:02d}:{minute_start:02d}:{second_start:02d} -loglevel error {output}' |
|
else: |
|
command = f'ffmpeg -y -i {input_audio_path} -ss {hour_start:02d}:{minute_start:02d}:{second_start:02d} -t {hour_duration:02d}:{minute_duration:02d}:{second_duration:02d} -loglevel error {output}' |
|
|
|
|
|
os.system(command) |
|
output_files.append(output) |
|
progress_bar.update(1) |
|
|
|
progress_bar.close() |
|
|
|
|
|
with open(f"{output_folder}/output_files.txt", "w") as f: |
|
for output_file in output_files: |
|
f.write(f"{output_file}\n") |
|
|