-
Notifications
You must be signed in to change notification settings - Fork 4
/
change_bitrate.py
54 lines (43 loc) · 1.46 KB
/
change_bitrate.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
# -*- coding: utf-8 -*-
import argparse
import os
import re
import shutil
import subprocess
import sys
from lib.io_utils import *
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="media/downloads/*.mp3", help="Input file pattern")
parser.add_argument('-bitrate', dest="BITRATE", default=128, help="Target bitrate")
parser.add_argument('-out', dest="OUTPUT_DIR", default="media/downloads/128k/", help="Output file pattern")
parser.add_argument('-probe', dest="PROBE", action="store_true", help="Just output details?")
a = parser.parse_args()
files = getFilenames(a.INPUT_FILE)
OUTPUT_DIR = a.OUTPUT_DIR if len(a.OUTPUT_DIR) > 0 else os.path.dirname(a.INPUT_FILE) + "/"
if not a.PROBE:
makeDirectories([OUTPUT_DIR])
hasTmp = False
for i, fn in enumerate(files):
infn = fn.replace("\\", "/")
outfn = OUTPUT_DIR + os.path.basename(infn)
if outfn == infn:
hasTmp = True
outfn = OUTPUT_DIR + "tmp/" + os.path.basename(infn)
if i==0 and not a.PROBE:
makeDirectories([outfn])
command = ['ffmpeg','-y',
'-i', infn,
'-map','0:a:0',
'-b:a', '%sk' % a.BITRATE,
outfn]
print(" ".join(command))
if a.PROBE:
continue
finished = subprocess.check_call(command)
if hasTmp:
os.remove(infn)
shutil.copyfile(outfn, infn)
if hasTmp and not a.PROBE:
shutil.rmtree(OUTPUT_DIR + "tmp")
print("Done.")