Always uses dashes in filenames

In these days of cloud storage, file synchronisation, and sharing on social media it's more important than ever to properly name files.

Spaces make sense in written language but computers don't like them. Try to use a space in a filename in a URL and it'll replace it with %20. Try to do anything on the command line and you'll have to wrap the filename in double quotes.

The two common conventions are underscores (_) and hyphens (-). One convention used commonly in Unix/Linux filenames is to use underscores to separate words in the file title, and hyphens to separate the title from version number. For example Apache modules take the form of file_name-version.extension, eg. mod_ftp-0.9.6.tar.gz.

Turns out if your filename is ever to be processed by a regular expressions (such as by the Google indexer!) the underscore will be removed and filename concatenated. When using the the \w operator to find words the underscore is ignored, but the hypen is not.

Always use dashes in filenames instead of spaces or underscores.

Screen On The Green - A Raspberry Pi Cinema

screenshot of screen on the green website

I have the pleasure of being one of a small group who run a cinema night once a month in the small town of Writtle, Essex. It's my job to take care of anything remotely technical.

We didn't have any money when we started so had to go cap-in-hand to the parish council who were great and bought the screen and the projector, we use an old Hifi system I had for sound.

We didn't want to use DVDs as we wanted a pre-show slideshow, trailers and an intermission which would've been impractical using DVDs, especially since we would need Blu-ray. Another option was to use a laptop which would've worked fine, but the Raspberry Pi with it's h.264 capable GPU seemed a perfect fit.

Initially we used a Raspberry Pi 3b+ but with the recent release of the A+ we've switched due it's lower power consumption.

In order to use the hardware h.264 decoding we had to use Omxplayer which unfortunately doesn't have playlist support so I had to use a little bash magic.

First we need a playlist file containing a list of video files in the order we want them to play:

cards/30min-preroll.mp4  
trailers/theapartment.mp4  
trailers/12angrymen.mp4  
trailers/incredibleshrinkingman.mp4  
cards/comingsoon.mp4  
cards/feature.mp4  
cards/intro.mp4  
film/amelie-pt1.mp4  
cards/intermission-start.mp4  
ads/ads.mp4  
cards/intermission-end.mp4  
film/amelie-pt2.mp4  
cards/thanks-for-coming.mp4  

A simple bash script to read the playlist and line-by-line and invoke Omxplayer. The -o both parameter sets the output of audio to both the HDMI cable and phones out. -b blanks the screen first.

#!/bin/bash

PLAYER=(/usr/bin/omxplayer -o both -b)  
PLAYLIST="sotg.pls"

clear  # clear the screen

if [ -e "$PLAYLIST" ]  
then  
    IFS=$'\012'  # Set the line ending
    for file in $(cat "$PLAYLIST")
    do
        ${PLAYER[@]} "$file"
    done
fi  

In order to split the film in two for the intermission I find a suitable spot, note the time, and convert it to seconds. For example, 45m 34s in would be (45 * 60) + 34 which is 2734. I then use this in a couple of FFMpeg commands to trim the single film file in to two parts:

#!/bin/bash

FN="amelie"  
TIME=2734

ffmpeg -ss 0 -t $TIME -i $FN.mp4 -strict -2 -af "volume=12dB" $FN-pt1.mp4  
ffmpeg -ss $TIME -i $FN.mp4 -strict -2 -af "volume=12dB" $FN-pt2.mp4  

-ss sets the start time for the trimmed file, and -t sets the end time, which we can omit on the second command as we want to trim to the end. -af "volume=12dB" boosts the volume while we're at it.

Preventing SYSLOG spam

One of my Raspberry Pis has a problem. When connected to WIFI every few seconds two lines are added to my /var/syslog:

Mar 6 10:13:15 Pi dhcpcd[764]: eth0: Router Advertisement from...  

In this particular example I could fix this by disabling the IPv6 DHCP servive in my router, or disabling IPv6 on the Pi, but for the moment I want to simply keep these messages out of my syslog.

Happily, there's a nice syntax which allows us to add lines to the /etc/rsyslog.conf to filter lines sent to the syslog and prohibit them from being added.

In the case of the example above I added the following line near the top of the configuration file:

:msg, contains, "Router Advertisement from" stop

Then restart with sudo systemctl restart rsyslog and no more junk in the syslog!

sudo in .bashrc

Due to a quirk with the Raspberry Pi's current implementation of Debian I had some trouble mounting CIFS shares at start up. The solution was to run the mount command once logged in, the easiest way to do this is in the .bashrc script.

However! To run the mount -a command to mount all shares requires elevated privileges but I didn't want to have to type password twice at login. The solution to this is add the command itself to the /etc/sudoers with the NOPASSWD flag:

pi    ALL=(ALL)    NOPASSWD: /bin/mount  

One thing to note is that it's important to provide the full path to the command.

Extracting and renaming files from zip archives

I had a pile of zip files all containing different files, but also containing a file called preview.png that I wanted to extract and rename. Since the names of the zip are descriptive I wrote a bash script to loop over the files, extract the preview file and rename it. The interesting bit is stripping the .zip extension.

#!/bin/bash

FILES=./*.zip  
for F in $FILES  
do  
  echo "Processing $F file..."
  unzip "$F" preview.png
  FN=${F%.zip} # strip .zip
  mv preview.png "${FN##*/}.png"
done  

For tar or gzipped tar the following will work in place of the 'unzip' line above:

tar -xf "$F" path/to/preview.png # extract from tar  
tar -zxf "$F" path/to/preview.png # extract from tar.gz