Changes between Initial Version and Version 1 of Concatenate


Ignore:
Timestamp:
Jul 18, 2012, 7:42:02 PM (7 years ago)
Author:
burek
Comment:

Created the page

Legend:

Unmodified
Added
Removed
Modified
  • Concatenate

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