|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import logging |
| 3 | +import oss2 |
| 4 | +import os |
| 5 | +import json |
| 6 | +import subprocess |
| 7 | +import shutil |
| 8 | + |
| 9 | +logging.getLogger("oss2.api").setLevel(logging.ERROR) |
| 10 | +logging.getLogger("oss2.auth").setLevel(logging.ERROR) |
| 11 | +LOGGER = logging.getLogger() |
| 12 | + |
| 13 | + |
| 14 | +def get_fileNameExt(filename): |
| 15 | + (_, tempfilename) = os.path.split(filename) |
| 16 | + (shortname, extension) = os.path.splitext(tempfilename) |
| 17 | + return shortname, extension |
| 18 | + |
| 19 | + |
| 20 | +def handler(environ, start_response): |
| 21 | + context = environ['fc.context'] |
| 22 | + # get request_body |
| 23 | + try: |
| 24 | + request_body_size = int(environ.get('CONTENT_LENGTH', 0)) |
| 25 | + except (ValueError): |
| 26 | + request_body_size = 0 |
| 27 | + event = environ['wsgi.input'].read(request_body_size) |
| 28 | + |
| 29 | + evt = json.loads(event) |
| 30 | + oss_bucket_name = evt["bucket"] |
| 31 | + object_key = evt["object"] |
| 32 | + output_dir = evt["output_dir"] |
| 33 | + dst_format = evt['dst_format'] |
| 34 | + shortname, _ = get_fileNameExt(object_key) |
| 35 | + creds = context.credentials |
| 36 | + auth = oss2.StsAuth(creds.accessKeyId, |
| 37 | + creds.accessKeySecret, creds.securityToken) |
| 38 | + oss_client = oss2.Bucket(auth, 'oss-%s-internal.aliyuncs.com' % |
| 39 | + context.region, oss_bucket_name) |
| 40 | + |
| 41 | + # simplifiedmeta = oss_client.get_object_meta(object_key) |
| 42 | + # size = float(simplifiedmeta.headers['Content-Length']) |
| 43 | + # M_size = round(size / 1024.0 / 1024.0, 2) |
| 44 | + |
| 45 | + input_path = oss_client.sign_url('GET', object_key, 6 * 3600) |
| 46 | + # m3u8 特殊处理 |
| 47 | + rid = context.request_id |
| 48 | + if dst_format == "m3u8": |
| 49 | + return handle_m3u8(rid, oss_client, input_path, shortname, output_dir) |
| 50 | + else: |
| 51 | + return handle_common(rid, oss_client, input_path, shortname, output_dir, dst_format) |
| 52 | + |
| 53 | + |
| 54 | +def handle_m3u8(request_id, oss_client, input_path, shortname, output_dir): |
| 55 | + ts_dir = '/tmp/ts' |
| 56 | + if os.path.exists(ts_dir): |
| 57 | + shutil.rmtree(ts_dir) |
| 58 | + os.mkdir(ts_dir) |
| 59 | + transcoded_filepath = os.path.join('/tmp', shortname + '.ts') |
| 60 | + split_transcoded_filepath = os.path.join( |
| 61 | + ts_dir, shortname + '_%03d.ts') |
| 62 | + cmd1 = ['ffmpeg', '-y', '-i', input_path, '-c:v', |
| 63 | + 'libx264', transcoded_filepath] |
| 64 | + cmd2 = ['ffmpeg', '-y', '-i', transcoded_filepath, '-f', 'segment', |
| 65 | + '-segment_list', os.path.join(ts_dir, 'playlist.m3u8'), '-segment_time', '10', split_transcoded_filepath] |
| 66 | + try: |
| 67 | + subprocess.run( |
| 68 | + cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) |
| 69 | + |
| 70 | + subprocess.run( |
| 71 | + cmd2, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) |
| 72 | + |
| 73 | + for filename in os.listdir(ts_dir): |
| 74 | + filepath = os.path.join(ts_dir, filename) |
| 75 | + filekey = os.path.join(output_dir, shortname, filename) |
| 76 | + oss_client.put_object_from_file(filekey, filepath) |
| 77 | + os.remove(filepath) |
| 78 | + print("Uploaded {} to {}".format(filepath, filekey)) |
| 79 | + |
| 80 | + except subprocess.CalledProcessError as exc: |
| 81 | + # if transcode fail,trigger invoke dest-fail function |
| 82 | + raise Exception(request_id + |
| 83 | + " transcode failure, detail: " + str(exc)) |
| 84 | + |
| 85 | + finally: |
| 86 | + if os.path.exists(ts_dir): |
| 87 | + shutil.rmtree(ts_dir) |
| 88 | + |
| 89 | + # remove ts 文件 |
| 90 | + if os.path.exists(transcoded_filepath): |
| 91 | + os.remove(transcoded_filepath) |
| 92 | + return {} |
| 93 | + |
| 94 | + |
| 95 | +def handle_common(request_id, oss_client, input_path, shortname, output_dir, dst_format): |
| 96 | + transcoded_filepath = os.path.join('/tmp', shortname + '.' + dst_format) |
| 97 | + if os.path.exists(transcoded_filepath): |
| 98 | + os.remove(transcoded_filepath) |
| 99 | + cmd = ["ffmpeg", "-y", "-i", input_path, transcoded_filepath] |
| 100 | + try: |
| 101 | + subprocess.run( |
| 102 | + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) |
| 103 | + |
| 104 | + oss_client.put_object_from_file( |
| 105 | + os.path.join(output_dir, shortname + '.' + dst_format), transcoded_filepath) |
| 106 | + except subprocess.CalledProcessError as exc: |
| 107 | + # if transcode fail,trigger invoke dest-fail function |
| 108 | + raise Exception(request_id + |
| 109 | + " transcode failure, detail: " + str(exc)) |
| 110 | + finally: |
| 111 | + if os.path.exists(transcoded_filepath): |
| 112 | + os.remove(transcoded_filepath) |
| 113 | + return {} |
0 commit comments