-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.py
More file actions
103 lines (87 loc) · 3.18 KB
/
index.py
File metadata and controls
103 lines (87 loc) · 3.18 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
import subprocess
import oss2
import logging
import json
import os
import time
logging.getLogger("oss2.api").setLevel(logging.ERROR)
logging.getLogger("oss2.auth").setLevel(logging.ERROR)
LOGGER = logging.getLogger()
'''
1. function and bucket locate in same region
2. service's role has OSSFullAccess
3. event format
{
"bucket_name" : "test-bucket",
"object_key" : "a.mp3",
"output_dir" : "output/",
"dst_type": ".wav",
"ac": 1,
"ar": 4000
}
'''
# a decorator for print the excute time of a function
def print_excute_time(func):
def wrapper(*args, **kwargs):
local_time = time.time()
ret = func(*args, **kwargs)
LOGGER.info('current Function [%s] excute time is %.2f seconds' %
(func.__name__, time.time() - local_time))
return ret
return wrapper
def get_fileNameExt(filename):
(fileDir, tempfilename) = os.path.split(filename)
(shortname, extension) = os.path.splitext(tempfilename)
return fileDir, shortname, extension
@print_excute_time
def handler(event, context):
LOGGER.info(event)
evt = json.loads(event)
oss_bucket_name = evt["bucket_name"]
object_key = evt["object_key"]
output_dir = evt["output_dir"]
dst_type = evt["dst_type"]
ac = evt.get("ac")
ar = evt.get("ar")
creds = context.credentials
auth = oss2.StsAuth(creds.accessKeyId,
creds.accessKeySecret, creds.securityToken)
oss_client = oss2.Bucket(
auth, 'oss-%s-internal.aliyuncs.com' % context.region, oss_bucket_name)
exist = oss_client.object_exists(object_key)
if not exist:
raise Exception("object {} is not exist".format(object_key))
input_path = oss_client.sign_url('GET', object_key, 3600)
fileDir, shortname, extension = get_fileNameExt(object_key)
cmd = ['ffmpeg', '-i', input_path,
'/tmp/{0}{1}'.format(shortname, dst_type)]
if ac:
if ar:
cmd = ['ffmpeg', '-i', input_path, "-ac",
str(ac), "-ar", str(ar), '/tmp/{0}{1}'.format(shortname, dst_type)]
else:
cmd = ['ffmpeg', '-i', input_path, "-ac",
str(ac), '/tmp/{0}{1}'.format(shortname, dst_type)]
else:
if ar:
cmd = ['ffmpeg', '-i', input_path, "-ar",
str(ar), '/tmp/{0}{1}'.format(shortname, dst_type)]
LOGGER.info("cmd = {}".format(" ".join(cmd)))
try:
subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
except subprocess.CalledProcessError as exc:
LOGGER.error('returncode:{}'.format(exc.returncode))
LOGGER.error('cmd:{}'.format(exc.cmd))
LOGGER.error('output:{}'.format(exc.output))
LOGGER.error('stderr:{}'.format(exc.stderr))
LOGGER.error('stdout:{}'.format(exc.stdout))
for filename in os.listdir('/tmp/'):
filepath = '/tmp/' + filename
if filename.startswith(shortname):
filekey = os.path.join(output_dir, fileDir, filename)
oss_client.put_object_from_file(filekey, filepath)
os.remove(filepath)
LOGGER.info("Uploaded {} to {}".format(filepath, filekey))
return "ok"