-
Notifications
You must be signed in to change notification settings - Fork 24
/
aws_split_and_stitch.yaml
164 lines (148 loc) · 4.7 KB
/
aws_split_and_stitch.yaml
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: Split and stitch demo long content on AWS
description: |
This job splits an input video into 30s chunks, transcodes the chunks in parallel,
and finally stitches the final result into a single file.
inputs:
bucket: my-bucket
source: s3://my-bucket/master.mov
tasks:
- name: create a presigned url
var: signedURL
run: aws s3 presign $SOURCE --expires-in 7200 > $TORK_OUTPUT
image: amazon/aws-cli:2.13.10
env:
SOURCE: "{{ inputs.source }}"
- name: get the video metadata
var: ffprobe
run: |
ffprobe \
-v quiet \
-print_format json \
-show_error \
-show_format \
-show_streams \
$SOURCE > $TORK_OUTPUT
image: jrottenberg/ffmpeg:3.4-alpine
env:
SOURCE: "{{ tasks.signedURL }}"
- name: extract the duration of the video
var: duration
run: |
DURATION=$(echo -n $FFPROBE | jq -r '.format.duration')
echo -n $DURATION >> $TORK_OUTPUT
image: badouralix/curl-jq
env:
FFPROBE: "{{ tasks.ffprobe }}"
- name: extract the framerate of the video
var: framerate
run: |
FRAMERATE=$(echo -n $FFPROBE | jq -r '.streams[] | select (.codec_type=="video") | .r_frame_rate')
echo -n $FRAMERATE >> $TORK_OUTPUT
image: badouralix/curl-jq
env:
FFPROBE: "{{ tasks.ffprobe }}"
- name: clean and parse framerate
var: framerate
run: |
python script.py $FRAMERATE > $TORK_OUTPUT
image: python:3-slim
env:
FRAMERATE: "{{ tasks.framerate }}"
files:
script.py: |
import re
import sys
cfrate = re.sub(r"[^0-9/\\.]", "", sys.argv[1])
pfrate = cfrate.split("/")
frate = float(pfrate[0])/float(pfrate[1])
print(frate,end="")
- name: calculate chunks times
var: chunks
run: |
python script.py $DURATION $FRAMERATE > $TORK_OUTPUT
image: python:3-slim
env:
DURATION: "{{ tasks.duration }}"
FRAMERATE: "{{ tasks.framerate }}"
files:
script.py: |
import math
import json
import sys
duration = float(sys.argv[1])
frate = float(sys.argv[2])
frate_ceil = math.ceil(frate)
time_unit = frate_ceil/frate
chunk_size = 30*time_unit
chunks = []
start = 0
length = 0
while(start<duration):
if(duration-start<chunk_size):
length=duration-start
else:
length=chunk_size
if duration-(start+length) < 5:
length=duration-start
chunks.append({"start":start,"length":length})
start = start+length;
print(json.dumps(chunks))
- name: generate a random folder name to store the results
var: folderName
run: echo -n "video-$(shuf -i 1-10000 -n1)" > $TORK_OUTPUT
image: ubuntu:mantic
- name: transcode the chunks in parallel
each:
list: "{{ fromJSON(tasks.chunks) }}"
task:
name: encode the chunk
var: chunk{{ item.index }}
run: |
ffmpeg -ss ${START} -i $SOURCE -to $LENGTH -vf scale=w=-2:h=720 -c:v h264 -c:a aac -ar 48000 -b:a 128k /tmp/chunk.mp4
image: jrottenberg/ffmpeg:3.4-alpine
env:
LENGTH: "{{ item.value.length }}"
SOURCE: "{{ tasks.signedURL }}"
START: "{{ item.value.start }}"
post:
- name: upload the chunk to minio
run: aws s3 cp /tmp/chunk.mp4 s3://$BUCKET_NAME/$FOLDER_NAME/chunks/chunk_$NUMBER.mp4
image: amazon/aws-cli:2.13.10
env:
BUCKET_NAME: "{{inputs.bucket}}"
FOLDER_NAME: "{{tasks.folderName}}"
NUMBER: "{{ item.index }}"
mounts:
- type: volume
target: /tmp
retry:
limit: 2
timeout: 180s
- name: stitch the chunks into a single video
run: |
for filename in /tmp/chunks/*.mp4; do
echo "file $filename" >> /tmp/chunks.txt
done
ffmpeg -f concat -safe 0 -i /tmp/chunks.txt -c copy /tmp/output.mp4
image: jrottenberg/ffmpeg:3.4-alpine
env:
BUCKET_NAME: "{{tasks.bucketName}}"
pre:
- name: download the chunks
run: aws s3 sync s3://$BUCKET_NAME/$FOLDER_NAME/chunks /tmp/chunks
image: amazon/aws-cli:2.13.10
env:
BUCKET_NAME: "{{inputs.bucket}}"
FOLDER_NAME: "{{tasks.folderName}}"
post:
- name: upload the final video to s3
run: aws s3 cp /tmp/output.mp4 s3://$BUCKET_NAME/$FOLDER_NAME/output.mp4
image: amazon/aws-cli:2.13.10
env:
BUCKET_NAME: "{{inputs.bucket}}"
FOLDER_NAME: "{{tasks.folderName}}"
mounts:
- type: volume
target: /tmp
retry:
limit: 2