-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathffmpeg.py
33 lines (26 loc) · 1.09 KB
/
ffmpeg.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
import subprocess
import os
class FFmpeg:
def __init__(self, ffmpeg_bin='ffmpeg'):
self.ffmpeg_bin = ffmpeg_bin
def extract_segment(self, source, destination, start, stop, timeout=None):
args = ["-i", source,
"-ss", str(start),
"-to", str(stop),
"-f", "wav", destination]
self._run_ffmpeg(args, destination=destination, timeout=timeout)
def _run_ffmpeg(self, args, destination=None, timeout=None):
cmd = [self.ffmpeg_bin, "-y"] + args
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
try:
stdout, stderr = proc.communicate(timeout=timeout)
if proc.returncode != 0:
raise RuntimeError("ffmpeg failed:\n\n{}".format(stderr.decode("utf-8")))
except subprocess.TimeoutExpired as e:
if destination:
try:
os.remove(destination)
except OSError:
pass
raise
return stdout, stderr