本文最后更新于:2015-06-04 14:31:47
原文:http://www.yaosansi.com/post/how-to-combine-files-on-windows-and-linux/

WINDOWS

1
格式:copy /b <filename1>+<filename2>+…+<filenameN> <filename>

使用方法:假设你有两个文件分别是stream1.ts 和 stream2.ts 那么我们在该目录下输入“copy /b stream1.ts+stream2.ts stream.ts”就可以了,其中stream.ts是你合并后生成的新文件名。

当然,windows也可以使用type命令:

1
格式:type file1 file2 > file3

Linux

在Linux下同样也能实现文件合并,用“cat”命令就能实现。

1
格式:cat stream1.ts stream2.ts > stream.ts

FFmpeg

以上方法仅适合TS等文件信息保存中文件各个片段的视频格式。而对于文件信息在文件特定部位(如文件头)等视频,直接使用上述方法只是把文件强制的合并到了一起,甚至不能正常播放。

使用ffmpeg的concat可以实现简单的流文件合并功能

1
ffmpeg -i "concat:01.ts|02.ts"  -c copy -y  cat.ts

也可以使用文件列表,将需要拼接的视频文件按以下格式保存在一个列表 list.txt 中:

1
2
3
4
# this is a comment
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
1
ffmpeg -f concat -i mylist.txt -c copy output

自动生产文件列表 (将目录中所有.wav文件输出到mylist.txt中)

1
2
3
4
# with a bash for loop
for f in ./*.wav; do echo "file '$f'" >> mylist.txt; done
# or with printf
printf "file '%s'\n" ./*.wav > mylist.txt

注意:FFmpeg由于只是重新封装,并不会对文件进行重新的编解码,所以速度非常快并且是无损的,但并不意味着适用所有场景。

Shell脚本

合并相同编码文件

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
#!/bin/bash
#cut/join videos using ffmpeg without quality loss

if [ -z $1 ] || [ -z $2 ]; then
echo "Usage:$0 c[ut] seconds <File>"
echo " eg. $0 c 10 80 example.mp4"
echo " eg. $0 c 00:00:10 00:01:20 example.mp4"
echo "Usage:$0 j[oin] <FileType>"
echo " eg. $0 j avi"
exit
fi

case "$1" in
c)
echo "cuttig video..."
fileName=$(echo $4 | cut -f 1 -d '.')
fileType=$(echo $4 | cut -f 2 -d '.')
ffmpeg -i $4 -ss $2 -t $3 -acodec copy -vcodec copy $fileName-$2-$3.$fileType
;;
j)
echo "joinning videos..."
rm temp_list.txt
for f in ./*.$2; do echo "file '$f'" >> temp_list.txt; done
printf "file '%s'\n" ./*.$2 > temp_list.txt
ffmpeg -f concat -i temp_list.txt -c copy output.$2
rm temp_list.txt
;;
*)
echo "wrong arguments"
;;
esac
exit

合并不同编码文件

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
#!/bin/bash

################################################################################
#
# Script name: MultiMedia Concat Script (mmcat)
# Author: burek (burek021@gmail.com)
# License: GNU/GPL, see http://www.gnu.org/copyleft/gpl.html
# Date: 2012-07-14
#
# This script concatenates (joins, merges) several audio/video inputs into one
# final output (just like as if all the inputs were played in a playlist, one
# after another).
#
# All input files must have at least one audio and at least one video stream.
# If not, you can easily add audio silence, using FFmpeg. Just search the
# internet for "ffmpeg add silence".
#
# The script makes use of FFmpeg tool (www.ffmpeg.org) and is free for use under
# the GPL license. The inspiration for this script came from this FAQ item:
# http://ffmpeg.org/faq.html#How-can-I-join-video-files_003f
#
# If you find any bugs, please send me an e-mail so I can fix it.
#
################################################################################
#
# General syntax: mmcat <input1> <input2> <input3> ... <output>
#
# For example: mmcat file1.flv file2.flv output.flv
# would create "output.flv" out of "file1.flv" and "file2.flv".
#
################################################################################

# change this to what you need !!!
EXTRA_OPTIONS='-vcodec libx264 -crf 23 -preset medium -acodec aac -strict experimental -ac 2 -ar 44100 -ab 128k'

################################################################################
#
# NO NEED TO TOUCH ANYTHING AFTER THIS LINE!
#
################################################################################

# the version of the script
VERSION=1.3

# location of temp folder
TMP=/tmp

################################################################################

echo "MultiMedia Concat Script v$VERSION (mmcat) - A script to concatenate multiple multimedia files."
echo "Based on FFmpeg - www.ffmpeg.org"
echo "Don't forget to edit this script and change EXTRA_OPTIONS"
echo ""

################################################################################
# syntax check (has to have at least 3 params: infile1, infile2, outfile
################################################################################
if [ -z $3 ]; then
echo "Syntax: $0 <input1> <input2> <input3> ... <output>"
exit 1
fi

################################################################################
# get all the command line parameters, except for the last one, which is output
################################################################################
# $first - first parameter
# $last - last parameter (output file)
# $inputs - all the inputs, except the first input, because 1st input is
# handled separately
################################################################################
first=${@:1:1}
last=${@:$#:1}
len=$(($#-2))
inputs=${@:2:$len}

# remove all previous tmp fifos (if exist)
rm -f $TMP/mcs_*

################################################################################
# decode first input differently, because the video header does not have to be
# kept for each video input, only the header from the first video is needed
################################################################################
mkfifo $TMP/mcs_a1 $TMP/mcs_v1

ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>/dev/null </dev/null &
ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>/dev/null </dev/null &

# if you need to log the output of decoding processes (usually not necessary)
# then replace the "2>/dev/null" in 2 lines above with your log file names, like this:
#ffmpeg -y -i $first -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a1 2>$TMP/log.a.1 </dev/null &
#ffmpeg -y -i $first -an -f yuv4mpegpipe -vcodec rawvideo $TMP/mcs_v1 2>$TMP/log.v.1 </dev/null &

################################################################################
# decode all the other inputs, remove first line of video (header) with tail
# $all_a and $all_v are lists of all a/v fifos, to be used by "cat" later on
################################################################################
all_a=$TMP/mcs_a1
all_v=$TMP/mcs_v1
i=2
for f in $inputs
do
mkfifo $TMP/mcs_a$i $TMP/mcs_v$i

ffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>/dev/null </dev/null &
{ ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>/dev/null </dev/null | tail -n +2 > $TMP/mcs_v$i ; } &

# if you need to log the output of decoding processes (usually not necessary)
# then replace the "2>/dev/null" in 2 lines above with your log file names, like this:
#ffmpeg -y -i $f -vn -f u16le -acodec pcm_s16le -ac 2 -ar 44100 $TMP/mcs_a$i 2>$TMP/log.a.$i </dev/null &
#{ ffmpeg -y -i $f -an -f yuv4mpegpipe -vcodec rawvideo - 2>$TMP/log.v.$i </dev/null | tail -n +2 > $TMP/mcs_v$i ; } &

all_a="$all_a $TMP/mcs_a$i"
all_v="$all_v $TMP/mcs_v$i"
let i++
done

################################################################################
# concatenate all raw audio/video inputs into one audio/video
################################################################################
mkfifo $TMP/mcs_a_all
mkfifo $TMP/mcs_v_all
cat $all_a > $TMP/mcs_a_all &
cat $all_v > $TMP/mcs_v_all &

################################################################################
# finally, encode the raw concatenated audio/video into something useful
################################################################################
ffmpeg -f u16le -acodec pcm_s16le -ac 2 -ar 44100 -i $TMP/mcs_a_all \
-f yuv4mpegpipe -vcodec rawvideo -i $TMP/mcs_v_all \
$EXTRA_OPTIONS \
$last

################################################################################
# remove all fifos
################################################################################
rm -f $TMP/mcs_*

参考: