Skip to content

Commit

Permalink
fix duplicated video streaming bug caused by lambda re-use for /tmp f…
Browse files Browse the repository at this point in the history
…older issue
  • Loading branch information
joseongil-zb committed Aug 26, 2022
1 parent e1d4584 commit 5b318f6
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions stack/lambdas/awsdna-download-cctv/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def lambda_function(event, context):
print(event)
channelName = event['pathParameters']['channelName']

guid = 'cctv_'+channelName+'_'+datetime.datetime.now().strftime("%f")
tmpPrefix = '/tmp/'+guid

rmOutput = subprocess.check_output(['rm', '-rf', 'tmpPrefix'])
subprocess.check_output(['mkdir', tmpPrefix])
# print('rmOutput:' + rmOutput)

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('channel')
resp = table.get_item(
Expand All @@ -33,9 +40,9 @@ def lambda_function(event, context):
print(resp['Item']['playback_url'])
url = resp['Item']['playback_url']
file = req.get(url, allow_redirects=True)
open('/tmp/playlist.m3u8', 'wb').write(file.content)
open(tmpPrefix+'/playlist_'+guid+'.m3u8', 'wb').write(file.content)

with open("/tmp/playlist.m3u8", "r") as f:
with open(tmpPrefix+'/playlist_'+guid+'.m3u8', "r") as f:
for line in f:
pass
last_line = line
Expand All @@ -45,10 +52,10 @@ def lambda_function(event, context):
url3 = '/'.join(url.split('/')[0:-1])+'/'+url2
print('url3:' + url3)
file2 = req.get(url3, allow_redirects=True)
open('/tmp/chunklist.m3u8', 'wb').write(file2.content)
open(tmpPrefix+'/chunklist_'+guid+'.m3u8', 'wb').write(file2.content)

tsList = []
with open("/tmp/chunklist.m3u8", "r") as f:
with open(tmpPrefix+'/chunklist_'+guid+'.m3u8', "r") as f:
for line in f:
if line.endswith('.ts\n'):
tsList.append(line.split('\n')[0])
Expand All @@ -58,7 +65,7 @@ def lambda_function(event, context):
for file in tsList:
tsUrl = '/'.join(url.split('/')[0:-1])+'/'+file
raw = req.get(tsUrl, allow_redirects=True)
open('/tmp/'+file, 'wb').write(raw.content)
open(tmpPrefix+'/'+guid+file, 'wb').write(raw.content)
print('tsUrl:', tsUrl)

# logger.log(logging.ERROR, 'Hello SQS and Lambda!')
Expand Down Expand Up @@ -88,21 +95,21 @@ def lambda_function(event, context):
# s3.download_file('ivs-video-archive-us-east-1',
# f, '/tmp/'+f.split('/')[-1])

filePath = "/tmp"
filePath = tmpPrefix
file_list = sorted(
[file for file in os.listdir(filePath) if file.endswith(".ts")])

logger.log(logging.ERROR, 'filelist:'+'\n'.join(file_list))

with open("/tmp/file_list.txt", "w+") as f:
with open(tmpPrefix+'/file_list_'+guid+'.txt', "w+") as f:
for file in file_list:
f.write("file '{}'\n".format(file))

os.chdir('/tmp/')
os.chdir(tmpPrefix+'/')
output = subprocess.check_output(
['ffmpeg', '-f', 'concat', '-i', 'file_list.txt', '-c', 'copy', 'cctv.mp4'])
['ffmpeg', '-f', 'concat', '-i', 'file_list_'+guid+'.txt', '-c', 'copy', 'cctv_'+guid+'.mp4'])
# logger.log(logging.ERROR, 'ffmpeg result: '+output)
logger.log(logging.ERROR, '/tmp/* file list: ' +
logger.log(logging.ERROR, tmpPrefix+'/* file list: ' +
', '.join(sorted(os.listdir(filePath))))

# # download file locally to /tmp retrieve metadata
Expand Down Expand Up @@ -131,21 +138,23 @@ def lambda_function(event, context):

# uploaded modified video to Amazon S3 bucket

mp4Name = 'cctv/'+channelName+'_'+datetime.datetime.now().strftime("%f")+'.mp4'
try:
s3.upload_file('/tmp/cctv.mp4', output_bucket, mp4Name)
s3.upload_file(tmpPrefix+'/cctv_'+guid+'.mp4',
output_bucket, 'cctv/'+guid+'.mp4')
except boto3.exceptions.S3UploadFailedError:
error_message = 'Lambda role does not have permission to call PutObject for the output S3 bucket.'
# add_failed(bucket, error_message, failed_records, key)
# continue

return {
response = {
'statusCode': 200,
'body': json.dumps({
'playback_url': 'https://d3328b7fefvh0o.cloudfront.net/'+mp4Name
'playback_url': 'https://d3328b7fefvh0o.cloudfront.net/cctv/'+guid+'.mp4'
}),
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
}
}
print('RESPONSE: ' + response)
return response

0 comments on commit 5b318f6

Please sign in to comment.