How to concatenate two flv files?

kev*_*kev 15 mencoder merge ffmpeg flv

I want to concatenate two flv files(downloaded from Youtube.com) by Linux command line tools.
The ffmpeg tutorial is too complicated, so I tried the mencode tutorial.
But I get a empty output. I don't know how to fix the problem.

ffmpeg -i input_1.flv

ffmpeg version 0.8.1-4:0.8.1-0ubuntu1, Copyright (c) 2000-2011 the Libav developers
  built on Mar 22 2012 05:29:10 with gcc 4.6.3
This program is not developed anymore and is only provided for compatibility. Use avconv instead (see Changelog for the list of incompatible changes).
[flv @ 0x89faaa0] Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 59.94 (2997/50) -> 29.97 (30000/1001)
Input #0, flv, from 'input_1.flv':
  Metadata:
    starttime       : 0
    totalduration   : 173
    totaldatarate   : 457
    bytelength      : 9874241
    canseekontime   : true
    sourcedata      : B4A7D6704MH1334385311768958
    purl            : 
    pmsg            : 
  Duration: 00:02:52.83, start: 0.000000, bitrate: 459 kb/s
    Stream #0.0: Video: h264 (Main), yuv420p, 640x360 [PAR 1:1 DAR 16:9], 360 kb/s, 29.97 tbr, 1k tbn, 59.94 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16, 99 kb/s
At least one output file must be specified
Run Code Online (Sandbox Code Playgroud)

ffmpeg -i input_2.flv

ffmpeg version 0.8.1-4:0.8.1-0ubuntu1, Copyright (c) 2000-2011 the Libav developers
  built on Mar 22 2012 05:29:10 with gcc 4.6.3
This program is not developed anymore and is only provided for compatibility. Use avconv instead (see Changelog for the list of incompatible changes).
[flv @ 0x85b7aa0] Estimating duration from bitrate, this may be inaccurate

Seems stream 0 codec frame rate differs from container frame rate: 59.94 (2997/50) -> 29.97 (30000/1001)
Input #0, flv, from 'input_2.flv':
  Metadata:
    starttime       : 0
    totalduration   : 488
    totaldatarate   : 254
    bytelength      : 15467674
    canseekontime   : true
    sourcedata      : BADC21903MH1334385484128583
    purl            : 
    pmsg            : 
  Duration: 00:08:07.54, start: 0.000000, bitrate: 250 kb/s
    Stream #0.0: Video: h264 (Main), yuv420p, 640x360 [PAR 1:1 DAR 16:9], 151 kb/s, 29.97 tbr, 1k tbn, 59.94 tbc
    Stream #0.1: Audio: aac, 44100 Hz, stereo, s16, 99 kb/s
At least one output file must be specified
Run Code Online (Sandbox Code Playgroud)

mencoder -of lavf -oac copy -ovc copy -o output.flv input_1.flv input_2.flv

MEncoder svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team
success: format: 0  data: 0x0 - 0x96ab41
libavformat version 53.21.0 (external)
Mismatching header version 53.19.0
libavformat file format detected.
[flv @ 0xb6b09d80]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: video (h264), -vid 0
[lavf] stream 1: audio (aac), -aid 0
VIDEO:  [H264]  640x360  0bpp  29.970 fps  360.0 kbps (43.9 kbyte/s)
[V] filefmt:44  fourcc:0x34363248  size:640x360  fps:29.970  ftime:=0.0334
** MUXER_LAVF *****************************************************************
REMEMBER: MEncoder's libavformat muxing is presently broken and can generate
INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer
will play these INCORRECT files as if nothing were wrong!
*******************************************************************************
OK, exit.
videocodec: framecopy (640x360 0bpp fourcc=34363248)
Audio format 0x4134504d is incompatible with '-oac copy', please try '-oac pcm' instead or use '-fafmttag' to override it.

Exiting...
Run Code Online (Sandbox Code Playgroud)

mencoder -of lavf -oac pcm -ovc copy -o output.flv input_1.flv input_2.flv

MEncoder svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team
success: format: 0  data: 0x0 - 0x96ab41
libavformat version 53.21.0 (external)
Mismatching header version 53.19.0
libavformat file format detected.
[flv @ 0xb6bb6d80]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: video (h264), -vid 0
[lavf] stream 1: audio (aac), -aid 0
VIDEO:  [H264]  640x360  0bpp  29.970 fps  360.0 kbps (43.9 kbyte/s)
[V] filefmt:44  fourcc:0x34363248  size:640x360  fps:29.970  ftime:=0.0334
==========================================================================
Opening audio decoder: [ffmpeg] FFmpeg/libavcodec audio decoders
libavcodec version 53.35.0 (external)
Mismatching header version 53.32.2
AUDIO: 44100 Hz, 2 ch, s16le, 99.0 kbit/7.02% (ratio: 12376->176400)
Selected audio codec: [ffaac] afm: ffmpeg (FFmpeg AAC (MPEG-2/MPEG-4 Audio))
==========================================================================
** MUXER_LAVF *****************************************************************
REMEMBER: MEncoder's libavformat muxing is presently broken and can generate
INCORRECT files in the presence of B-frames. Moreover, due to bugs MPlayer
will play these INCORRECT files as if nothing were wrong!
*******************************************************************************
OK, exit.
videocodec: framecopy (640x360 0bpp fourcc=34363248)
VIDEO CODEC ID: 28
AUDIO CODEC ID: 2e, TAG: 0
Writing header...
[flv @ 0xb6bb6d80]Codec for stream 0 does not use global headers but container format requires global headers
[flv @ 0xb6bb6d80]Codec for stream 1 does not use global headers but container format requires global headers
[NULL @ 0xb6472380]codec not compatible with flv
Floating point exception (core dumped)
Run Code Online (Sandbox Code Playgroud)

stat output.flv

  File: `output.flv'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 700h/1792d  Inode: 1722857     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/     kev)   Gid: ( 1000/     kev)
Access: 2012-05-04 10:44:45.153319669 +0800
Modify: 2012-05-04 10:44:45.153319669 +0800
Change: 2012-05-04 10:44:45.153319669 +0800
 Birth: -
Run Code Online (Sandbox Code Playgroud)

evi*_*oup 12

我会亲自用ffmpeg的

连接分离器

首先创建一个名为的文件inputs.txt,如下所示:

file 'input1.flv'
file 'input2.flv'
Run Code Online (Sandbox Code Playgroud)

然后像这样使用ffmpeg:

ffmpeg -f concat -i inputs.txt -c copy output.mp4
Run Code Online (Sandbox Code Playgroud)

(您可以使用output.flv,但 MP4 通常是一种更有用的格式)。从 1.1 开始,分路器可用于 ffmpeg 版本。如果由于某种原因你想坚持使用过时的 ffmpeg 版本,你可以使用

连接协议

这有点复杂。该FLV容器不会在文件级别支持级联,所以你需要REMUX到一个容器,-样的MPEG传输流。不幸的是,对于 h.264 视频和 AAC 音频,您需要应用几个比特流过滤器。

由于您使用的是 linux,因此您可以使用命名管道。

mkfifo temp0 temp1
Run Code Online (Sandbox Code Playgroud)

您需要在三个单独的终端窗口(或选项卡,如果您的终端模拟器支持它们 -ctrl+shift+t通常打开一个新选项卡)中执行以下操作:

ffmpeg -i input0.flv -map 0 -c copy -f mpegts -bsf h264_mp4toannexb -y temp0

ffmpeg -i input1.flv -map 0 -c copy -f mpegts -bsf h264_mp4toannexb -y temp1

ffmpeg -f mpegts -i "concat:temp0|temp1" -c copy -absf aac_adtstoasc output.mp4
Run Code Online (Sandbox Code Playgroud)

事实上,您可以在一个(相当复杂的)命令行上运行所有这些:

ffmpeg -i input0.flv -map 0 -c copy -f mpegts -bsf h264_mp4toannexb -y temp0 2> /dev/null & \
ffmpeg -i input1.flv -map 0 -c copy -f mpegts -bsf h264_mp4toannexb -y temp1 2> /dev/null & \
ffmpeg -f mpegts -i "concat:temp0|temp1" -c copy -absf aac_adtstoasc output.mp4
Run Code Online (Sandbox Code Playgroud)

确保 output.mp4 不存在,否则将不起作用。如果有人在不支持命名管道的系统上阅读本文,他们必须使用:

ffmpeg -i input0.flv -map 0 -c copy -f mpegts -bsf h264_mp4toannexb temp0.ts
ffmpeg -i input1.flv -map 0 -c copy -f mpegts -bsf h264_mp4toannexb temp1.ts
ffmpeg -i "concat:temp0.ts|temp1.ts" -c copy -absf aac_adtstoasc output.mp4
Run Code Online (Sandbox Code Playgroud)

这将创建一对名为 temp0.ts 和 temp1.ts 的中间文件,完成后可以安全地销毁它们。

请注意,这些说明适用于 OP 中指定的 FLV 文件,并且可能适用于来自 Internet 的大多数现代 FLV,因为它们几乎普遍使用 h264 视频和 aac 音频。对于其他编解码器,这些说明必须稍作调整。