-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from bnusunny/dev
split video in parallel
- Loading branch information
Showing
23 changed files
with
2,274 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,4 +248,6 @@ $RECYCLE.BIN/ | |
|
||
samconfig.toml | ||
.aws-sam | ||
packaged.yaml | ||
packaged.yaml | ||
|
||
efs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"eventVersion": "2.0", | ||
"eventSource": "aws:s3", | ||
"awsRegion": "us-east-1", | ||
"eventTime": "1970-01-01T00:00:00.000Z", | ||
"eventName": "ObjectCreated:Put", | ||
"userIdentity": { | ||
"principalId": "EXAMPLE" | ||
}, | ||
"requestParameters": { | ||
"sourceIPAddress": "127.0.0.1" | ||
}, | ||
"responseElements": { | ||
"x-amz-request-id": "EXAMPLE123456789", | ||
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH" | ||
}, | ||
"s3": { | ||
"s3SchemaVersion": "1.0", | ||
"configurationId": "testConfigRule", | ||
"bucket": { | ||
"name": "serverless-video-transcoding", | ||
"ownerIdentity": { | ||
"principalId": "EXAMPLE" | ||
}, | ||
"arn": "arn:aws:s3:::serverless-video-transcoding" | ||
}, | ||
"object": { | ||
"key": "videos/wildlife_1h_4k.mp4", | ||
"size": 7210676572, | ||
"eTag": "617586411c792e23833f01ea820822d3-860", | ||
"sequencer": "0A1B2C3D4E5F678901" | ||
} | ||
} | ||
} | ||
] | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import boto3 | ||
import os | ||
import json | ||
import math | ||
import subprocess | ||
from urllib.parse import unquote_plus | ||
from botocore.config import Config | ||
|
||
s3_client = boto3.client('s3', os.environ['AWS_REGION'], config=Config( | ||
s3={'addressing_style': 'path'})) | ||
efs_path = os.environ['EFS_PATH'] | ||
PARALLEL_GROUPS = int(os.environ['PARALLEL_GROUPS']) | ||
MAX_CONCURRENCY_MAP = 40 | ||
|
||
def analyze_video(bucket, key, video_file): | ||
video_file_presigned_url = s3_client.generate_presigned_url( | ||
ClientMethod='get_object', | ||
Params={'Bucket': bucket, 'Key': key}, | ||
ExpiresIn=600 | ||
) | ||
|
||
# get media information. | ||
cmd = ['ffprobe', '-loglevel', 'error', '-show_format', | ||
'-show_streams', '-of', 'json', video_file_presigned_url] | ||
|
||
print("get media information") | ||
return json.loads(subprocess.check_output(cmd)) | ||
|
||
|
||
def generate_control_data(video_details, segment_time, download_dir, object_name): | ||
control_data = { | ||
"video_details": video_details, | ||
"download_dir": download_dir, | ||
"object_name": object_name, | ||
"video_groups": [] | ||
} | ||
|
||
video_stream = None | ||
for stream in video_details["streams"]: | ||
if stream["codec_type"] == "video": | ||
video_stream = stream | ||
break | ||
|
||
if video_stream != None: | ||
video_duration = float(video_stream["duration"]) | ||
segment_count = int(math.ceil(video_duration / segment_time)) | ||
print("video duration: {}, segment_time: {}, segment_count: {}".format( | ||
video_duration, segment_time, segment_count)) | ||
|
||
video_groups = [] | ||
group_count = PARALLEL_GROUPS | ||
group_segment_count = math.ceil(1.0*segment_count/group_count) | ||
|
||
for group_index in range(0, group_count): | ||
video_segments = [] | ||
for segment_index in range(0, group_segment_count): | ||
if segment_count <= 0: | ||
break | ||
video_segments.append({ | ||
"start_ts": segment_time * (group_index * group_segment_count + segment_index), | ||
"duration": segment_time, | ||
"segment_order": group_index * group_segment_count + segment_index | ||
}) | ||
segment_count -= 1 | ||
video_groups.append(video_segments) | ||
|
||
control_data["video_groups"] = video_groups | ||
|
||
return control_data | ||
|
||
|
||
def lambda_handler(event, context): | ||
bucket = event['bucket'] | ||
key = event['key'] | ||
object_prefix = event['object_prefix'] | ||
object_name = event['object_name'] | ||
download_dir = os.path.join(efs_path, event['job_id']) | ||
segment_time = int(event.get('segment_time', os.environ['DEFAULT_SEGMENT_TIME'])) | ||
|
||
try: | ||
os.mkdir(download_dir) | ||
except FileExistsError as error: | ||
print('directory exist') | ||
|
||
os.chdir(download_dir) | ||
|
||
video_details = analyze_video(bucket, key, object_name) | ||
|
||
control_data = generate_control_data(video_details, segment_time, download_dir, object_name) | ||
|
||
return control_data |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
aws-xray-sdk==2.4.2 |
Oops, something went wrong.