Cut Video file with ffmpeg

FFMPEG is an open source and really powerful audio/video processor. You can do a lot of different things with ffmpeg. One of which is to cut video. Below are some examples to see how you can cut video with free ffmpeg.

Basic Syntax:

ffmpeg -i [input_file] -ss [start_seconds] -t [duration_seconds] [output_file]

Cut Video without Re-encoding the video file :

ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -c copy cut_video.mp4


With Re-encoding:

ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -async 1 -strict -2 cut_video.mp4

Configure postfix to use Sendgrid as default Relay

If you would like to configure postfix to use Sendgrid as the mail relay, you can do so by editing the postfix main configuration file.

vi /etc/postfix/

And then paste the content below at the end of the file :

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:username:password
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
header_size_limit = 4096000
relayhost = []:587

WordPress Admin Security

Yeah there are a lot of script kiddies out there scanning word press sites and brute forcing word press admin logins. There are ways you can limit access to the wordpress admin panel from restricted set IP Address or block unwanted browser agents. Here are some examples using .htaccess and rewrite :


Most likely there is already an .htaccess file inside your wordpress directory. Add the following snippets to your .htaccess file :


Restrict WP Admin to known IP Address only and throw 404 for everyone else, You can add multiple IP Addresses on new line:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)?wp-login\.php(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)?wp-admin$
RewriteCond %{REMOTE_ADDR} !^192\.168\.100\.111$
RewriteRule ^(.*)$ - [R=404,L]


You can block by different SERVER variables in the same manner. Below are some examples.

Block By Referrer :

RewriteCond %{HTTP_REFERER} !^http://(.*)?yoursite\.com [NC]
RewriteCond %{REQUEST_URI} ^(.*)?wp-login\.php(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)?wp-admin$
RewriteRule ^(.*)$ - [F]


Block By User Agent :

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*(Mechanize).*$ [NC]
RewriteCond %{HTTP_USER_AGENT} ^.*(Baiduspider|HTTrack|Yandex).*$ [NC]
RewriteRule .* - [F,L]

Random Wake up of Windows Machine from Sleep mode

It happens at times that the windows machine wakes up randomly from sleep mode. Here are some steps you may be able to follow to identify the cause and address the issue :

Find out the cause of the wake :

powercfg -lastwake

You may see the Device that woke the computer up as the output of the above command. If you do not see anything, that means there could have been a generic wake up timer set by some application,  then you can issue the following command mode run as administrator :

powercfg -waketimers

Here you will see an output with the  Service Info, for example, mine was caused by teamviewer :

C:\Windows\system32>powercfg -waketimers
Timer set by [SERVICE] \Device\HarddiskVolume6\Program Files (x86)\TeamViewer\Te
amViewer_Service.exe (TeamViewer) expires at 1:12:18 AM on 8/5/2015.
  Reason: generic

Now you can either just disable wake timers completely . Go to power options and then select “Sleep” option and then disable “wake timers”.

or you can disable the timer just for the application, in application settings if available.

The other way to find out the application or service causing the wake is to look at the event viewer logs.

Launch Event viewer by issuing the command :


Then go to Windows Logs and then right click on the System > Filter Current Log. In the Event Sources drop down select “Power-Troubleshooter” , click Ok and you will see a list of Logs on when and why your system was woke up.

To find out the list of devices that are able to wake up the computer, issue the following command :

powercfg -devicequery wake_armed

And to disable  a device from above list :

powercfg -devicedisablewake "name of device from above command"

To enable a device from above list :

powercfg -deviceenablewake "name of device from above command"

Installing newer or multiple version of python on Centos

I stumbled across this situation where I needed to install python  > 2.7 on Centos. Yum breaks if you upgrade the system python as yum relies on python 2.6 . So the only way to not touch the system python but still be able to use later version of python for your needs is to do the alternate install and then use virtual env to use the newer python. Here is how :

First of all , install the Development tools for some pre-requisite development libraries :

yum groupinstall "Development tools"

Now Install additional Dependencies:

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

Now Download and install the version of python that you require, I for example needed the 2.7.6 :

tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

Next, get the setup script for easy setup tool  and install it for newer version of your python :


Next, install pip using the setup tool :

easy_install-2.7 pip

Now install the virtualenv for newer python and create a sandbox call test-python-2.7

pip2.7 install virtualenv
virtualenv -p /usr/local/bin/python2.7 test-python-2.7

Next, Activate the new sandbox  and check the python version :

source test-python-2.7/bin/activate
python --version

Now you can deactivate the environment when you are done using the alternate python version:


Postfix Queue Management

Retry delivery of mail in queue :

postqueue -f

Flush mail for a domain to primary mail server from Relay server:

postqueue -s

Delete all email in queue for a domain:

postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /@example\.com/ { print $1 }' | tr -d '*!' | postsuper -d -

Delete all email in queue for an email address:

postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /email@example\.com/ { print $1 }' | tr -d '*!' | postsuper -d -

Remove all Mailer Daemon Emails From Mail Queue :

for emailID in `mailq | grep '^[A-Z0-9]' | grep 'MAILER-DAEMON' | awk '{print$1}'| tr -d '*!'`; do postsuper -d $emailID; done

Remove all Emails with specific string from mail queue :

for emailID in `mailq | grep '^[A-Z0-9]' | grep -i 'STRING' | awk '{print$1}'| tr -d '*!'`; do postsuper -d $emailID; done

Remove all Email to an address from the mail queue :

mailq | tail -n +2 | awk 'BEGIN { RS = "" } / pedro_cesar7@hotmail\.com$/ { print $1 }' | tr -d '*!' | postsuper -d -

Build 32 bit application or libraries on 64 bit machine

In order to configure script to build 32bit library on a 64 bit machine, you would have to pass the following arguments :

./configure --build=i686-pc-linux-gnu "CFLAGS=-m32" "CXXFLAGS=-m32" "LDFLAGS=-m32"

Also, If you get an error that says :

configure: error: C++ compiler cannot create executable

You would have to install following additional packages :

yum install gcc-multilib g++-multilib

Nagios Bash script to monitor Domain Expiration

This little script here, lets you use nagios to monitor domain name expiration and warn you ahead of time. You can also use it to just run some cron jobs, our extract the portion you need and use in other places :

#!/bin/bash #Author : Amit K Nepal domain=$1

if [ -z $1 ];then echo "Usage : $0 <domain name>" exit; fi WARN_DAYS=90 CRITICAL_DAYS=30 secondsToMonths=2592000 secondsToDays=86400 function getMonthAndDays() { seconds=$1 m=$((${seconds}/$secondsToMonths)) d=$(((${seconds}% (${secondsToMonths}))/${secondsToDays})) echo "$m Months and $d Days" }


expiration=`whois ${domain} | egrep -i 'Expiration|Expires on' | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}'` expirationSeconds=`date --date="${expiration}" +%s` today=`date +"%s"` diff=$(($expirationSeconds-$today)) DaysLeft=$(($diff/86400)) #convert to day message="`getMonthAndDays $diff`, Expires on $expiration" if [[ "$DaysLeft" -gt $CIRITCAL_DAYS ]];then echo "$message" exit 0; elif [[ "$DaysLeft" -lt $CRITICAL_DAYS ]];then echo "$message" exit 2; elif [[ "$DaysLeft" -lt $WARN_DAYS ]];then echo "$message" exit 1; else echo "Unable to check Domain Expiration" exit 3; fi