| import sys |
| import os |
| import re |
| import gc |
| import lhotse |
|
|
| def main(): |
| if len(sys.argv) != 3: |
| print("Usage: python process_single_chunk.py <base_output_dir> <manifest_path>") |
| sys.exit(1) |
|
|
| base_output_dir = sys.argv[1] |
| tiny_manifest_path = sys.argv[2] |
|
|
| |
| match = re.search(r'manifest_(\d+)', os.path.basename(tiny_manifest_path)) |
| if match: |
| chunk_idx = int(match.group(1)) |
| else: |
| print(f"[Worker] Could not extract chunk index from {tiny_manifest_path}. Aborting.") |
| sys.exit(1) |
|
|
| |
| from lhotse.audio.backend import ( |
| CompositeAudioBackend, LibsndfileBackend, TorchaudioFFMPEGBackend |
| ) |
| from lhotse import set_caching_enabled |
| set_caching_enabled(False) |
| gc.set_threshold(100, 2, 2) |
| |
| composite = CompositeAudioBackend(backends=[ |
| LibsndfileBackend(), TorchaudioFFMPEGBackend() |
| ]) |
| lhotse.audio.set_current_audio_backend(composite) |
|
|
| |
| output_dir = os.path.join(base_output_dir, f"chunk_{chunk_idx:04d}") |
| os.makedirs(output_dir, exist_ok=True) |
| print(f"[Worker] Starting Chunk {chunk_idx} using {os.path.basename(tiny_manifest_path)}") |
|
|
| try: |
| |
| chunk_cutset = lhotse.CutSet.from_jsonl(tiny_manifest_path) |
|
|
| |
| chunk_cutset.to_shar( |
| output_dir, |
| fields={"recording": "opus"}, |
| shard_size=10000, |
| num_jobs=1, |
| fault_tolerant=True |
| ) |
| print(f"[Worker] Finished Chunk {chunk_idx}.") |
|
|
| |
| os.remove(tiny_manifest_path) |
| print(f"[Worker] Deleted manifest {os.path.basename(tiny_manifest_path)}") |
|
|
| except Exception as e: |
| print(f"[Worker] FAILED Chunk {chunk_idx}. Manifest kept for retry. Error: {e}") |
| |
| sys.exit(1) |
|
|
| if __name__ == '__main__': |
| main() |
|
|