How to concatenate two flv files?

Solution 1:

I would personally do this with ffmpeg's

Concat demuxer

First create a file called inputs.txt which looks like this:

file 'input1.flv'
file 'input2.flv'

Then use ffmpeg like so:

ffmpeg -f concat -i inputs.txt -c copy output.mp4

(You can use output.flv, though MP4 is a generally more useful format). The demuxer is avaiable on versions of ffmpeg from 1.1 onwards. If you want to stick to an outdated version of ffmpeg for some reason, you can use the

Concat protocol

This is a little bit complicated. The FLV container does not support concatenation at the file level, so you'll need to remux to a container that does - like an MPEG Transport Stream. Unfortunately, with h.264 video and AAC audio, you'll need to apply a couple of bit stream filters.

Since you're on linux, you can use named pipes.

mkfifo temp0 temp1

You'll need to do the following in three separate teminal windows (or tabs if your terminal emulator supports them - ctrl+shift+t normally opens a new tab):

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

You can, in fact, run all of those on one (rather complicated-looking) command line:

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

Make sure that output.mp4 doesn't already exist, of this won't work. If someone is reading this is on a system that doesn't support named pipes, they'd have to use:

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

This will create a pair of intermediary files called temp0.ts and temp1.ts, which can be safely destroyed when you're done.

Note that these instructions will work for the FLV files specified in the OP, and probably most modern FLVs from the internet, since they almost-universally use h264 video and aac audio. For other codecs, these instructions will have to be tweaked a little.

Solution 2:

Do you insist on using FLV format? you can convert them to mpg or some other formats(refer this) and then join them(refer this).

or may be you can just try the joining method directly on flv files.

EDIT

Read this or this. It uses some different parameters for MEncoder.

Solution 3:

I found a python package: http://pypi.python.org/pypi/vnc2flv/


flvcat.py

flvcat.py is a simplistic editing program for a FLV movie. It supports concatenating multiple movies, clipping a movie's frame size, re-sampling a movie into a smaller size with auto-panning, etc. Syntax:

flvcat.py [options] src1[:ranges1] src2[:ranges2] ... output 

For each movie file, you can clip the parts of the movie to add by specifying its ranges. Ranges is comma-separated, hyphenated list of milliseconds. For example,

out.flv:10000-20000 

means a 10-second clip from movie out.flv (0:10-0:20). Specifying only one end of the range is also supported:

out.flv:10000- 

means the entire movie except the first 10 seconds. When ranges are omitted, the whole movie is used.

Examples:

$ flvcat.py movie1.flv movie2.flv output.flv
(Concatenate movie1.flv and movie2.flv and save it as output.flv)

$ flvcat.py -W 640x480 movie1.flv output.flv
(Resize the movie1.flv with auto-panning with its window size 640x480 and save it as output.flv)

$ flvcat.py movie1.flv:15000-30000 output.flv
(Clip the part of movie1.flv from 0:15 to 0:30 and save it as output.flv)

$ flvcat.py movie1.flv:2500- output.flv
(Chop the first 2.5 seconds off and save it as output.flv)

Options:

-r fps
    Specifies the number of frames per second. (default: 15) 
-K keyframe
    Specifies the rate of key frames that is inserted in every this number of frames. (default: every 150 frames) 
-B blocksize
    Specifies the block size. (default: 32) 
-C wxh{+|-}x{+|-}y
    Specifies the clipping. (default: entire frame) 
-W wxh
    Specifies the window size for auto panning. Auto panning tracks the changes in the screen and tries to focus on the active part of the screen. This helps reducing the movie screen size. (default: no auto panning) 
-S speed
    Specifies the speed of auto panning. (default: 60 frames) 
-f
    Forces overwriting the output file.