-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
50 lines (35 loc) · 1.62 KB
/
lambda_function.py
File metadata and controls
50 lines (35 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import json
import ffmpeg
import time
from json import loads
from pytubefix import YouTube
from pytubefix.cli import on_progress
def handler(event, context):
print(event)
#json_data = json.loads(event)
try:
yt = YouTube(event['url'], on_progress_callback=on_progress)
video_stream = yt.streams.filter(progressive=False, file_extension='mp4', only_video=True).first()
# Get the highest quality audio stream
audio_stream = yt.streams.filter(progressive=False, file_extension='mp4', only_audio=True).first()
#get timestamp to append to file. This can be changed to projectid and timestamp later on
timestamp = str(int(time.time()))
video_path = '/app/downloads/video-' + timestamp + '.mp4'
audio_path = '/app/downloads/audio-' + timestamp + '.mp4'
output_path = '/app/downloads/final-' + timestamp + '.mp4'
# Download the video and audio streams
video_stream.download(output_path='/app/downloads', filename='video-' + timestamp + '.mp4')
audio_stream.download(output_path='/app/downloads', filename='audio-' + timestamp + '.mp4')
input_video = ffmpeg.input(video_path)
input_audio = ffmpeg.input(audio_path)
ffmpeg.concat(input_video, input_audio, v=1, a=1).output(output_path).run()
return {
'statusCode': 200,
'body': json.dumps('Success')
}
except Exception as e:
return {
'statusCode': 404,
'body': json.dumps(str(e))
}