<aside> 💡
효율적인 스트리밍 방식 채택
arm64 아키텍쳐로 변경해도 특별히 가성비가 좋아지지 않음 (?)
테스트
| memory(MB) | arm64 | x86_64 | 
|---|---|---|
| 1800 | 110s | 92s | 
| 2240 | 90s | 73s | 
| 3000 | 71s | 59s | 
| 4000 | 53s | 46s | 
| 6000 | 44s | 39s | 
람다 메모리 할당 부족에 따른 영상 변환 실패
Pymysql 커넥션이 가능범위 이상으로 맺어져 rds 테이블 업데이트 불가
⚠️ 다른 문제의 강의가 조회됨
/tmp/{question_id} 경로에 문제별로 독립적으로 저장최종 람다 코드
from ffmpeg_streaming import input, Formats, Bitrate, Representation, Size, S3, CloudManager
import os
import stat
import boto3
from utils import set_has_lecture_true
s3_client = boto3.client(
    "s3",
    aws_access_key_id=os.environ['s3_aws_id'],
    aws_secret_access_key=os.environ['s3_aws_secret'],
)
def lambda_handler(event, context):
    print("/tmp START: " + str(os.listdir('/tmp')))
    
    s3_source_bucket = event['Records'][0]['s3']['bucket']['name'] # thai-images
    s3_source_key = event['Records'][0]['s3']['object']['key'] # /lecture/original/test.mp4
    s3_source_key = s3_source_key.replace("+", " ") # 공백 포함된 파일명 처리
    
    s3_source_basename = os.path.splitext(os.path.basename(s3_source_key))[0] # test
    s3_source_extension = os.path.splitext(os.path.basename(s3_source_key))[1] # .mp4
    question_id = s3_source_basename.replace(" ", "")
    
    temp_original_dir = f"/tmp/{s3_source_basename + s3_source_extension}"
    temp_converted_dir = f"/tmp/{question_id}"
    
    # S3에서 /tmp로 원본 다운로드
    print(f'Downloading {s3_source_key}')
    s3_client.download_file(s3_source_bucket, s3_source_key, temp_original_dir)
    ### 미작동
    # destination = CloudManager().add(s3, bucket_name="thai-images", folder=f"lecture/{question_id}/")
    _360p  = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
    _480p  = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
    _720p  = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
    _1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))
    
    source_video = input(temp_original_dir)
    print(f'Converting from {temp_original_dir} to {temp_converted_dir}')
    dash = source_video.dash(Formats.h264())
    dash.representations(_720p)
    dash.generate_hls_playlist()
    # dash.output(clouds=destination)
    dash.output(f'{temp_converted_dir}/{question_id}.mpd')
    
    print('Sending converted files to S3')
    converted_files = os.listdir(f'{temp_converted_dir}')
    for file in converted_files:
        try:
            s3_client.upload_file(
                f'{temp_converted_dir}/{file}', # 변환된 파일 위치
                'thai-images', # 목표 S3 버킷명
                f'lecture/converted/{question_id}/{file}' # 목표 S3 경로 및 파일명
            )
        except Exception as e:
            print(e)
        finally:
            os.remove(f"{temp_converted_dir}/{file}")
        
    # /tmp 비우기
    try:
        os.rmdir(temp_converted_dir)          
        for file in os.listdir('/tmp'):
            os.remove('/tmp/' + file)
    except Exception as e:
        print(e)
    print("/tmp END: " + str(os.listdir('/tmp')))
                
    print('RDS Operation')
    set_has_lecture_true(question_id)
    print(f'{question_id} : Processing complete successfully')