Convert multiple Flash (flv) Videos to, for example, MPEG encoding

With ffmpeg and bash this is really easy. Also .flv and .mpeg are easily interchangeable by all other video codecs supported by ffmpeg:

#!/bin/bash
##convert all videos of $srctype type to $desttype type encoding
SRCCODEC="flv"
DESTCODEC="mpeg"
for i in *.$SRCCODEC; do
    echo
    echo -n "###################  "
    echo -n "will convert \"$i\""
    echo "  ###################"
    echo
    ffmpeg -i "${i}" -y "${i%%.${SRCCODEC}}.${DESTCODEC}" && rm -vf "$i"
done

Note that this includes removing the original video file only after a successful recode run overwriting existing destination files (-y option). The interesting part is this: ${i%%.${SRCCODEC}}. It removes the source’s postfix, i.e. file extension. You could save this as a text file, say convert_all_videos_in_pwd.sh and chmod +x filename. I have my own scripts go to ~/bin which I include in $PATH via ~/.bashrc. If you do so you would typically call this within the videos’ directory including logging all output and errors to convert.log, i.e. redirecting STDOUT (= file descriptor 1) and STDERR (= file descriptor 2) via &>:

convert_all_videos_in_pwd.sh &> convert_all.log &

If you want to see the output you could use tail -f logfile or read more on redirection and duplication. You could also do all that in a single command line:

for i in *.flv; do echo converting $i; ffmpeg -i "${i}" -y "${i%%.flv}.mpeg" && rm -vf "$i"; done

Extracting a Bunch of .gz Files in One Go using Bash’s For Loop While Keeping Originals

While compressing a set of single files is fairly easy (one only needs to add ‘.gz’ or similar to the end of “file name strings”) the oposite is not so easy. Nevertheless it can be done doing:

for i in *.gz; do echo extracting $i ...; sudo su -c "gzip -dc $i > ${i:0:${#i}-3}"; done && ls *xpm

sudo su -c "" is for Ubuntu systems, ${#i}-3 sais “length of i minus 3” which makes ${i:0:${#i}-3} to become each file name without trailing .gz.

Ressources

  • Bash by Example
  • man 8 bash section “Parameter Expansion” -> “Substring Expansion”

Prompt me, Bash! Pronto!

While searching for a promt command for bash to trimm long pwds (current working directory) I stumbled uppon “Blog.ubrious“. After a little hacking I adopted it to work for linux bash. It also cuts off leading characters when pwd is longer than 20 characters with build-in commands only. Plus I opted to use a ‘@’ as dilimeter between user name and host name so to rather have ssh style. Here is what I came up with:

PROMPT_COMMAND='PS1="\[33[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\33[32m\\]"; else echo "\\[\33[31m\\]"; fi\`[\u'@'\h: \`if [[ ${#PWD} > 20 ]]; then echo ${PWD:$((${#PWD}-20))}; else echo "\\w"; fi\`]\$\[33[0m\] "; echo -ne "33]0;`hostname -s`:`pwd`07"'

If wordpress does mess up the code (which I hope does not happen) please visit the original blogger’s post since he was able to upload a text file (I’m not, unfortunatelly).Here is a small list on what it can do for you and a little demo:

  1. Changes to red if the last command didn’t have a return code of 0
  2. Includes your bash history # for easy history command repeating. Follow the link to find out how to use !!, !$ and !number.
  3. Makes sure path length doesn’t exceed 30 regardless of what the base name is (bit after last fwd slash).

Bash prompt trimming

The only small hassle about it is even though it uses \w it wouldn’t abreveate my home directory as ‘~’ for me.

Note 2008/05/14: If you want to use this promt command with screen, too, make sure to test $TERM for ‘screen’. For example mine reads:

case "$TERM" in
xterm*|rxvt*|screen*)
     PROMPT_COMMAND=...

How to find out what occupies space on your Linux hard drive

The other day I noticed that my settings directory (/etc) uses over 13 MB of my hard drive. So I wandered which package (I’m using a Debian based package managed system) makes the settings directory grow so large. After a couple of trails and errors I came up with the following sequence of commands:

$ du -h --max-depth=1 /etc 2> /dev/null | egrep '(^[5-9][0-9]{2}K)|M'
692K    /etc/X11
672K    /etc/acpi
712K    /etc/xdg
2.1M    /etc/brltty
500K    /etc/ssl
528K    /etc/mono
20K     /etc/NetworkManager
13M     /etc
$ dpkg -S '/etc/brltty'
brltty-x11, brltty: /etc/brltty
$ apt-cache show brltty | grep -A5 'Description'
Description: Access software for a blind person using a soft braille terminal
BRLTTY is a daemon which provides access to the Linux console (text mode)
for a blind person using a soft braille display.  It drives the braille
terminal and provides complete screen review functionality.
The following display models are supported:
* Alva (ABT3xx/Delphi)

Fortunatelly, I’m not blind so I could remove brltty with aptitude which then suggested to remove dependencies, too.

References:

  • Regex reference
  • Resources for advanced Ubuntu topics, eg. how to remove …-desktop meta packages with apt-get (instead of aptitude), secure networking setup, etc.

Let Bash do the math: Doing calculations using that bash

Have you ever wanted to “just do some math” without much fuss? I noticed a while ago (but haven’t really used it since) that bash (born again shell) has, amongst many other evaluations, an arithmetic one. Using the syntax $((expression)) you can evaluate the expression to arithmetically. Operations allowed are quite a few. From man bash:

ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, asso ciativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.

       id++ id--   variable post-increment and post-decrement
       ++id --id   variable pre-increment and pre-decrement
       - +             unary minus and plus
       ! ~             logical and bitwise negation
       **              exponentiation
       * / %         multiplication, division, remainder
       + -             addition, subtraction
       <>       left and right bitwise shifts
       =
                        comparison
       == !=       equality and inequality
       &               bitwise AND
       ^               bitwise exclusive OR
       |                bitwise OR
       &&             logical AND
       ||              logical OR
       expr?expr:expr
                        conditional operator
       = *= /= %= += -= <>= &= ^= |=
                        assignment
       expr1 , expr2
                        comma

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is ref erenced, or when a variable which has been given the integer attribute using declare -i is assigned a value. A null value evaluates to 0. A shell variable need not have its integer attribute turned on to be used in an expression.

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Other wise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arith metic base, and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.