I tried to create simple yt converter which from given url will download audio and upload it to s3. I was able to save text file using this lambda function but when it comes to audio it just get timed out. Here is my code:
import json
import tinys3
from pytube import YouTube
import boto3
def lambda_handler(event, context):
video_url = event['url']
file_name = video_url[8:]+'.mp4'
BUCKET_NAME = 'my_bucket_name'
def YoutubeAudioDownload(video_url,destination_path):
AUDIO_DOWNLOAD_DIR = destination_path
video = YouTube(video_url)
audio = video.streams.filter(only_audio = True).first()
try:
audio.download(AUDIO_DOWNLOAD_DIR)
print("audio was downloaded successfully")
except:
print("Failed to download audio")
try:
YoutubeAudioDownload(video_url,'/tmp/'+ file_name)
s3 = boto3.client('s3')
response = s3.put_object(Body='/tmp/'+ file_name, Bucket=BUCKET_NAME, Key=file_name, ContentType='audio/mp4')
print(response)
return {
'statusCode': 200,
'body': json.dumps('MP4 file uploaded successfully')
}
except Exception as e:
print(e)
return {
'statusCode': 500,
'body': json.dumps('Error uploading MP4 file: ' + str(e))
}
When I delete extension ‘.mp4’ from file name it works but uploaded file isn’t working and weight only 39B. When extension is added there is timeout all the time.
2
Answers
Downloading videos and uploading to S3 might take some time. Ensure your Lambda function has sufficient execution time; otherwise, it might be interrupted due to a timeout. You can set the timeout duration in Lambda’s configuration under General Configuration.
Your issue seems to be you are mising writing binary data to S3 using the Python client. That is, you need to read the binary data from the downloaded MP4 file and then upload that binary data to S3. You can use the open function to read the file.
I just tested this code and it works. Only the upload part is specified. I uploaded a MP4 named Friends.mp4.
This code does the same as this Java example.
Point here is when using AWS SDK, you must specify the binary data to upload.
After I ran the Python code — the file is located in the bucket.