-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_postprocess.py
More file actions
executable file
·62 lines (45 loc) · 1.79 KB
/
preview_postprocess.py
File metadata and controls
executable file
·62 lines (45 loc) · 1.79 KB
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
#!/usr/bin/env python3
import argparse
import glob
import os
import subprocess
from ffmpeg_py import FilterGraph, Compressor, FadeOut
__author__ = "Graeme Urquhart"
__version__ = "0.1.0"
__description__ = "Audio file preview generator (ffmpeg)"
def main(cli_args):
# process specified files
for path in cli_args.paths:
create_preview(path)
# if no files specified, recursively find aif files and process them
if not cli_args.paths:
for path in glob.iglob('./**/*.aif', recursive=True):
create_preview(path)
def create_preview(path):
"""Create 2 second preview from input audio file, add compressor, fade-out, and convert to ogg format."""
# build .ogg output file path
path_no_ext, _ = os.path.splitext(path)
output_path = f'{path_no_ext}.ogg'
filters = FilterGraph(
Compressor(ratio=8),
FadeOut()
)
# [ffmpeg binary, *arguments, output path]
command = ['/usr/local/bin/ffmpeg', # ffmpeg executable
'-i', path, # input file
'-ss', '0', '-t', '2', # max duration 2s
'-af', f'{filters}', # audio filtergraph
'-c:a', 'libvorbis', # convert to ogg, with libvorbis codec
output_path]
subprocess.run(args=command)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__description__)
# Specify argument for input file paths
parser.add_argument("paths", nargs="*", help="Audio files to be processed")
# Specify output of "--version"
parser.add_argument(
"--version",
action="version",
version=f"%(prog)s (version {__version__})"
)
main(cli_args=parser.parse_args())