IP Camera – James Batchelor https://james-batchelor.com Useful I.T & VoIP Ramblings Tue, 07 Apr 2020 18:10:16 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.5 https://james-batchelor.com/wp-content/uploads/2025/05/cropped-cropped-logo-jb-202505-32x32.png IP Camera – James Batchelor https://james-batchelor.com 32 32 Working From Home – Pi Camera as Windows Webcam https://james-batchelor.com/index.php/2020/04/07/working-from-home-pi-camera-as-windows-webcam/ Tue, 07 Apr 2020 18:10:16 +0000 http://james-batchelor.com/?p=658 Continue reading "Working From Home – Pi Camera as Windows Webcam"]]> Hopefully the mad dash for home working is over, and now everyone who can has settled in to a comfortable home setup with new knowledge of what a VPN and remote desktops are all about.

I thought my established home setup was great, however I didn’t envisage words like Teams and Zoom to become the buzzwords of companies the world over.

This posed an issue for myself. Even though my 2017 Dell XPS has a webcam ready to go, it spends its home office days docked with the lid closed. When opening the 4K screen combined with the Full HD monitors, Windows implementation of scaling rears is appalling head and just looks terrible. Combined with the Dell’s decision to locate the webcam below the screen to give maximum nostril-cam angle, its not something I like to use.

Buying a USB webcam didn’t seem worth it for what is (hopefully at time of writing) a temporary solution.

So in comes another Pi project, this time using a spare Raspberry Pi Camera module connected to an aptly placed Pi, that can be used on a Windows machine for the software likes of Zoom, Skype and Teams…

Pi Setup

Enable Camera

Using a fresh install of Raspbian Buster, locate and login to SSH of the Pi and run the configuration utility:

sudo raspi-config

In the menu, navigate to 5. Interfacing Options, then P1 Camera, and enable.

Navigate back to the main menu and Enter on Finish, select Yes if prompted to reboot.

Set Static IP

The camera will be accessed over the network, so makes life easier and more reliable for it to always be found on the network in the same location. In an SSH session, enter:

sudo nano /etc/dhcpcd.conf

Scroll to the bottom of the page and enter the following:

static ip_address={IP ADDRESS}/24
static routers={ROUTER ADDRESS}
static domain_name_servers={ROUTER ADDRESS}

Install Software

The software I’d prefer for this task is MJPEG Streamer, a lightweight camera image generator that has come to my aid when setting up a CCTV system.

Start with installing the dependencies:

sudo apt-get install build-essential imagemagick libv4l-dev
libjpeg-dev cmake

Download the MJPEG Streamer files from git:

git clone https://github.com/jacksonliam/mjpg-streamer.git

Navigate the folder in order to compile:

cd mjpg-streamer/mjpg-streamer-experimental

Then compile:

make

No errors? Go and install it:

sudo make install

This will install the program, and leave you at the command line with little fanfare…

Testing

Sanity check, and time to see if the software will work, from the command line run the following:

/usr/local/bin/mjpg_streamer -i "input_uvc.so -r
1280x720 -d /dev/video0 -f 30" -o "output_http.so -p 8080 -w
/usr/local/share/mjpg-streamer/www"

These are the common variables should you need to adjust anything to get started:

-r 1280x720 – Set stream output resolution
-f 30 – Framerate
-p 8080 – Port the stream binds to

From another PC, open a browser and navigate to http://{IP ADDRESS}:8080/?action=stream where {IP ADDRESS} is that of the Raspberry Pi.

Launch at Startup

You may notice when testing the above test command that it occupies the command line whilst running, and doesn’t allow any other tasks, exiting the program by using Ctrl + C also stops the video stream.

Therefore its more convenient to have the camera stream start when the Pi starts, and have it run unhindered in the background to give easy access to the Windows machine.

Script File

Before it can start automatically, the command needs to be wrapped in a script file and executed:

Open a new text file with nano, located in the Pi (Default user) home directory:

sudo nano /home/pi/camstart.sh

Paste the command tested above into the file, then save and exit with Ctrl+O and Ctrl+X

This file needs to be executed, so give it execute permissions:

sudo Chmod +x /home/pi/camstart.sh

Script made, it needs to be run at boot, this can be done with cron:

sudo crontab -e

Enter the following on a new line at the bottom of the file:

@reboot /home/pi/camstart.sh

Use Ctrl+O and Ctrl+X to save and exit.

Now to see if it worked, restart the Pi gracefully with:

sudo shutdown -r now

When it re-appears, visit http://{IP ADDRESS}:8080/?action=stream again and see if you have a live stream.

Windows Setup

Now the camera is setup and broadcasting the image over the local network, we need something on the windows machine to capture and convert it and make it recognisable by video conferencing software.

There are a number of applications that can achieve this, but often it is an addon to a much larger software suite and would be considered to be bloatware to what we want to achieve.

Cue IP Camera Adapter 3.1 https://ip-webcam.appspot.com/ as a lightweight application that has just does the conversion process and nothing else.

Run the setup wizard and upon installation, run the “Configure IP Camera Adapter” from the start menu:

Enter http://{IP ADDRESS}:8080/?action=stream into the URL field.

And test the link by pressing Autodetect, the message with tell you if it was successful:

Final Testing

From here the camera should be available on most video software, you will just have to update it to use the newly added camera, this will be labelled as MJPEG Camera:

]]>
LibreElec – Pi Camera Mjpeg Streaming https://james-batchelor.com/index.php/2019/08/10/libreelec-pi-camera-mjpeg-streaming/ https://james-batchelor.com/index.php/2019/08/10/libreelec-pi-camera-mjpeg-streaming/#comments Sat, 10 Aug 2019 19:20:39 +0000 http://james-batchelor.com/?p=623 Continue reading "LibreElec – Pi Camera Mjpeg Streaming"]]> Following the setup of a Cent OS CCTV server, I’ve been using Raspberry Pi’s as video sources. But what if there was a Raspberry Pi in perfect situ for a CCTV camera, but was already in use as a media player?

A Linux system has always had the impression that it is versatile, so this should be an achievable task. A barrier would be how to get this done with the operating system installed, in this case it is LibreElec, an OS with the tagline “Just enough OS for Kodi”. Therefore, it would be more of a challenge than a usual Debian install.

The team at LibreElec saw this type of thing coming, and included the Docker service as a Kodi addon to allow the curious tinkerer to add more than Kodi to a Pi.

If you have the LibreElec based Pi in the opportune placement to add a camera, here is how to add Mjpeg streaming capabilities…

Add the Addons

Via Kodi on the screen, goto:

Addons --> Install from Repositories --> Services

Then install the two addons required:

RaspiTools
Docker

CLI Access

To setup Docker and its container (the Mjpeg streamer) requires Command Line Interface (CLI) access to the Pi, if not already enabled during setup enable it via Kodi by navigating to:

Settings --> LibreElec --> Services --> SSH

Mjpeg streamer

The M-Jpeg-streamer is a well-used Linux library, and have chosen the Open-Horizon version of a Docker image for this task.

Log in to the Pi via SSH and run the following:

docker pull openhorizon/mjpg-streamer-pi3:latest

Follow the prompts and be prepared to wait as the Docker image builds.

When complete, start the Docker container with:

docker run --restart=always -it -d --privileged -p 8081:8080
openhorizon/mjpg-streamer-pi3 ./mjpg_streamer -o "output_http.so -w
./www" -i "input_raspicam.so -x 1280 -y 720 -fps 10 -ex night"

This code explained:

docker run: start a Docker container
–restart=always: Restarts a container if the system is restarted
-it: allocate a pseudo-TTY for debugging and to stop it.
-d: Run in background, enables CLI to exit without stopping container.
–privileged: Give privileges that allows access to the camera.
-p 8081:8080: Translate streaming port from 8080 to 8081, as not to conflict with Kodi.
openhorizon/mjpg-streamer-pi3 ./mjpg_streamer: name of Docker image.
-o “output_http.so -w ./www”: internal reference.
-i “input_raspicam.so -x 1280 -y 720 -fps 10 -ex night”: Camera settings, set resolution, frames per second and Pi camera filters.

Run

The Docker container should now be running, check this by entering this in the CLI:

docker ps -a

To show running containers.

Now you can access the web GUI by visiting http://ip_address:8081 to test.

From here it can be added as a Network Camera to MotionEye.

Configuration

During setup its important to consider the hardware it running on. This was running on a Wi-Fi only Raspberry Pi 3A+, while it’s processing power was more than adequate for my initial setup of 1920×1080@20fps this saturated the network connection, leaving no bandwidth left to stream the media LibreElec was designed for. The reduction to 1280×720@10fps reduced the active bandwidth enough as not to interrupt the media players primary function.

]]>
https://james-batchelor.com/index.php/2019/08/10/libreelec-pi-camera-mjpeg-streaming/feed/ 1
Automated Time-lapse Solution https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/ https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/#comments Sat, 03 Sep 2016 20:26:08 +0000 http://james-batchelor.com/?p=439 Continue reading "Automated Time-lapse Solution"]]> A while ago I worked on a simple CCTV system for work, which involved using software to capture images every second then batch convert them to video every five minutes. It was crude but did the job.

I also dabbled in the past with time lapse videos, however this was a more manual process with images capturing to a folder, then personally loading them into Windows Movie Maker to create the video. With the tedium of creating the videos, the software I used for capturing (YAWCam) would hang after a few weeks constant running, not to mention without upkeep, the hundreds of thousands of image files populating the hard drive.

As a project it was time to combine the learnings from these and create an automated time lapse video creator, a program that would capture images, then create a 5-minute video that contains the days’ footage, and finish by deleting the temporary images to leave just the days video.

image104062

As with the CCTV project, a batch file will be used to create the process as what I need can be achieved by DOS commands plus it’s able to be called by Windows Task Scheduler. This time I will have the advantage of using an IP camera instead of USB, so negating the need for software to capture images, however the WGet utility is needed to download and save the images locally. Also the script will invoke FFMpeg to convert the images to video at the end of the day.

Setup

Getting started, place the WGet utility in the System32 folder so it can be called from dos without specifying a path. Then create this folder hierarchy on the root of C:\ drive:

Timelapse File Structure

Here’s the code for capture.bat, I placed this in the Data folder with ffmpeg.exe for neatness:

@echo off
::SET DATE TO A VARIABLE
set todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%
::RESET COUNTER TO ZERO
set counter=100000

::CREATE TODAYS FOLDER AND NAVIGATE TO IT
echo Creating Folder…
c:
cd \cam\Data\temp\
md %todaydate%
cd %todaydate%
::LOOP START
echo Capturing Images…
:capture
::GET IMAGE FROM WEBCAM CGI
wget -q http://xxx.xxx.xxx.xxx/image/jpeg.cgi

::RENAME FILE USING COUNTER
ren jpeg.cgi image%counter%.jpg

::INCREMENT COUNTER BY ONE
set /a counter=counter+1

::SET TIME TO VARIABLE
set hour=%time:~0,2%
if “%hour:~0,1%” == ” ” set hour=0%hour:~1,1%
set min=%time:~3,2%
if “%min:~0,1%” == ” ” set min=0%min:~1,1%
set secs=%time:~6,2%
if “%secs:~0,1%” == ” ” set secs=0%secs:~1,1%
set thetime=%hour%%min%%secs%

::TIME BEFORE NEXT IMAGE
timeout /t 3 >nul

::CHECK ITS NOT END OF DAY
if %thetime% LSS 120600 (
goto capture
) else (
::START CONVERTING VIDEO
echo Processing Timelapse Video…
“C:\cam\Data\ffmpeg.exe” -loglevel quiet -i C:\cam\Data\temp\%todaydate%\image1%%05d.jpg -r 28 -c:v libx264 C:\cam\Video\%todaydate%.mp4
echo Deleting temp files…
::TIME DELAY TO ALLOW PROCESS TO CLOSE
timeout /t 10 >nul
::CHANGE DIRECTORY OUT OF FOLDER TO BE DELETED THEN DELETE TEMP FILE
cd \cam\Data\temp\
RD /S /Q “C:\cam\Data\temp\%todaydate%”
)
echo Process Complete

Next is to create a Scheduled Task in Windows to launch the program each day at midnight, this process varies by system but ensure that user privileges are high enough to write and execute on disk.

Timelapse Scheduled Task

I found Windows Server 2012 R2 had a lot more options over Server 2003, so I set the task to run when even when not logged on, to run hidden, and now the time can be specified to the second, so chose to start task at 00:00:03 to make sure the date is correct.

Notes

Hopefully the annotation will be helpful enough if you would like to apply your own tweaks, but here are some of notes about the issues I faced when creating the script:

  • Originally the images named with a timestamp, however FFMpeg requires images to be sequentially numbered to create the slideshow.
  • The counter was set at 000000, but on the first loop the arithmetic to increase the counter changed the value to 1, so lines were added to include the leading zeros
  • The leading zeros meant that the machine was reading the number as an octet, so on the 8th loop the counter reset to 000001, as a workaround a leading 1 was added to the counter.
  • Capturing images every 10 seconds equates to 8640 images a day, the counter it set to five decimals to ensure no looping of counter numbers.
  • In addition to storing the date as a variable to allow multiple calls, it allows the program to keep the same date after time moves into the next day. Since the video conversion starts at 23:59 each day its very likely the process will move into the next day when temp files are deleted.
  • Initially the time variables were inside the IF statement, but learned that the variable with an IF statement are created when the program is parsed, not run, therefore would not update.
  • However, the time needs to be in the loop to update before the IF statement is reached, the date is purposely kept out to it retains the value set at the start of the script.
  • While statements don’t really exist in batch files, so goto and If commands were used to create a crude while loop.

I dare not say that the code is perfect, or if the choice of programming language is suitable, the aspiration would be to create a self contained executable with a configuration file to specify target folders, change finish time and time between image captures. But for now it’s doing the job just fine.

Update

Since creating the program I’ve reused parts of the code to create a preview program that compiles the images taken so far that day into a video, without deleting any files. Here’s the code, again, save as a bat file and double click to run immediately.

@echo off

echo Establishing todays date…
::FIND TODAYS DATE TO ESTABLISH FOLDER NAME
set todaydate=%date:~-4%-%date:~3,2%-%date:~0,2%

::FIND TIME TO CREATE FILENAME VARIABLE
set hour=%time:~0,2%
if “%hour:~0,1%” == ” ” set hour=0%hour:~1,1%
set min=%time:~3,2%
if “%min:~0,1%” == ” ” set min=0%min:~1,1%
set secs=%time:~6,2%
if “%secs:~0,1%” == ” ” set secs=0%secs:~1,1%
set thetime=%hour%%min%%secs%

echo Locating temp files…
::NAVIGATE TO IMAGE FOLDER
C:
cd \cam\Data\temp\
cd %todaydate%

echo Creating preview video
::CREATE VIDEO AND ADD PREVIEW IN FILENAME TO AVOID DUPLICATE FILENAMES
“C:\cam\Data\ffmpeg.exe” -loglevel quiet -i C:\cam\Data\temp\%todaydate%\image1%%05d.jpg -r 28 -c:v libx264 V:\Timelapse\Video\preview-%todaydate%-%thetime%.mp4

echo Complete

]]>
https://james-batchelor.com/index.php/2016/09/03/automated-time-lapse-solution/feed/ 1
Easy CCTV Solution using IP Cameras https://james-batchelor.com/index.php/2016/09/01/easy-cctv-solution-using-ip-cameras/ Thu, 01 Sep 2016 15:32:01 +0000 http://james-batchelor.com/?p=447 Continue reading "Easy CCTV Solution using IP Cameras"]]> For a while I have been looking for simple CCTV solution, where video is captured on a long loop, so when the storage is full the earliest dated footage is deleted to make way for new. And of course, footage is available for immediate review.

Many newer IP cameras, including my Trendnet TV-IP572W comes equipped with a microSD slot for recording on a rotating basis. However, this has two main caveats, firstly the investment in a microSD card to be used solely for this purpose and of a high enough capacity to record enough footage, especially with the introduction of HD capture. Secondly is accessing the footage, as it is effectively held on the IP camera it is the gateway to the data. In my experience this process is slow, with having to download each video file manually and slow transfer speeds.

Imagine wanting to view an event that could have happened over a span of a few hours, and with video captured in segments of 5 minutes at most, the whole process can become tedious very quickly.

Therefore, I came up with another solution, one that uses my server’s hard disks for video storage to save on money while allowing larger video retention than a micro SD card. Also the ability to automatically delete older files to make way for new. This method uses Samba settings of an IP camera to save video to a Windows Server, and on the server itself, employing Disk Quota management to effectively trick the camera into thinking it only has a certain amount of disk space, to allow the cyclic video retention and prevent the footage taking up a whole drive on the server.

The benefit of employing Disk Quota management is that many cameras can record to the same volume or drive without fighting over space, plus with careful space management the days’ retention per camera can be manipulated. For instance an outdoor facing camera can be allocated more data and therefore more footage to search for suspicious activity, while an indoor camera can be set to record just a day’s footage in case of break ins.

Here’s how…

The heart of this system is to assign a user account per camera; this gives the ability to manipulate data capacity for each feed.

On the server

Open up Computer Management, on newer systems this is found by right clicking the Windows icon.

ecs01

In System Tools, Local Users and Groups, right click Users and select New user.

ecs02

Enter the details in the new user field, ensuring to check User cannot change password and Password never expires. Then click Create.

ecs03

Next, on the drive of the video storage, right click and choose Properties, then choose the Quota tab.

Enable quota management by clicking the Enable quota management checkbox, and check the Deny disk space to users exceeding limit.

ecs04

Then click on Quota Entries, then the new entry button ecs09.

Time for the drawn out Windows user selection, click Advanced, on the next window click Find now, then choose the newly created cctv account, then OK and OK to bring up the Add New Quota Entry.

ecs05

Set the disk limit for this user, this is the maximum the camera will store on the drive, you can specify a warning level but most IP Camera will ignore this setting. On clicking OK the user will appear in the Quota Entries Windows, this window can then be closed.

Click OK on the drive properties window and acknowledge the warning, Disk Quota will be active.

On the Camera

Setting the camera up can vary drastically on the model, in the setup menu the recording menu needs to be found.

ecs08

In the example of the Trendnet TV-IP572W, in the setup menu, choose Action then click the Recording checkbox to open the settings dialog.

Dependant on the camera settings, look for an option to save to a remote machine/server, or in this case a Samba share.

Use the credentials added to the Quota Management, and if available choose the Cyclic / Loop option so the camera manages deletion of the oldest files.

Review

Finally it’s time to wait for the quota to fill, and then check to see if the older data is being deleted automatically. For a more in depth review open the Quota Entries window, the camera will use the maximum available so will always be triggering the warning status, but this means that the maximum amount of data is available for review.

ecs10

]]>