Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating length.json #10

Open
danmathewsrobin opened this issue Oct 9, 2023 · 2 comments
Open

Creating length.json #10

danmathewsrobin opened this issue Oct 9, 2023 · 2 comments

Comments

@danmathewsrobin
Copy link

how to properly create length.json file?

@goldiusleonard
Copy link

goldiusleonard commented Jan 18, 2024

Here is my script to create the length.json file. Just sepcify the data_root and the json_path to output the length.json file. @danmathewsrobin

import json

from pathlib import Path
from argparse import ArgumentParser
from tqdm import tqdm
from soundfile import SoundFile


def _parse_args():
    parser = ArgumentParser()

    parser.add_argument(
        "--data_root",
        type=str,
    )
    parser.add_argument(
        "--json_path",
        type=str,
    )
    args = parser.parse_args()
    return args


def _get_audio_paths(
    data_root: str, audio_exts: list = [".wav", ".flac", ".mp3", ".aac"]
):
    audio_paths = []

    for audio_ext in audio_exts:
        audio_paths.extend(list(Path(data_root).rglob(f"*{audio_ext}")))

    return list(set(audio_paths))


def _main():
    args = _parse_args()

    audio_paths = _get_audio_paths(args.data_root)

    json_length = {}

    for audio_path in tqdm(audio_paths):
        sn = SoundFile(str(audio_path)).frames
        audio_path_cut = Path(
            *Path(audio_path).parts[len(Path(args.data_root).parts) :]
        )
        audio_path_cut = audio_path_cut.parent / audio_path_cut.stem

        json_length[str(audio_path_cut).replace("\\", "/")] = sn

    Path(args.json_path).parent.mkdir(parents=True, exist_ok=True)

    with open(args.json_path, "w") as f:
        json.dump(json_length, f)


if __name__ == "__main__":
    _main()

@arjunbahuguna
Copy link

@danmathewsrobin you can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants