Apply delay to MKV/eac3to chapter files

Small bash script to apply a delay to MKV/eac3to chapter files (CHAPTERXX=/CHAPTERXXNAME= format):

#!/bin/bash
# usage: ./chapshift.sh "+0.792 seconds" < <input> > <output>
set -o errexit -o noclobber -o nounset -o pipefail

date_offset="$1"

apply_delay() {
    date --date="$1 $date_offset" +%T,%N | cut -c 1-12
}

while read -r origline
do
    line=`echo ${origline} | sed 's/\=/\=\ /g' | sed 's/\n//g'`
    if [[ $line =~ ^CHAPTER[0-9][0-9]=\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9] ]]
    then
        read -r chapter start_date <<<"$line"
        if [[ $start_date =~ 00:00:00.000.* ]]
        then
            new_start_date="00:00:00.000"
        else
            new_start_date="$(apply_delay "$start_date")"
        fi
        chapter=`echo ${chapter} | sed 's/=\ /=/g'`
        new_start_date=`echo ${new_start_date} | sed 's/\,/\./g'`
        printf "%s%s\n" "${chapter}" "$new_start_date"
    else
        printf "%s\n" "$origline"
    fi
done

Run any program through a SOCKS proxy (chain)

proxychains-ng enables you to easily route any program trough a proxy (chain) without the need to work yourself through tons of man pages and parameter lists.

git clone https://github.com/rofl0r/proxychains-ng.git
cd proxychains-ng/
./configure --prefix=/usr/
make
sudo make install

After installing you can either create a default config at /etc/proxychains.conf or pass a custom location/file using -f <filename>. A sample config is located at src/proxychains.conf, which can be installed at the default location using sudo make install-config.

strict_chain #chain proxies in the order as they appear in the list (all proxies must be online)
proxy_dns #proxy DNS requests
remote_dns_subnet 224 #subnet for internal remote DNS mapping
# Some timeouts in milliseconds (defaults)
tcp_read_time_out 15000
tcp_connect_time_out 8000
chain_len=1 # Chain length

[ProxyList]
socks5 127.0.0.1 1080 # shadowsocks
socks4 127.0.0.1 9050 # tor
http   somehost  8080 <user> <password> # http

proxychains-ng supports different chaining modes:

  • strict_chain: use all proxies in the order they appear in the config
    • all must be online
  • dynamic_chain: use all online proxies in the order they appear
    • at least one must be online
  • round_robin_chain: proxy chain of online proxies with a length of chain_len
    • Previously used servers are skipped/used as an offset for round robin
  • random_chain: as the name says… random proxy chain with a length of chain_len

Using proxychains-ng is as simple as:

proxychains4 wget <url>
proxychains4 -f <file.conf> ssh user@host

Using lockfile to create a semaphore in bash scripts

Sometimes you want to limit the number of times a script can run in parallel. You can do so by using lockfile (part of procmail). For example: want to make sure a script only has one instance? Simply add

lockfile -r 0 "<lock file name>" || exit 1

to the start of your script. Retry count, sleep time, etc are configurable. The -! flag inverts the exit status, making in possible to use lockfile as break conditions in a while loops with bash.