forked from wilhelmy/ati
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subprocesses.py
52 lines (42 loc) · 1.7 KB
/
subprocesses.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
# ffmpeg wrapper library. May raise exceptions™
import subprocess, json
FFPROBE_PATH = "ffprobe"
YOUTUBE_UPLOAD_PATH = "youtube-upload"
class Miscommunication(Exception):
pass
def talk_to_process(cmd):
"""Wrapper around subprocess.call for synchronous, input-less command execution"""
sub = subprocess.Popen(cmd,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
out, err = sub.communicate()
if sub.returncode == 0:
return out
else:
raise Miscommunication("You know, everybody always just talks past each other. :(")
def probe(filename):
"""Call an ffprobe binary and load the resulting json"""
# Raises an exception in case of invalid JSON or miscommunication.
return json.loads(talk_to_process(
[FFPROBE_PATH, filename, "-print_format", "json", "-show_streams"]
))
def ffprobe(filename):
"""returns audio and video streams separately"""
p = probe(filename)['streams']
return [ v for v in p if v['codec_type'] == 'audio' ],\
[ v for v in p if v['codec_type'] == 'video' ]
def ffmerge(file1, file2):
"""merges two files via ffmpeg into one"""
pass
def youtube_upload(videopath, title, description=None, recording_date=None, public=False):
privacy = "public" if public else "unlisted"
cmd = [YOUTUBE_UPLOAD_PATH,
"--title", title,
"--privacy", privacy]
if description:
cmd += ["--description", description]
if recording_date:
cmd += ["--recording-date", recording_date]
cmd.append(videopath)
return talk_to_process(cmd).decode("ascii").splitlines()[0]