How to concatenate two flv files?

16,208

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. 
Share:
16,208

Related videos on Youtube

kev
Author by

kev

Updated on September 18, 2022

Comments

  • kev
    kev almost 2 years

    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
    

    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
    

    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...
    

    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)
    

    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: -
    
    • Sebastian
      Sebastian over 11 years
      mencoder -forceidx -of lavf -oac copy -ovc copy -o output.flv clip1.flv clip2.flv clip3.flv source
  • tumchaaditya
    tumchaaditya about 12 years
    ok. does the joining method in my link work with flv files?(the "cat" method).
  • neu242
    neu242 almost 11 years
    Fantastic answer! This was the only way I managed to merge FLV's without recoding the video.
  • evilsoup
    evilsoup over 10 years
    @DaveJarvis you're probably using an old version of ffmpeg (such as those provided in the repositories of most major Linux distros). From your comment to another of my answers I think you're using either Debian or Ubuntu (or a derivative), which doesn't even contain the real ffmpeg, but a fake and deliberately crippled version from the libav team (due to some nasty project-forking politics). Try with a static version of ffmpeg from here, or compile your own following the instruction on the ffmpeg wiki.