-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatenate_mp4.sh
executable file
·145 lines (111 loc) · 4.15 KB
/
concatenate_mp4.sh
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
#!/bin/bash
# Usage is sudo bash /home/mypc/softwares/concatenate_mp4.sh
#How to use concatenate_mp4.sh process:
#1- Check if any process of concatenate_mp4.sh script is already working. If there is a running process, the process will finish.
#2- Check the first line in streamListsQueue.txt, if it's an empty exit, the process will finish.
#3- Get Stream ID in streamListsQueue.txt first Item.
#4- Check if original file belongs to Stream ID exists. If streamID.mp4 does not exist, the process will finish.
#5- The next step is that script checks if streamId_1.mp4, streamId_2.mp4, streamId_3.mp4, etc. files exist. If a stream file exists, concatenate process begins in concatenate_files function. Also, concatenate process is working on the S3 mount.
#6- The script tries 3 more files if they exist. If files don't exist, process will be finished and removing stream ID in streamListsQueue.txt
function currentFileCheck(){
if [ ! -f $currentStream ]
then
false
else
true
fi
}
function originalFileCheck(){
if [ ! -f $originalFilePath ]
then
echo "Original file $originalFilePath does not exist, now trying next Stream ID"
# This command deleting first line in streamListsQueue list
sed -i '1d' $STREAM_SCRIPT_DIRECTORY
# Trying next stream ID
true
else
false
fi
}
# concatenate second file to end of first file
# first file 1.mp4 duration: 10sec
# second file 2.mp4 duration: 20sec
# output file 1.mp4 duration: 30sec
concatenate_files()
{
file1=$1
file2=$2
outputFile=$1_out_temp.mp4
temp1=$1_temp.ts
temp2=$2_temp.ts
ffmpeg -y -i $file1 -c copy -bsf:v h264_mp4toannexb -f mpegts $temp1
ffmpeg -y -i $file2 -c copy -bsf:v h264_mp4toannexb -f mpegts $temp2
ffmpeg -f mpegts -i "concat:$temp1|$temp2" -c copy -bsf:a aac_adtstoasc $outputFile
#replace outputFile to first file
mv $outputFile $file1
#delete second file and fifos
rm $2 $temp1 $temp2
}
function begin_function()
{
# get the file name
STREAM_SCRIPT_DIRECTORY="/home/ubuntu/scripts/streamListsQueue.txt"
AWS_S3_DIRECTORY="/mnt/s3_n/vod/streams"
CONCATENATED_STREAM_FOLDER="/home/ubuntu/scripts/concatenated_streams.txt"
firstStreamId=$(head -n 1 $STREAM_SCRIPT_DIRECTORY)
if [ -z "$firstStreamId" ];
then
echo "Stream List Queue is empty"
exit 1
fi
echo "MP4 File:" $firstStreamId
filename=$(basename "$firstStreamId")
#find the position of suffix,suffix of "stream1_720p_1.mp4" is "_1.mp4". original file stream1_720p.mp4
position=`echo $filename | grep -b -o -E "_{1}[0-9]+\.mp4$" | awk 'BEGIN {FS=":"}{print $1}'`
# If stream ID has no suffix
if [ -z $position ];
then
position=`echo $filename | grep -b -o -E "\.mp4$" | awk 'BEGIN {FS=":"}{print $1}'`
fi
streamId=${filename:0:$position}
originalFilePath=$AWS_S3_DIRECTORY/$streamId.mp4
echo "stream ID: "$streamId
echo "Original file name: $originalFilePath"
}
# Check process is already working on it
if [ `ps -aux |grep "concatenate" | egrep -v "grep|sh -c"| wc -l` -gt 2 ]
then
echo "Concatenate_mp4.sh Process already running"
exit 1
else
echo "Concatenate_mp4.sh Process is starting"
fi
echo "Concatenate_mp4.sh script is started"
begin_function
# Try check current file is exist 3 times, after that finishes
while originalFileCheck $originalFilePath
do
echo "Trying again in a loop"
begin_function
done
i=0
c=1
# Try check current file is exist 3 times, after that finishes
while [ $i -le 2 ]
do
currentStream=$AWS_S3_DIRECTORY/$streamId"_"$c".mp4"
echo "Current Stream ID: $currentStream"
echo "Stream file is exist checking"
if currentFileCheck $currentStream; then
((c++))
echo $currentStream "concatenating original file ->" $originalFilePath
concatenate_files $originalFilePath $currentStream;
echo $currentStream "concatenated original file ->" $originalFilePath >> $CONCATENATED_STREAM_FOLDER
else
((c++))
((i++))
echo "Stream file not found:$currentStream Tried Count:$i"
fi
done
# This function deleting first line in streamListsQueue list
sed -i '1d' $STREAM_SCRIPT_DIRECTORY