|
| 1 | +import base64 |
| 2 | +import os |
| 3 | +import time |
| 4 | +import oss2 |
| 5 | + |
| 6 | +# Specify access information, such as AccessKeyId, AccessKeySecret, and Endpoint. |
| 7 | +# You can obtain access information from evironment variables or replace sample values in the code, such as <your AccessKeyId> with actual values. |
| 8 | +# |
| 9 | +# For example, if your bucket is located in the China (Hangzhou) region, you can set Endpoint to one of the following values: |
| 10 | +# http://oss-cn-hangzhou.aliyuncs.com |
| 11 | +# https://oss-cn-hangzhou.aliyuncs.com |
| 12 | + |
| 13 | + |
| 14 | +access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '<yourAccessKeyId>') |
| 15 | +access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '<yourAccessKeySecret>') |
| 16 | +bucket_name = os.getenv('OSS_TEST_BUCKET', '<yourBucketName>') |
| 17 | +endpoint = os.getenv('OSS_TEST_ENDPOINT', '<yourEndpoint>') |
| 18 | + |
| 19 | +key = 'test-video.mp4' |
| 20 | +dest_key = 'dest_test-video' |
| 21 | +video_path = 'your mp4 video path' |
| 22 | + |
| 23 | +# Make sure that all parameters are correctly configured |
| 24 | +for param in (access_key_id, access_key_secret, bucket_name, endpoint): |
| 25 | + assert '<' not in param, 'Please set parameters:' + param |
| 26 | + |
| 27 | + |
| 28 | +# Create a bucket. You can use the bucket to call all object-related operations |
| 29 | +bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name) |
| 30 | + |
| 31 | +# Upload local video files |
| 32 | +put_result = bucket.put_object_from_file(key, video_path) |
| 33 | +print("put object result status: %s" % put_result.status) |
| 34 | + |
| 35 | +try: |
| 36 | + # Set process |
| 37 | + process = "video/convert,f_mp4,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1|sys/saveas,o_{0},b_{1}".format( |
| 38 | + oss2.compat.to_string(base64.urlsafe_b64encode(oss2.compat.to_bytes(dest_key))).replace('=', ''), |
| 39 | + oss2.compat.to_string(base64.urlsafe_b64encode(oss2.compat.to_bytes(bucket.bucket_name))).replace('=', '')) |
| 40 | + |
| 41 | + # Call async_ process_ Object interface |
| 42 | + result = bucket.async_process_object(key, process) |
| 43 | + print("async process object result status: %s" % result.status) |
| 44 | + print(result.request_id) |
| 45 | + print("event_id: %s" % result.event_id) |
| 46 | + print("async_request_id: %s" % result.async_request_id) |
| 47 | + print("task_id: %s" % result.task_id) |
| 48 | + |
| 49 | + # Sleep for a period of time, waiting for asynchronous video processing to complete |
| 50 | + time.sleep(10) |
| 51 | + |
| 52 | + # Check if the processed video exists |
| 53 | + exists = bucket.object_exists(dest_key+".mp4") |
| 54 | + print("is exists: %s" % exists) |
| 55 | +except oss2.exceptions.OssError as e: |
| 56 | + pass |
| 57 | +finally: |
| 58 | + # Delete video files and processed files |
| 59 | + del_key = bucket.delete_object(key) |
| 60 | + print("delete key result: %s" % del_key.status) |
| 61 | + del_dest_key = bucket.delete_object(dest_key+".mp4") |
| 62 | + print("delete dest key result: %s" % del_dest_key.status) |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
0 commit comments