mp4動画ファイルのトランスコードを Python for Lambda で自動化
こちらの方法でts形式でエンコードすることは出来ました。
import boto3 from botocore.client import ClientError import json import urllib REGION_NAME = 'ap-northeast-1' TRANSCODER_ROLE_NAME = 'lambda_auto_transcoder_role' PIPELINE_NAME = 'testPipeline' OUT_BUCKET_NAME = 'ana-in' COMPLETE_TOPIC_NAME = 'test-complete' print('Loading function') s3 = boto3.resource('s3') iam = boto3.resource('iam') sns = boto3.resource('sns', REGION_NAME) transcoder = boto3.client('elastictranscoder', REGION_NAME) def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2)) # Get ARN complete_topic_arn = sns.create_topic(Name=COMPLETE_TOPIC_NAME).arn transcoder_role_arn = iam.Role(TRANSCODER_ROLE_NAME).arn # Get the object from the event bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') print("bucket={}, key={}".format(bucket, key)) try: obj = s3.Object(bucket, key) except Exception as e: print(e) print("Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.".format(key, bucket)) # Publish a message sns.Topic(complete_topic_arn).publish( Subject="Error!", Message="Failed to get object from S3. bucket={}, key={}, {}".format(bucket, key, e), ) raise e # Delete inactive pipelines pipeline_ids = [pipeline['Id'] for pipeline in transcoder.list_pipelines()['Pipelines'] if pipeline['Name'] == PIPELINE_NAME] for pipeline_id in pipeline_ids: try: response = transcoder.delete_pipeline(Id=pipeline_id) print("Delete a transcoder pipeline. pipeline_id={}".format(pipeline_id)) print("response={}".format(response)) except Exception as e: # Raise nothing print("Failed to delete a transcoder pipeline. pipeline_id={}".format(pipeline_id)) print(e) # Create a pipeline try: response = transcoder.create_pipeline( Name=PIPELINE_NAME, InputBucket=bucket, OutputBucket=OUT_BUCKET_NAME, Role=transcoder_role_arn, Notifications={ 'Progressing': '', 'Completed': complete_topic_arn, 'Warning': '', 'Error': '' }, ) pipeline_id = response['Pipeline']['Id'] print("Create a transcoder pipeline. pipeline_id={}".format(pipeline_id)) print("response={}".format(response)) except Exception as e: print("Failed to create a transcoder pipeline.") print(e) # Publish a message sns.Topic(complete_topic_arn).publish( Subject="Error!", Message="Failed to create a transcoder pipeline. bucket={}, key={}, {}".format(bucket, key, e), ) raise e # Create a job try: job = transcoder.create_job( PipelineId=pipeline_id, Input={ 'Key': key, 'FrameRate': 'auto', 'Resolution': 'auto', 'AspectRatio': 'auto', 'Interlaced': 'auto', 'Container': 'auto', }, Outputs=[ { 'Key': 'output/{}'.format('.'.join(key.split('.')[:-1])), 'PresetId': '1351620000001-200030', # System preset: HLS 1M 'SegmentDuration': '10', }, ], ) job_id = job['Job']['Id'] print("Create a transcoder job. job_id={}".format(job_id)) print("job={}".format(job)) except Exception as e: print("Failed to create a transcoder job. pipeline_id={}".format(pipeline_id)) print(e) # Publish a message sns.Topic(complete_topic_arn).publish( Subject="Error!", Message="Failed to create transcoder job. pipeline_id={}, {}".format(pipeline_id, e), ) raise e return "Success"
PresetIdを1351620000001-100070(System preset: Web)
に変更すればmp4でエンコードされると思ったのですが、変更すると何もエンコードされなくなりました。
エラー内容
Failed to create a transcoder job. pipeline_id=1494818317732-9k629f
An error occurred (ValidationException) when calling the CreateJob operation: SegmentDuration '30' is not a valid option for preset 1351620000001-100070, because the container is not ts or fmp4.: ValidationException Traceback (most recent call last): File "/var/task/lambda_function.py", line 113, in lambda_handler raise e ValidationException: An error occurred (ValidationException) when calling the CreateJob operation: SegmentDuration '30' is not a valid option for preset 1351620000001-100070, because the container is not ts or fmp4.
REPORT RequestId: 2e7fee02-391d-11e7-947b-31c8905c64e9 Duration: 2409.70 ms Billed Duration: 2500 ms Memory Size: 128 MB Max Memory Used: 51 MB
lambdaのコードをPresetId以外にも修正する必要がありそうなのですが、どこを修正すれば良いか分かりません。
どなたかご教授いただければ幸いです。
回答1件
あなたの回答
tips
プレビュー