|
import csv |
|
import os |
|
|
|
import pysrt |
|
from pydub import AudioSegment |
|
|
|
|
|
def srt_to_segments_with_metadata(srt_file, episode, audio_file, output_dir, metadata_file): |
|
|
|
subs = pysrt.open(srt_file) |
|
|
|
|
|
audio = AudioSegment.from_wav(audio_file) |
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True) |
|
|
|
|
|
with open(metadata_file, mode='a', newline='', encoding='utf-8') as csvfile: |
|
csvwriter = csv.writer(csvfile) |
|
|
|
|
|
|
|
for index, sub in enumerate(subs): |
|
|
|
start_time = sub.start.ordinal |
|
end_time = sub.end.ordinal |
|
|
|
|
|
duration = (end_time - start_time) / 1000 |
|
|
|
|
|
segment = audio[start_time:end_time] |
|
|
|
|
|
segment_filename = f"{episode}_{index + 1:03d}.wav" |
|
segment_id = f"{episode}_{index + 1:03d}" |
|
audio_filename = os.path.join(output_dir, segment_filename) |
|
|
|
|
|
segment.export(audio_filename, format="wav") |
|
|
|
|
|
csvwriter.writerow( |
|
[segment_id, episode, f'{episode}/{segment_filename}', f'{duration:.3f}', sub.text]) |
|
|
|
print("Segmentation complete. Files saved to:", output_dir) |
|
print("Metadata saved to:", metadata_file) |
|
|
|
|
|
if __name__ == "__main__": |
|
metadata_file = 'full.csv' |
|
|
|
|
|
for episode in range(110, 111): |
|
episode = f'{episode:03d}' |
|
|
|
srt_file = f'srt/{episode}.srt' |
|
audio_file = f'{episode}.wav' |
|
output_dir = f'./wav/{episode}' |
|
srt_to_segments_with_metadata( |
|
srt_file, episode, audio_file, output_dir, metadata_file) |
|
|