This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
generate_audio_info.py
73 lines (58 loc) · 2.1 KB
/
generate_audio_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Create a CSV file with info on all audio files in a directory."""
import argparse
import logging
import os
from pathlib import Path
from typing import Tuple
from pydub.utils import mediainfo_json
from tqdm import tqdm
from utils import files
parser = argparse.ArgumentParser(
description='Create a CSV file with info on all audio files in a directory.')
parser.add_argument(
'-i', '--input-directory', type=str
)
parser.add_argument(
'-o', '--output-directory', type=str
)
args = parser.parse_args()
CSV_HEADERS = ['filename', 'duration', 'size', 'format', 'samplerate', 'channels', 'bits', 'codec']
CSV_FILENAME = 'audio_info.csv'
def check_args() -> Tuple[str, str]:
input_directory = Path(args.input_directory)
if not input_directory.is_dir():
raise ValueError("Input directory is not a valid directory")
output_directory = Path(args.output_directory)
if not output_directory.is_dir():
raise ValueError("Output directory is not a valid directory")
logging.info(
"Parameters\nAudio Directory: {}\nOutput Directory: {}".format(
input_directory, output_directory))
return input_directory, output_directory
def get_entry_from_mediainfo(filepath: str):
info = mediainfo_json(filepath)
stream = info.get('streams')[0]
media_format = info.get('format')
entry = [
filepath,
media_format.get('duration'),
media_format.get('size'),
media_format.get('format_name'),
stream.get('sample_rate'),
stream.get('channels'),
stream.get('bits_per_sample'),
stream.get('codec_name')
]
return entry
def main():
input_directory, output_directory = check_args()
filename_list = files.get_all_files_in_directory(input_directory)
csv_rows = [CSV_HEADERS]
for filename in tqdm(filename_list):
filepath = os.path.join(input_directory, filename)
entry = get_entry_from_mediainfo(filepath)
csv_rows.append(entry)
csv_filename = os.path.join(output_directory, CSV_FILENAME)
files.write_csv(csv_filename, csv_rows)
if __name__ == '__main__':
main()