File size: 1,029 Bytes
bb46cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess

from .error import SubprocessError


class FfmpegInvalidURLError(Exception):
    """
    Exception raised when a 4XX or 5XX error is returned when making a request
    """

    def __init__(self, url, error, *args):
        self.url = url
        self.error = error
        msg = 'Got error when making request to "{}": {}'.format(url, error)
        super(FfmpegInvalidURLError, self).__init__(msg, *args)


def ffmpeg_load(url: str, save_path: str) -> str:

    def run(cmd):
        proc = subprocess.Popen(
            cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        return_code = proc.returncode

        if return_code != 0:
            raise SubprocessError(
                cmd, return_code, stdout.decode(), stderr.decode())
        return return_code

    command = ['ffmpeg', '-n', '-i', url, '-t', '10', '-f', 'mp4',
               '-r', '30', '-vcodec', 'h264', save_path, '-loglevel', 'error']
    code = run(command)
    return code