Friday, March 13, 2015

Enable Remote Desktop in Elementary OS

In order to enable remote desktop to a host running Elementary OS, you need to perform the following steps


  1. Install xrdp
    sudo aptitude install xrdp
  2. Direct xrdp to use pantheon session manager
    echo gnome-session --session=pantheon > ~/.xsession
Now you should be able to login from any host to your Elementary desktop, using a client such as rdesktop

Saturday, January 28, 2012

Magic Folder for pictures and videos

Every time I decide to move pictures from my camera to the computer, I spend several hours... And that's quite annoying..
I have to rename the pictures, so they include the shot date into the filename, rename the video files for the same reason, and finally re-encode the video files to MPEG-4 in order to save some space.

Wouldn't it be nice if all these happened automatically?
Here is the way to create a so called "Magic Folder" that does the job for you.

Every time you copy a picture or a video file into this folder, the picture is renamed and the video file is renamed and encoded.
As an extra step, I move the original video file to another storage.

You will need the following packages


In order continuously monitor a folder for new files you can use incron

sudo aptitude install incron

You also need jhead, to rename the pictures

sudo aptitude search jhead

In order to be able to encode video files using the H.264 encoder, you can enable the medibuntu repositories and install the following

sudo aptitude install ffmpeg libavcodec-extra-52

To enable notifications, you have to install the following

sudo aptitude install libnotify-bin

The script that does the job is the following


#!/bin/bash

# This script gets a filepathname and a target folder as input
# If the filename does not start with a number, the file is renamed, including the modification date of the file
# If the file is an AVI video file, it is encoded into mp4 (h.264) using ffmpeg
# After the encoding process completes, the original file is moved to the target folder and a notification is sent

PARAM=$1
MOVETOPATH=$2
echo ${PARAM}

DIR="`dirname ${PARAM}`"
FNAME="`basename ${PARAM}`"
EXT=${FNAME##*.}
FILENAME=${FNAME%.*}
OLDIFS=$IFS

IFS='
'
FC="`echo ${FNAME} | cut -c1-1`"

if [ "${FC}" -eq "${FC}" ] 2>/dev/null; then
echo "${FNAME} starts with an integer, skipping everything"
else

if [ "x${EXT}" = "xjpg" -o "x${EXT}" = "xJPG" ] ; then
jhead -exonly -n%Y_%m_%d_%f ${PARAM}
# notify-send ["${FNAME}"] "File has been encoded." -t 1000
fi

if [ "x${EXT}" = "xAVI" -o "x${EXT}" = "xavi" ] ; then

MODDATE=`stat -c %y "${PARAM}"`
FDATE=`date -d "${MODDATE}" "+%Y_%m_%d"`
NEWFNAME=${FDATE}_${FNAME}
NEWPATHNAME=${DIR}/${NEWFNAME}

echo ---- Renaming file: $FNAME to ${NEWFNAME}..
mv ${PARAM} ${NEWPATHNAME}

ENCODEDPATHNAME=${NEWPATHNAME%.*}.mp4
echo ---- Encoding file ${NEWPATHNAME} to ${ENCODEDPATHNAME}..
ffmpeg -i "${NEWPATHNAME}" -vcodec libx264 -vpre hq -acodec libfaac -ac 2 -ab 160k -threads 0 "${ENCODEDPATHNAME}"
echo ---- Moving old video file to a backup folder..
mv "${NEWPATHNAME}" "${MOVETOPATH}/${NEWFNAME}"
# notify-send ["${FNAME}"] "File has been encoded." -t 1000
fi
fi
echo Done!
echo " "
IFS=$OLSIFS

The hq preset being used in the encoding command is the following.
I have changed it a little bit to improve quality.
You can adjust b & bt to reduce file size or improve quality

cat /home/USERNAME/.ffmpeg/libx264-hq.ffpreset
coder=1
flags=+loop
cmp=+chroma
partitions=+parti8x8+parti4x4+partp8x8+partb8x8
me_method=umh
subq=8
me_range=16
g=250
keyint_min=25
sc_threshold=40
i_qfactor=0.71
b_strategy=2
qcomp=0.6
b=4500k
bt=5500k
qmin=10
qmax=51
qdiff=4
bf=3
refs=4
directpred=3
trellis=1
flags2=+wpred+mixed_refs+dct8x8+fastpskip
wpredp=2


Now we need to tell the system the we need to monitor a specific folder and whenever a new file is copied in it, to run our script

Enter incrontab -e in your terminal and an editor will appear

Enter the following line, changing the paths appropriately


MAGIC_FOLDER_PATH IN_CREATE sh MAGIC_FOLDER_SCRIPT_PATH/magicfolder.sh $@/$# PATH_TO_STORAGE

Save the file in the editor and your Magic Folder should be working

!! Warning !!
Do not delete the original pictures/videos from your camera, until you make sure everything is working as expected

Wednesday, October 20, 2010

Batch rename your pictures to include shot date

It is a good practice to rename your pictures and include the date/time the picture was taken in the name.
You can use jhead to get the date/time info from the EXIF header and rename the pictures.

The following command will rename all the JPG files in the current folder(not recursively) using the EXIF info. After the operation the filenames will look like this YYYY_MM_DD_ORIGINALFILENAME.jpg

jhead -exonly -n%Y_%m_%d_%f *.jpg


You can visit the homepage for more info on jhead

Thursday, June 24, 2010

Batch rename files using their modification date

Unlike JPG images, video files do not include some kind of EXIF table.

An easy way to remember the date your video was shot, is to include the shot date in the filename.

In my case I had tons of small video files and the only clue about the shot date was their modification date. What I needed, was a small script to rename all the video files and include each file's modification date in the new filename.

This was a bit tricky because I had to handle spaces in directory names. The key was to temporarily modify the internal field separator (IFS) to exclude space. Here is the script.

#!/bin/bash
OLDIFS=$IFS

IFS=$'\n'
for file in `find . -name '*.mp4'`
do

dir=`dirname "$file"`
filen=`basename "$file"`

moddate=`stat -c %y "$file"`
fdate=`date -d "$moddate" "+%Y-%m-%d"`

echo Renaming file: $filen ..
mv $file $dir/$fdate-$filen
done

IFS=$OLSIFS