Play Video on Startup of Raspbian on Raspberry Pi

I wanted to be able to play a video on start up of the Raspberry Pi so no human intervention was necessary – you plug in the Pi it boots and plays the selected video(s). This proved a bit tricky in Raspbian so here are the steps required.

1. Create a file and call it playvid.sh put it on the desktop and in a text editor enter the following;

#!/bin/sh

# get rid of the cursor so we don't see it when videos are running
setterm -cursor off																						

VIDEOPATH="/home/pi/vids" 

# you can normally leave this alone
SERVICE="omxplayer"

# now for our infinite loop!
while true; do 
	$SERVICE -r -o hdmi $VIDEOPATH/video_name
	$SERVICE -r -o hdmi $VIDEOPATH/video_name
done

2. Create a file called videostart.desktop and put the following content in the file

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
StartupNotify=true
Name=Play Videos
Comment=Play all videos in /home/pi/vids
Exec=lxterminal --command "/home/pi/Desktop/playvid.sh"
Categories=Application;

3. Put videostart.desktop in /etc/xdg/autostart/

4. Put the videos to play into /home/pi/vids or which ever path you define as the VIDEOPATH variable within playvid.sh which you created in Step 1.

5. Edit playvid.sh (from Step 1) to have the right video names. You can add as many videos as required to the list to play, just copy and paste the following line of code replacing video_name with the full name of the file including the extention (i.e. mp4).

	$SERVICE -r -o hdmi $VIDEOPATH/video_name

6. Simply restart the Raspberry Pi and the videos should start playing.

 

Pushing recordings from DLink DCS-5222L to the Cloud

The DLink DCS-5222L is a brilliant piece of kit. However, it provides limited functionality to push your videos and snapshots to the cloud. Below I describe how I have setup my DCS-5222L to push to the Cloud without intervention.

One solution which I initially used was to install Surveillance Station on my Synology NAS and hook up the DLink so the recordings were copied onto the NAS. However, as my NAS is in the same place as the camera, what if someone comes in, steals the camera, and the NAS? Even with the recordings being synced to an Amazon S3 storage bucket which was done by a scheduled job on the NAS this still meant by the time someone had come in and made off with the NAS and the DLink I would be left with no recordings. So, the solution below was born.

The items involved are quite simple and free (to a point). The steps are described below;

1. Set up an Amazon Web Services (AWS) EC2 Windows Server 2012r2 instance (this is free if you use the basic tier and comes with 30Gb storage!)

2. Set up FileZilla FTP Server running on the instance (follow the instructions here at LifeHacker and further instructions here at How-To Geek).

3. Ensure you open the ports on the Windows Firewall for FTP and also within AWS under Security Groups, within the Security Group your Windows Server is in.

4. Test the FTP server connection from a local machine using the static IP given to you by AWS (or hostname if you set this us, obviously) and ensure you can FTP a file to the EC2 machine. I had issues around running FTP in passive mode, using the troubleshooting steps on the FileZilla wiki here I managed to fix these. I also had/have issues around permissions, I am unable to have my home directory for the FTP account anywhere other then the root of my C: drive, not an issue for me but might be for others.

5. Change the RDP and FTP ports on this box for (slightly) enhanced security – ensure you change the firewall rules before you do this so you don’t block your own RDP connection! To chage the RDP port for Windows Server 2012r2 follow the instructions here. The FTP port should be changed within FileZilla under Edit > Settings and then change “Listen on thse ports”, then click ‘OK’.

6.  Now you are ready to make the changes to the DLink Camera. Login to the camera and access the Setup page. Under Video Clip enter the FTP details of the server you just setup and test the connection. The camera will create a folder on the FTP server if successful and there will be a message telling you if you were successful or not towards the top of the page. You can do the same under the Snapshot section too.

7. Finally, so as to not over fill my EC2 instance I have written myself a small Java application which checks if the CCTV backup is over 5Gb in size and if it is will delete the oldest files until it is back under 5Gb again. This is run hourly as a scheduled windows task. You will find code snippets in my other posts that will help with achieving this if required.

Happy Cloud backup! Now whenever a motion is detected by my DLink DCS-5222L not only does it save the recordings to the Micro SD on board but also pushes the video via FTP to a Cloud based AWS EC2 instance which I can remotely access.

Java – Generating a list of files ordered newest to oldest

I recently had a situation where I needed an ordered list of file names by newest to oldest so I thought I would share the code. This uses the last modified date as in my case that worked and as java.io doesn’t provide the created date only last modified.

First a method which returns an ArrayList of all files within a given directory.

public static ArrayList listFiles(String directoryName) {
		File directory = new File(directoryName);

		ArrayList filenames = new ArrayList();

		// get all the files from a directory
		File[] fList = directory.listFiles();
  		if (fList != null) {
       			for (File file : fList) {
           				if (file.isFile()) {
                					filenames.add(file.getAbsolutePath());
           				} else if (file.isDirectory()) {
                					filenames.addAll(listFiles(file.getAbsolutePath()));
           				}
        			}
		}
		return filenames;
	}

We will then call the above method when building the ordered list of files. As below;

public static Map<String, Long> orderedListOfFiles(String directoryName) {
		Map<String, Long> fileMap = new HashMap<String, Long>();

		// get all the files from a directory
		ArrayList filePaths = listFiles(directoryName);
		for (String path: filePaths) {
     			File file = new File(path);
     			fileMap.put(path, file.lastModified());
		}
		
		List<Map.Entry<String, Long>> list = new LinkedList<Map.Entry<String, Long>>(fileMap.entrySet());
		
		Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {

				public int compare(Map.Entry<String, Long> m1, Map.Entry<String, Long> m2) {
					if (m1.getValue() > m2.getValue())
	            		return -1;
	                return 1;
				}

	        });
		 
		  Map<String, Long> result = new LinkedHashMap<String, Long>();
	        for (Map.Entry<String, Long> entry : list) {
	            result.put(entry.getKey(), entry.getValue());
	        }
		
		return result;
	}

To switch oldest to newest simply modify the compare method implemented inside the Comparator class.