You are not logged in.

#426 2009-07-15 14:36:23

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Post your handy self made command line utilities

Script to author a DVD consisting of multiple video files:

#!/bin/zsh

mkdir -p ~/tmp/dvd/

pushd ~/tmp/dvd/

for file in $@; do
    [[ -f "$file" ]] && ffmpeg -i "$file" -target pal-dvd "track-$(basename "$file").mpg"
done

echo "<dvdauthor>" > dvd.xml
echo "<vmgm />" >> dvd.xml
echo "<titleset>" >> dvd.xml
echo "<titles>" >> dvd.xml
echo "<pgc>" >> dvd.xml

for file in $@; do
    [[ -f "$file" ]] && echo "<vob file=\"track-$(basename "$file").mpg\" pause=\"5\"/>" >> dvd.xml
done

echo "</pgc>" >> dvd.xml
echo "</titles>" >> dvd.xml
echo "</titleset>" >> dvd.xml
echo "</dvdauthor>" >> dvd.xml

mkdir final
dvdauthor -o final -x dvd.xml

rm dvd.xml *.mpg

popd

Script to strip out all but a specified audio and video stream from a mkv file:

#!/bin/zsh

for movie in *.mkv; do
    mkvextract tracks "$movie" $1:"$movie.avi" $2:"$movie.ogg"
    mkvmerge -o "$movie-final.mkv" "$movie.avi" "$movie.ogg"
done

Script to manage a todo file in Emacs Org-mode format:

#!/bin/zsh

flag=$1
arg1=$2
arg2=$3
todofile="/home/barrucadu/docs/main.todo"

function append()
{
    tags=`echo $arg2 | sed "s/ /: :/"`
    echo "* TODO $arg1 :$tags:" >> $todofile
}

function delete()
{
    sed -i "$arg1 d" $todofile
}

function finish()
{
    sed -i "$arg1 s/^\* TODO/\* ODOT/" $todofile
    sed -i "$arg1 s/^\* DONE/\* ENODE/" $todofile
    sed -i "$arg1 s/^\* ENODE/\* TODO/" $todofile
    sed -i "$arg1 s/^\* ODOT/\* DONE/" $todofile
}

function clean()
{
    sed -i "/^\* DONE .*$/d" $todofile
}

function listall()
{
    grep -n -e "^\* \(TODO\|DONE\)" $todofile
}

function listtodo()
{
    grep -n "^\* TODO" $todofile
}

function listfinished()
{
    grep -n "^\* DONE" $todofile
}

function help()
{
    echo "Usage: todo [flag] [arg1] [arg2]"
    echo
    echo "---"
    echo
    echo "A simple todo-file manager, which manipulates todo files in a subset of the Emacs Org-mode format."
    echo
    echo "---"
    echo
    echo "Flags:"
    echo "    a  - Append new entry."
    echo "         arg1 = Description"
    echo "         arg2 = Tags"
    echo
    echo "    d  - Delete existing entry."
    echo "         arg1 = Number"
    echo
    echo "    f  - Mark entry as finished."
    echo "         arg1 = Number"
    echo
    echo "    c  - Clean finished entries from list."
    echo "    la - List all entries."
    echo "    lt - List entries marked as to do."
    echo "    lf - List entries marked as finished."
    echo "    h  - Display this text."
    echo
}

case "$flag" in
    a)
        append
        ;;
    d)
        delete
        ;;
    f)
        finish
        ;;
    c)
        clean
        ;;
    la)
        listall
        ;;
    lt)
        listtodo
        ;;
    lf)
        listfinished
        ;;
    h)
        help
        ;;
esac

Offline

#427 2009-07-15 14:51:40

sirocco
Member
Registered: 2008-03-10
Posts: 149

Re: Post your handy self made command line utilities

linkmaster03 wrote:

I made this exact program a while ago...

try pystopwatch

Offline

#428 2009-07-15 17:01:39

rebugger
Member
From: Germany
Registered: 2007-10-28
Posts: 229

Re: Post your handy self made command line utilities

Here is another one.
find files by its content

findcontent () {
    if [ -n "$2" ]; then
        find "$1" -type f -exec grep -q "$2" {} \; -print
    else
        find "./" -type f -exec grep -q "$1" {} \; -print
    fi
}

Maybe someone could add that only ascii-files are "inspected"?

Offline

#429 2009-07-15 18:37:03

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

Hi rebugger,

findcontent () {
    if [ -n "$2" ]; then
        find "$1" -type f -exec grep -qI "$2" {} \; -print
    else
        find "./" -type f -exec grep -qI "$1" {} \; -print
    fi
}

Does that do what you want?

-steve

Offline

#430 2009-07-15 20:22:34

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: Post your handy self made command line utilities

Barrucadu wrote:

Script to strip out all but a specified audio and video stream from a mkv file:

#!/bin/zsh

for movie in *.mkv; do
    mkvextract tracks "$movie" $1:"$movie.avi" $2:"$movie.ogg"
    mkvmerge -o "$movie-final.mkv" "$movie.avi" "$movie.ogg"
done

Excellent! That will be very useful for me, thx big_smile smile

Offline

#431 2009-07-15 20:28:27

rebugger
Member
From: Germany
Registered: 2007-10-28
Posts: 229

Re: Post your handy self made command line utilities

steve___ wrote:

Hi rebugger,

findcontent () {
    if [ -n "$2" ]; then
        find "$1" -type f -exec grep -qI "$2" {} \; -print
    else
        find "./" -type f -exec grep -qI "$1" {} \; -print
    fi
}

Does that do what you want?

-steve

Ah, didnt look for that smile Thanks

Offline

#432 2009-07-17 01:01:29

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: Post your handy self made command line utilities

sirocco wrote:
linkmaster03 wrote:

I made this exact program a while ago...

try pystopwatch

That takes all the fun out of programming it! I never use the pyGTK version anyways, just the CLI one because it is easier.

Offline

#433 2009-07-22 04:47:15

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

Start/stop/restart daemons with bash-completion:

#!/bin/bash
# rc - start/stop/restart Arch Linux daemons

if [ -n "$2"  ]; then
    /etc/rc.d/$1 $2; else
  echo "rc <init-script> <start/stop/restart>"
  echo ""
  echo "* Running daemons:"
  ls /var/run/daemons
  echo ""
fi

The bash environment needs to be told what variables to autocomplete for daemon.  Put in ~/.bashrc:

_rc()
{
  local cur prev
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}
  if [ $COMP_CWORD -eq 1 ]; then
    COMPREPLY=( $( compgen -W "$(for i in /etc/rc.d/*; do echo ${i##*/}; done)"  $cur ) )
  elif [ $COMP_CWORD -eq 2 ]; then
    COMPREPLY=( $( compgen -W "start stop restart" $cur ) )
  fi
}

complete -o default -F _rc rc

Last edited by Gen2ly (2009-07-25 10:37:54)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#434 2009-07-22 10:16:20

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: Post your handy self made command line utilities

I am no programmer so you may laugh at my silly program. I find it very useful though. It tries to mimic Thinkpad Presentation Manager for windows. I want to assign it to Fn-F7 but I have not had any luck so far.

With xdialog-gtk2 it even looks pretty smile

# 2009
# Presentation Manager by Rasmus <rasmus.pank@gmail.com>

Xdialog --backtitle "ThinkPad Presentation Manager for Linux" --title "ThinkPad Presentation Manager for Linux" --menu "Select the appropiate setting" 15 50 5 \
LvdsOnly           "LVDS at 1024x768" \
VgaOnly            "VGA at 1600x1200" \
Clone              "LVDS and VGA cloned at 1024x768" \
Presentation       "LVDS and VGA extended at 1024x768" \
Home               "LVDS at 1024x768 and VGA at 1600x1200" 2>/tmp/menuitem.$$

menuitem=`cat /tmp/menuitem.$$`

opt=$?

case $menuitem in 
    LvdsOnly) xrandr --output LVDS --mode 1024x768 --output VGA --off;;
    VgaOnly) xrandr --output VGA --mode 1600x1200 --output LVDS --off;;
    Clone) xrandr --output LVDS --mode 1024x768 --output VGA --mode 1024x768 --same-as LVDS;;
    Presentation) xrandr --output LVDS --mode 1024x768 --output VGA --mode 1024x768 --right-of LVDS;;
    Home) xrandr --output LVDS --mode 1024x768 --output VGA --mode 1600x1200 --right-of LVDS --primary;;
esac

Screenshot:
vMjBubg

Cheers,
Rasmus

Last edited by Pank (2009-07-22 10:28:55)


Arch x64 on Thinkpad X200s/W530

Offline

#435 2009-07-22 10:48:45

rebugger
Member
From: Germany
Registered: 2007-10-28
Posts: 229

Re: Post your handy self made command line utilities

@pank hey it looks quite nice - but i'm still using just "xrandr --auto" after plugging in the beamer

Offline

#436 2009-07-22 13:41:57

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: Post your handy self made command line utilities

Rebugger, I use it for both beamers and my external monitor. As you see, sometimes I want to have the display cloned and sometimes I want extended desktop. It might be an overkill smile


Arch x64 on Thinkpad X200s/W530

Offline

#437 2009-07-22 17:09:27

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

I like it

Offline

#438 2009-07-23 14:42:53

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Post your handy self made command line utilities

Daenyth wrote:

I'm well aware that this following bit can be greatly refined, and I will at some point, but this will display a nicer list of upgrade targets (pacman -Qu), until pacman 3.3 comes out.

LC_ALL=C pacman -Qu |sed -n '/Targets/,/Total Download/p;' | sed 's/Targets ([[:digit:]]\+)://;$d' | sed 's/[[:space:]]\+/\n/g'| grep .|sort| perl -ne 'chomp; @toks = split /-/, $_; $pkgrel = pop @toks; $pkgver = pop @toks; $pkgname = join "-", @toks; print "$pkgname $pkgver-$pkgrel\n"'

My take on this one, a little late since 3.3 is coming soon.   Probably needs more testing.

LC_ALL=C pacman -Qu | sed -r "s/^Checking|^$|^Total//;tx;s/Targets \([[:digit:]]+\): |^ +//;s/  [^$]/\n/g;p;:x;d"

Last edited by rson451 (2009-07-23 17:53:30)


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#439 2009-07-23 21:51:40

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Nifty smile

I had forgotten all about that.

Offline

#440 2009-07-23 22:14:13

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Post your handy self made command line utilities

smile  I had 46 updates on my server (due to a lagging mirror) and I wanted to go through and make sure the updates weren't going to brick me.  I remembered yours and figured I'd refine it since you never did.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#441 2009-07-23 22:50:06

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Yeah, I was too lazy. Easier in my mind to wait for 3.3, since it will be like that on its own

Offline

#442 2009-07-23 22:58:22

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

target=`date +%s --date="12 Aug 2009 00:00:00"` # date of 9.10 release
source=`date +%s` # today
diff=`expr $target - $source` # difference between dates in seconds
days=`expr $diff / 86400` # convert seconds to days

echo "$days Till event"

This isnt mine, i found it on Ubuntu forums. i added it to my bash rc, so every time i open a terminal it reminds me of the up and comming event

Last edited by markp1989 (2009-07-23 22:58:39)


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#443 2009-07-24 00:17:37

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

Burn iso's to CD or DVD.  Does a generic check to see if the CD/DVD has information on it and will blank the disk if you prompt it to.

#!/bin/bash
# burniso - burn iso's to a cd or dvd

# Limitations - generic check for CD/DVD by volume label to see if blank
#             - disks that have been written/erased since insertion will
#             -   not be properly recognized.

# iso filename
ISO=$@

# Text color variables
TXTBLD=$(tput bold)
TXTRED=$(tput setaf 1)
TXTGRN=$(tput setaf 2)
TXTYLW=$(tput setaf 3)
TXTBLU=$(tput setaf 4)
TXTPUR=$(tput setaf 5)
TXTCYN=$(tput setaf 6)
TXTWHT=$(tput setaf 7)
TXTRES=$(tput sgr0)

# Usage - displays if full argument isn't given.
if [[ -z $ISO ]]; then
    echo "${TXTBLD}${TXTGRN} * ${TXTRES}${TXTCYN}burniso <filename.iso>${TXTRES} - burn ISO's to cd or dvd.${TXTRES}"
    exit
fi

# Test if CD/DVD is blank, and prompt to blank and burn
if [[ -n `hal-device | grep volume_label_` ]]; then
  echo "${TXTBLD}${TXTGRN} * ${TXTRES}${TXTRED}Warning:${TXTRES} CD is not blank, erase it? ${TXTYLW}(y/n)${TXTRES}"
  read ERASE
  if [[ $ERASE == [Yy] ]]; then
    cdrecord -v dev=/dev/dvd blank=fast; else
    echo "${TXTBLD}${TXTGRN} * ${TXTRES}${TXTCYN}Exiting.${TXTRES}"
    exit
  fi; else
  echo "${TXTBLD}${TXTGRN} * ${TXTRES}${TXTCYN}CD/DVD is blank, continuing.${TXTRES}"
  echo "${TXTBLD}${TXTGRN} * ${TXTRES}${TXTRED}Warning:${TXTRES} Disks that have been written to after insertion will not be"
  echo "   recogized as blank."
  echo "${TXTBLD}${TXTGRN} * ${TXTRES}Burn ISO to CD? ${TXTYLW}(y/n)${TXTRES}"
  read ANSWER
  if [ $ANSWER == [Yy] ]; then
    cdrecord -dao -v $ISO; else
    echo "${TXTBLD}${TXTGRN} * ${TXTRES}${TXTCYN}Exiting.${TXTRES}"
  fi
fi

Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#444 2009-07-24 15:55:25

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Post your handy self made command line utilities

pgrep/pkill frontend

#! /bin/bash
IFS="
"
processes=$(ps -o pid,cmd,pcpu,pmem,ppid,euser -p $(pgrep "$1") 2>/dev/null ) || exit
echo "   "$(echo "$processes" | head -n 1)
processes=$(echo "$processes" | tail -n +2)
select process in $processes; do
pid=$(echo $process | sed 's/^ *\([0-9]*\).*$/\1/')
[ -z "$pid" ] && exit
echo "Kill $pid (y/Y/n)?"
read yesno
if [ x$yesno = xy ]; then
  kill $pid
elif [ x$yesno = xY ]; then
  kill -9 $pid
fi
exit
done

edit:in the sed command, removed leading spaces (I thought echo $process would take care of it)

Last edited by Procyon (2009-07-29 17:25:09)

Offline

#445 2009-08-01 08:26:32

Heller_Barde
Member
Registered: 2008-04-01
Posts: 245

Re: Post your handy self made command line utilities

Pank wrote:

I am no programmer so you may laugh at my silly program. I find it very useful though. It tries to mimic Thinkpad Presentation Manager for windows. I want to assign it to Fn-F7 but I have not had any luck so far.

With xdialog-gtk2 it even looks pretty smile

<snip/>

Cheers,
Rasmus

I hope you don't mind, I rewrote your little script (which i like very much btw) to use zenity (this is purely a personal preference) and not to use a temporary file. I thought you might like it smile

# 2009
# Presentation Manager by Rasmus <rasmus.pank@gmail.com>

menuitem=`zenity --list --title="Presentation Manager" \
--width=400 --height=330 \
--text="Screen Manager for Linux\nchoose your mode" \
--column="Modename" --column="Description" --separator=";" \
LvdsOnly           "LVDS at 1024x768" \
VgaOnly            "VGA at 1600x1200" \
Clone              "LVDS and VGA cloned at 1024x768" \
Presentation       "LVDS and VGA extended at 1024x768" \
Home               "LVDS at 1024x768 and VGA at 1600x1200"`

case $menuitem in 
    LvdsOnly) xrandr --output LVDS --mode 1024x768 --output VGA --off;;
    VgaOnly) xrandr --output VGA --mode 1600x1200 --output LVDS --off;;
    Clone) xrandr --output LVDS --mode 1024x768 --output VGA --mode 1024x768 --same-as LVDS;;
    Presentation) xrandr --output LVDS --mode 1024x768 --output VGA --mode 1024x768 --right-of LVDS;;
    Home) xrandr --output LVDS --mode 1024x768 --output VGA --mode 1600x1200 --right-of LVDS --primary;;
esac

cheers Phil

Offline

#446 2009-08-02 16:06:13

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 834
Website

Re: Post your handy self made command line utilities

Heller_Barde wrote:
Pank wrote:

I am no programmer so you may laugh at my silly program. I find it very useful though. It tries to mimic Thinkpad Presentation Manager for windows.

I hope you don't mind, I rewrote your little script (which i like very much btw) to use zenity (this is purely a personal preference) and not to use a temporary file. I thought you might like it smile

I also liked the idea and also wrote my own, in python. Pypres is a small utility for enabling external displays based on presets, intended to be used with the "Presentation" media button found on many laptops.


You need to install an RTFM interface.

Offline

#447 2009-08-02 20:01:11

Heller_Barde
Member
Registered: 2008-04-01
Posts: 245

Re: Post your handy self made command line utilities

anrxc wrote:

I also liked the idea and also wrote my own, in python. Pypres is a small utility for enabling external displays based on presets, intended to be used with the "Presentation" media button found on many laptops.

neat, this seems to be a neat exercise for everyone to enjoy big_smile
soon there will be cropping up examples in brainfuck and whitespace... tongue

Offline

#448 2009-08-03 00:39:21

majiq
Member
Registered: 2009-03-06
Posts: 259

Re: Post your handy self made command line utilities

This is a small script I created to have consistent desktop items on my Openbox desktop. It replaces my "show_desktop" keybinding for Openbox. The reason that is is because I can't get the items to register as desktop items, so I have to bring them to the fore, which breaks the state of "Show Desktop: On". If someone can point out a way to not break the Show Desktop On state, please share, because this acts based on previous actions and time, only, and not actions since the show desktop was executed.

#!/bin/bash

#Figure out the current desktop
desktop=$(wmctrl -d | sed -e '/^.  -/d' | cut -f 1 -d ' ')

#Determine the lockfile
lockfile="/tmp/."$USER"_desktop_show_lock_$desktop"

#If it exists
if [ -e $lockfile ]; then

  #See if it's too old. If so, remove and reexecute
  if [ $(( $(date +%s) - $(stat --format="%Y" $lockfile) )) -gt $((1*15)) ]; then
    rm $lockfile
    $0
    exit 0
  fi

  #If it's not too old, maximize everything on the current desktop
  for id in $( wmctrl -l | grep "0x........  $desktop" | cut -f 1 -d ' ' ); do
    xdotool windowmap $id
  done

  rm $lockfile
else

  #toggle the builtin show desktop
  wmctrl -k on

  #Show the newly deemed desktop items
  for pattern in $(cat ~/.config/openbox/desktop_items.conf ); do
    case pattern in

    [^#]*)
      for id in $( wmctrl -l | grep "$pattern" | cut -f 1 -d " "); do
        xdotool windowactivate $id
      done
      ;;
    esac

  done

  touch $lockfile || xmessage "Touch fail"

fi

exit 0
~

It works with a file at ~/.config/openbox/desktop_items.conf that can use comments and identifies windows via regexps that match against the first item in the WM_NAME field. Mine looks like this:

dclock
#majiq_prayer_times
#majiq_desktop_input
majiq_*

Last edited by majiq (2009-08-03 03:22:33)

Offline

#449 2009-08-03 01:54:47

alterecco
Member
Registered: 2009-07-13
Posts: 152

Re: Post your handy self made command line utilities

Gen2ly wrote:

Start/stop/restart daemons with bash-completion:

The bash environment needs to be told what variables to autocomplete for daemon.  Put in ~/.bashrc:

_rc()
{
  local cur prev
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}
  if [ $COMP_CWORD -eq 1 ]; then
    COMPREPLY=( $( compgen -W "$(for i in /etc/rc.d/*; do echo ${i##*/}; done)"  $cur ) )
  elif [ $COMP_CWORD -eq 2 ]; then
    COMPREPLY=( $( compgen -W "start stop restart" $cur ) )
  fi
}

complete -o default -F _rc rc

Thank you. That is a very cool one. I made a small modification to try and get the available arguments, or fall back to the defaults

_rc()
{
    local cur prev usage
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    if [ $COMP_CWORD -eq 1 ]; then
        COMPREPLY=( $( compgen -W "$(for i in /etc/rc.d/*; do echo ${i##*/}; done)"  $cur ) )
    elif [ $COMP_CWORD -eq 2 ]; then
        usage=$(grep 'usage:' /etc/rc.d/$prev | sed -e 's/.*{\(.*\)}.*/\1/' | tr '|' ' ')
        if [[ -n $usage ]]; then
            COMPREPLY=( $( compgen -W "$usage" $cur ) )
        else
            COMPREPLY=( $( compgen -W "start stop restart" $cur ) )
        fi
    fi
}

.]

Offline

#450 2009-08-04 03:59:17

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: Post your handy self made command line utilities

PSPMP4: a simple x264 video converter for creating PSP compatible video. It is very good for anime as it works with ASS subtitles too smile x264-git might be needed because of profiling.

#!/bin/bash

##
### PSPMP4: A simple video encoding tool for PSP video
##
#



###
## Main directory for where your PSP encodes will be stored
#
MP4DIR="/home/maister/psp-encodes"
#
##
###

if [ ! -d "$MP4DIR"/tmp ]
then
    mkdir "$MP4DIR"/tmp
fi

function help()
{
    echo "Usage: `basename $0` --threads/-t | --input/-i | --bitrate/-b (video bitrate) | --abitrate/-ab (audio bitrate) | --output/-o (basename only) | --size/-s width:height | -x264 \"--options for x264 here\" | -mplayer \"-options for mplayer here\""
    echo ""
    echo "Example: `basename $0` -i 720p.mkv -o Episode5 -b 800 -ab 160"
    echo ""
    echo "This script provides sane defaults for encoding PSP video, and should suit most video clips. It is sufficient to just supply an input. A recent version of x264 is required, as this script uses the baseline profile, which is only supported in newer git-versions."

}

## Default values

TAG=`date +%Y%m%d-%T`
FILENAME=""
BITRATE=550
AB=128
THREADS=auto
SCALE=480:272

##

if [ -z "$1" ]
then
    help
    exit 1
fi

while [ -n "$1" ]
do
    case "$1" in
        --threads | -t )
        shift
        THREADS="$1"
        shift
        ;;
        
        --input | -i )
        shift
        FILENAME="$1"
        shift
        ;;
        
        -b | --bitrate )
        shift
        BITRATE="$1"
        shift
        ;;

        -ab | --abitrate )
        shift
        AB="$1"
        shift
        ;;

        -s | --size )
        shift
        SCALE="$1"
        shift
        ;;

        -x264 )
        shift
        X264OPTS="$1"
        shift
        ;;

        -mplayer )
        shift
        MPLAYEROPTS="$1"
        shift
        ;;
        
        --output | -o )
        shift
        TAG="$1"
        shift
        ;;

        * )
        help
        exit 1
        ;;
        
    esac

done

# Creates fifo-pipes for audio and video
mkfifo "$MP4DIR"/tmp/video$TAG.y4m
mkfifo "$MP4DIR"/tmp/audio$TAG.wav

# Encodes audio
faac -b $AB -w "$MP4DIR"/tmp/audio$TAG.wav &
mplayer $MPLAYEROPTS -vo null -vc null -ao pcm:file="$MP4DIR"/tmp/audio$TAG.wav:fast -nocorrect-pts "$FILENAME" &

sleep 2

#First pass, video
x264 --bitrate $BITRATE --partitions=all --trellis 1 --level 2.1 --profile baseline --threads $THREADS $X264OPTS --pass 1 --stats ".stats$TAG" --output NUL "$MP4DIR"/tmp/video$TAG.y4m &
    mplayer -vo yuv4mpeg:file="$MP4DIR"/tmp/video$TAG.y4m -benchmark -vf scale=$SCALE -nosound -vc ffh264, "$FILENAME"


sleep 2

#Second pass, video
x264 --bitrate $BITRATE --partitions=all --trellis 1 --level 2.1 --profile baseline --threads $THREADS $X264OPTS --pass 2 --stats ".stats$TAG" --output "$MP4DIR"/tmp/video$TAG.264 "$MP4DIR"/tmp/video$TAG.y4m &
    mplayer -vo yuv4mpeg:file="$MP4DIR"/tmp/video$TAG.y4m -benchmark -vf scale=$SCALE -vc ffh264, $MPLAYEROPTS -nosound "$FILENAME"

sleep 2

#Muxing into PSP-compatible MP4
ffmpeg -y -vcodec copy -acodec copy -i "$MP4DIR"/tmp/video$TAG.264 -i "$MP4DIR"/tmp/audio$TAG.m4a -f psp "$MP4DIR/$TAG.mp4"

rm "$MP4DIR"/tmp/video$TAG*
rm "$MP4DIR"/tmp/audio$TAG*

There is one very weird bug with it that I can't figure out though. When I use this script in two instances, the .264 files become broken :S Works like a charm when there's only one instance though.

EDIT: Owh wait, I think I see it. I use the same ".stats" file for all instances! Fixed the script now.

Also, I find that with a quad core CPU, the video scaling of mplayer is very often the bottleneck, not x264 :s Wish mplayer was multi-threaded smile If you're going to encode several episodes of something, encode multiple videos at the same time, and use one thread for x264 (-t 1 option). smile Core i7 just loves this. The threading is insane!

Last edited by Themaister (2009-08-04 04:16:20)

Offline

Board footer

Powered by FluxBB