View difference between Paste ID: 5web4Srw and raniG4xC
SHOW: | | - or go back to the newest paste.
1
#!/bin/bash
2
3
################################################################################
4
#
5
# Script name: MultiMedia Concat Script (mmcat)
6
# Author: burek (burek021@gmail.com)
7
# License: GNU/GPL, see http://www.gnu.org/copyleft/gpl.html
8
# Date: 2012-07-14
9
#
10
# This script concatenates (joins, merges) several audio/video inputs into one
11
# final output (just like as if all the inputs were played in a playlist, one
12
# after another).
13
#
14
# All input files must have at least one audio and at least one video stream.
15
# If not, you can easily add audio silence, using FFmpeg. Just search the
16
# internet for "ffmpeg add silence".
17
#
18
# The script makes use of FFmpeg tool (www.ffmpeg.org) and is free for use under
19
# the GPL license. The inspiration for this script came from this FAQ item:
20
# http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f
21
#
22
# If you find any bugs, please send me an e-mail so I can fix it.
23
#
24
################################################################################
25
#
26
# General syntax: mmcat <input1> <input2> <input3> ... <output>
27
#
28
# For example: mmcat file1.flv file2.flv output.flv
29
# would create "output.flv" out of "file1.flv" and "file2.flv".
30
#
31
################################################################################
32
33
# change this to what you need !!!
34
EXTRA_OPTIONS='-vcodec libx264 -crf 23 -preset medium -acodec aac -strict experimental -ac 2 -ar 44100 -ab 128k'
35
36
################################################################################
37
#
38
# NO NEED TO TOUCH ANYTHING AFTER THIS LINE!
39
#
40
################################################################################
41
42
# the version of the script
43-
VERSION=1.1
43+
VERSION=1.2
44
45
# location of temp folder
46
TMP=/tmp
47
48
################################################################################
49
50
echo "MultiMedia Concat Script v$VERSION (mmcat) - A script to concatenate multiple multimedia files."
51
echo "Based on FFmpeg - www.ffmpeg.org"
52
echo "Don't forget to edit this script and change EXTRA_OPTIONS"
53
echo ""
54
55
################################################################################
56
# syntax check (has to have at least 3 params: infile1, infile2, outfile
57
################################################################################
58
if [ -z $3 ]; then
59
	echo "Syntax: $0 <input1> <input2> <input3> ... <output>"
60
	exit 1
61
fi
62
63
################################################################################
64
# get all the command line parameters, except for the last one, which is output
65
################################################################################
66
# $first  - first parameter
67
# $last   - last parameter (output file)
68
# $inputs - all the inputs, except the first input, because 1st input is
69
#           handled separately
70
################################################################################
71
first=${@:1:1}
72
last=${@:$#:1}
73
len=$(($#-2))
74
inputs=${@:2:$len}
75
76
# remove all previous tmp fifos (if exist)
77
rm -f $TMP/mcs_*
78
79
################################################################################
80
# decode first input differently, because the video header does not have to be
81
# kept for each video input, only the header from the first video is needed
82
################################################################################
83
mkfifo $TMP/mcs_a1 $TMP/mcs_v1
84
ffmpeg -y -i $first\
85
	-vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1\
86
	-an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1\
87-
	2>$TMP/mcs_log_1 &
87+
	</dev/null 2>$TMP/mcs_log_1 &
88
89
################################################################################
90
# decode all the other inputs, remove first line of video (header) with tail
91
# $all_a and $all_v are lists of all a/v fifos, to be used by "cat" later on
92
################################################################################
93
all_a=$TMP/mcs_a1
94
all_v=$TMP/mcs_v1
95
i=2
96
for f in $inputs
97
do
98
	mkfifo $TMP/mcs_a$i $TMP/mcs_v$i $TMP/mcs_t$i
99
	ffmpeg -y -i $f \
100
		-vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i\
101
		-an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_t$i\
102-
		2>$TMP/mcs_log_$i &
102+
		</dev/null 2>$TMP/mcs_log_$i &
103
	tail $TMP/mcs_t$i -n +2 > $TMP/mcs_v$i &
104
	all_a="$all_a $TMP/mcs_a$i"
105
	all_v="$all_v $TMP/mcs_v$i"
106
	let i++
107
done
108
109
################################################################################
110
# concatenate all raw audio/video inputs into one audio/video
111
################################################################################
112
mkfifo $TMP/mcs_a_all
113
mkfifo $TMP/mcs_v_all
114
cat $all_a > $TMP/mcs_a_all &
115
cat $all_v > $TMP/mcs_v_all &
116
117
################################################################################
118
# finally, encode the raw concatenated audio/video into something useful
119
################################################################################
120
ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i $TMP/mcs_a_all \
121
       -f yuv4mpegpipe -vcodec rawvideo -i $TMP/mcs_v_all \
122
	$EXTRA_OPTIONS \
123
	$last\
124
	2>$TMP/mcs_log_output
125
126
################################################################################
127
# remove all fifos
128
################################################################################
129
rm -f $TMP/mcs_*