Odd, sweet and still elegant.


In case you were wondering? This post is part of a series of which its master can be found here.

As I was wondering how ‘catenating’ avi files would/should work, I stumbled upon this short but elegant solution. *SWEET*

avimerge -o Name-of-your-choosing.avi -i $(ls)

In case the directory you work in contains several file types.

avimerge -o Name-of-your-choosing.avi -i $(ls *.avi)

When the ordering of said files is odd at best!?

avimerge -o Name-of-your-choosing.avi -i $(ls *.avi | sort -n)

Only problem with this setup is, that if you want to ‘automate’ this process, avimerge keeps spewing errors at me?? However mencoder will work when used and needed as a ‘batch converter’ of sorts. 😉

mencoder -forceidx -oac copy -ovc copy $(ls) -o "$OUPUT"

In case the directory you work in contains several file types.

mencoder -forceidx -oac copy -ovc copy $(ls *.avi) -o "$OUPUT"

When the ordering of said files is odd at best!?

mencoder -forceidx -oac copy -ovc copy $(ls *.avi | sort -n) -o "$OUTPUT"

So here is an enhanced script which from now on also process(es) *.wmv and mp{e,eg,g,4} file(s) for you.
Edited on: 10092011@11:15GMT)

## Invoke as: ./cat.files.sh $ARG
## Where $ARG is one of the following three.
# avi, mpg or wmv
## Thus (in my case):
# cat.files.sh avi
## Will catenate all the avi files it finds in the current working directory.
## However:
# cat.files.sh avi wmv
## Will fail, because the script only accepts one argument passed to it.
## That's on purpose!
## But feel free to edit at will.

Scriptname: cat.files.sh :

#!/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"argument at a time!"$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

Enjoy! You scriptkiddies, you… 😀

7 thoughts on “Odd, sweet and still elegant.

  1. https://help.ubuntu.com/community/UsingTheTerminal
    The only real difference in between command line usage and the use of a gui (to accomplish the same goal).
    Is this: Speed!

    Why? In a gui you turn options on and off with either the click (or a multitude of them) of the mouse or somewhat ‘faster’ =(reads as ‘more advanced’) by using shortcut keyboard options and has the benefit of being somewhat more ‘intuitive’.
    The cli however can ‘act’ in a similar way (as it does for me now):

    $ somecommand -w -t -f /some/input/file /some/output/file
    

    But requires the user to be able to think in a more ‘abstract’ manner.

    Now you decide what’s best (for your situation?) 😉

    Like

  2. excellent writing skills & also I just had a quick question for you.. is this a WP theme blog or is it a different theme? I really like the design

    Like

  3. awesome writing skills.. Did you write this yourself? Either way I’m impressed, and I will be back to read more in the future

    Like

    • Questions, questions, questions and then some…

      • @1 Thanks.
      • @2 What do you think?
      • @3 Good for you.
      • @4 ditto.

      As for the one not asked. Yes I moderate all comments and links I don’t like will be removed! Start your own blog if you so must advertise. Capiche!

      PS: Interesting IP address btw. Multiuser system in use over there. 😆

      Like

  4. I wanted to check up and allow you to know how much I treasured discovering this blog today. I’d personally consider it the honor to work at my company and be able to use the tips provided on your website and also take part in visitors’ comments like this. Should a position associated with guest publisher become available at your end, please let me know.

    Like

    • While I’ve said a while ago on facebook: “Buying one’s way into heaven never did really work out for anyone”

      I am going to add the following: “Sucking up one’s way into ****heaven does neither”
      In other words you’ll have to do much better than this ‘to impress me’! 😆

      As for the guest publishing. I already got me a person to put the comma’s and shit in the right places. Namely Me!

      All kidding set aside though: I do welcome constructive criticism and I’d consider co-authoring material with people who actually know what they are talking about. That would be people who are me⁴ or something!?

      Also one should consider my blog as the place where I so now and then “talk out loud”, nothing more… Me talking out loud may be of benefit to a few and a true pain in the ass to a few others. Honestly, I wouldn’t want to have it any other way.

      As for tips, scripts and other content on my blog read the following:
      https://bohemian0wildebeest.wordpress.com/notice/

      Kindest regards,

      Alex

      Like

Comments are closed.