Cat 1.mpg 2.mpg 3.mpg et al… > New.mpg


Updated today: Saturday, September 10, 2011
(Dear reader, you might want to take a look at the following post: http://wp.me/pNJrg-dM Why? Well I’ve got plans to add this one and many others to my “one script to rule them all” effort soon.)

Either:

cat 1.mpg 2.mpg 3.mpg et al... > New.mpg

Or:
If one were so inclined to do:

cat 1.vob 2.vob 3.vob et al... > New.vob

Anyway this post belongs to its master:
case-select-video-conversions/
Related post:
odd-sweet-and-still-elegant

By far the easiest way to join multiple *.mp(e)g files together is to merge them with a simple utility called cat. There is only one problem though. While playback of the new file will work (most of the times 😆 ) one will notice that the time index is off (Relating to the first file in the catenated series.) To overcome this problem: One uses in the next example an ever handy utility called: avidemux.

#!/bin/bash
# Modified: Today by E.l.f
#
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
#
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
#
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the Free Software Foundation, Inc., 51
## Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
## http://www.gnu.org/copyleft/gpl.html
#
## Script-name - cat-mpg.sh
#
## Source (inspired on): http://ffmpeg.org/ffmpeg-doc.html
## And: http://ubuntuforums.org/showthread.php?t=1037517
## If colors are not defined in .bashrc, then uncomment the next three lines.
#YELLOW="\033[1;33m"
#RED="\033[0;31m"
#ENDCOLOR="\033[0m"
## Root check / Sanity check.  You decide.  >-)
## Just in case someone calls this script with sudo -s,
## or has cd'ed into his/her path.
if [ $USER = root ]; then
  echo -e $RED"Are you Insane!"
  echo -e $RED"Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"Exiting..."$ENDCOLOR
  exit 0
else
  echo ""
  echo "$USER may proceed."  
  echo "May peace be with you."  
fi
echo -n "Enter filename: "
read OUTPUT
OUTPUT=$OUTPUT
OUTPUT+=".mpg" # Append new extension
VAR="mpg-files.txt"
ls *.mp*g | sort > $VAR # Collect the files in the current directory
cat $VAR | while read line; do  # Loop read the filenames from the file
INPUT=$(echo ${line}) # Grab the nxt new filename
cat "$INPUT" >> "$OUTPUT"  # Append works.
done
\rm $VAR # Remove the text file with the file names
# \rm *.mp*g # Maybe later!?
exit 0

An updated version of the above mentioned script is available below:
Now it also handles ‘catenation’ of other file types. <– Changed a few bits here and there, enough to merit an “update”.

#!/bin/bash
# Modified: Today by E.l.f.
#
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 2 of the License, or (at your option) any later
## version.
#
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
#
## You should have received a copy of the GNU General Public License along with
## this program; if not, write to the Free Software Foundation, Inc., 51
## Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
## http://www.gnu.org/copyleft/gpl.html
#
## Script-name - cat.files.sh
#
## Source (inspired on): http://ffmpeg.org/ffmpeg-doc.html
## And: http://ubuntuforums.org/showthread.php?t=1037517
RED="\033[0;31m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
NC="\033[0m"
if [ $USER = root ]; then
  echo -e $RED"   Are you Insane!"
  echo -e $CYAN"   Error: In order to use this script, one must NOT be $USER"
  echo -e $YELLOW"    Exiting..."$NC
  exit 0
else
  echo ""
  echo -e $BLUE"    $USER may proceed."
  echo -e $CYAN"    May peace be with you."$NC
fi
clear
echo "-------------------Catenate Options List-------------------"
echo
echo "    Please choose one of the following options:              "
echo "                              Or                             "
echo "    Hit Ctrl+c (^c) to stop now."
echo "    \"UPPER\", \"lower\" and \"Capitalized\" spelling "
echo "    are valid forms of input."
echo
echo "    Catenate [avi] files."
echo "    Catenate [mpg] files."
echo "    Catenate [wmv] files."
echo
echo "-------------------------------------------------------------"
echo ""
MINPARAMS=1
if [ $# -gt "$MINPARAMS" ]; then
  echo -e $YELLOW"This script only operates on"$NC" $BLUE"$MINPARAMS""$NC" "$YELLOW"file at a time!"$NC
  echo -e $BLUE"(Do)es your filename(s): "$YELLOW""$*""$NC". "$BLUE"Contain space(s)?"$NC
  echo -e $BLUE"If so, then you might want to rename {that,those} file(s)."$NC
  echo -e $BLUE"By replacing those spaces with {.,-,_}s. ; - )"$NC
  exit 0
fi
if [ "$1" = avi ];then Choice=avi;Choice=$Choice;fi
if [ "$1" = mpg ];then Choice=mpg;Choice=$Choice;fi
if [ "$1" = wmv ];then Choice=wmv;Choice=$Choice;fi
#read Choice
case "$Choice" in
    "Avi" | "AVI" | "avi")
    echo -n "Enter filename: "
    read OUTPUT
    OUTPUT=$OUTPUT
    OUTPUT+=".avi"
    ## Here have one to rule them all!
    # LS=$(ls *.{avi,mp{e,eg,g},wmv} 2>/dev/null)
    LS=$(ls *.avi 2>/dev/null)
    echo -e $BLUE"The elegance in this case referring only to this one short but sweet line..."$NC
    ## Why does the echo work as expected\?  *Continues on line 54*
    echo -e $CYAN"    mencoder -forceidx -oac copy -ovc copy "$LS" -o "$OUTPUT""$NC
    mencoder -forceidx -oac copy -ovc copy $(ls *.avi | sort -n) -o "$OUTPUT"
    ## Let's move the new file out of the way.
    mv "$OUTPUT" ../"$OUTPUT"
    ## Then we remove the files we've worked on.
    \rm *.avi
    ## Make it seem nothing happened. ; - )
    mv ../"$OUTPUT" .
    ls -lh
    cd ..
    exit 0
  ;;
    "Mpg" | "MPG" | "mpg")
    echo -n "Enter filename: "
    read OUTPUT
    OUTPUT=$OUTPUT
    OUTPUT+=".mpg" # Append new extension
    VAR="mpg-files.txt"
    ls *.mp{e,eg,g} 2>/dev/null | sort > $VAR # Collect the files in the current directory
    cat $VAR | while read line; do  # Loop read the filenames from the file
    INPUT=$(echo ${line}) # Grab the nxt new filename
    cat "$INPUT" >> "$OUTPUT"
    done
    \rm $VAR # Remove the text file with the file names
    ## Let's move the new file out of the way.
    mv "$OUTPUT" ../"$OUTPUT"
    ## Then we remove the files we've worked on.
    \rm *.mp{e,eg,g} 2>/dev/null
    ## Make it seem nothing happened. ; - )
    mv ../"$OUTPUT" .
#    sleep 2
#    ## Lets run repair right after shall we.
#    ls *.mp{e,eg,g,4} 2>/dev/null | sort > $VAR # Collect the files in the current directory
#    cat $VAR | while read line; do  # Loop read the filenames from the file
#    INPUT=$(echo ${line}) # Grab the nxt new filename
#    OUTPUT=${INPUT%.mp*} # Remove shortest match of characters between the '. ' and the '*' at end of string
#    OUTPUT+=".avi" # Append new extension
#    avidemux --nogui --rebuild-index --autoindex --force-alt-h264 --load "$INPUT" --save "$OUTPUT" --output-format AVI --quit
#    done
#    if [ -f "$VAR" ] ; then \rm $VAR;fi # Remove the text file with the file names
#    \rm *.mp{e,eg,g,4} 2>/dev/null
#    \rm *.idx 2>/dev/null # We don't need those indexes anymore.
    ls -lh
    cd ..
    exit 0
  ;;
    "Wmv" | "WMV" | "wmv")
    echo -n "Enter filename: "
    read OUTPUT
    OUTPUT=$OUTPUT
    OUTPUT+=".wmv"
    LS=$(ls *.wmv 2>/dev/null)
    echo -e $BLUE"The elegance in this case referring only to this one short but sweet line..."$NC
    ## Why does the echo work as expected\?  *Continues on line 54*
    echo -e $CYAN"    mencoder -forceidx -oac copy -ovc copy "$LS" -o "$OUTPUT""$NC
    mencoder -forceidx -oac copy -ovc copy $(ls *.wmv | sort -n) -o "$OUTPUT"
    ## Let's move the new file out of the way.
    mv "$OUTPUT" ../"$OUTPUT"
    ## Then we remove the files we've worked on.
    \rm *.wmv
    ## Make it seem nothing happened. ; - )
    mv ../"$OUTPUT" .
    ls -lh
    cd ..
    exit 0
  ;;
    *)
     # Default option.
     # Empty input (hitting RETURN) fits here, too.
     echo
     echo "Please choose a valid option."
     echo "\"UPPER\", \"lower\" and \"Capitalized\" spelling are supported"
     echo "Usage: wmv|flv|vob|avi|mpg"
     echo
  ;;
esac
exit 0

2 thoughts on “Cat 1.mpg 2.mpg 3.mpg et al… > New.mpg

  1. Pingback: Now go repair them « Bohemian Wildebeest's Blog

  2. Pingback: Batch processing by means of using any script of your choosing. « Bohemian Wildebeest's Blog

Comments are closed.