-
Notifications
You must be signed in to change notification settings - Fork 2
/
vid2aud.py
executable file
·32 lines (28 loc) · 1.05 KB
/
vid2aud.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
#!/usr/bin/env python
import sys
import subprocess
import json
import pathlib
probeargs = ['ffprobe', '-loglevel', 'quiet', '-hide_banner', '-of', 'json', '-show_streams', '-i', '']
convertargs = ['ffmpeg', '-i', '', '-map', '0:a:0', '-c:a', 'copy', '']
outdir = pathlib.Path("music")
outdir.mkdir(exist_ok=True)
for item in sys.argv[1:]:
probeargs[-1] = item
proc = subprocess.run(probeargs, stdout=subprocess.PIPE)
iteminfo = json.loads(proc.stdout)
newname = pathlib.Path(pathlib.Path(item).name)
for stream in iteminfo['streams']:
if stream['codec_type'] == 'audio':
codec = stream['codec_name']
if codec == 'opus' or codec == 'vorbis':
newname = newname.with_suffix(".ogg")
elif codec == 'aac':
newname = newname.with_suffix(".m4a")
break
outfile = outdir / newname
print("Filename: {} Codec: {}".format(item, codec))
print("New File: {}".format(outfile))
convertargs[2] = item
convertargs[-1] = outfile
subprocess.run(convertargs)