Thursday, April 20, 2017

Dealing with WiFi IP Cameras and .264 files

On a recent visit to China, I purchased a cheap IP camera which looks like a robot:

You can get one online from amazon for twice the amount that you might pay for it when you buy it from one of the shops in ShenZhen (I got it for around 150 CNY or 1500 INR). The camera is supposed to be installed in your house where you can configure it to connect to your home's WiFi router. That way you can connect to the camera from anywhere in the world (using your web browser or smart phone app) and watch and hear what ever the camera picks up. Here are some of the features of this camera:

  • Tech Specs (you can find these on the amazon page as well, although the model might say something else)
    • Model: The packaging for my camera said "Mini Robot Wireless Camera H811 IP Camera"
    • Power: DC Input 5V 2Amps (via microUSB connector)
    • Image quality: 960P 1.3Megapixel
  • WiFi:
    • Out of the box, the camera defaults to WiFi master mode (i.e. wifi hotspot) . This is so that you can use the app on your smartphone to connect to it and if required, you can configure the camera to switch to client mode and connect to your home's WiFi router.
    • By default, when you look for it, the SSID of the WiFi signal emitted by the camera would be something like: "HSL-337026-GDDEN"
    • The default WiFi WPA2 key is 0123456789
    • Android app:
      • The app for your smart phone is called NetCam (Google Play Store link)
    • Computer:
      • In case you connect to the camera using a laptop, punch in the following URL into your web browser: http://192.168.246.1:81/
      • It will ask you for login credentials, enter "admin" as login and leave the password blank.
  • Reset button and microSD card slot:
    • The camera has a microSD card slot on the back. If you install a microSD card, the camera will also record the video (only video no audio) onto the SD card.
    • The small hole next to the camera on the back is for resetting the camera in case you forget its login password (this is separate from the WiFi password)
  • Physical structure:
    • The bottom part of the camera is made of iron and the camera head itself has a magnet in it. This allows you to screw in the bottom of the camera wherever you want and them attach the camera head and orient it in any direction you want.
    • The bottom of the camera also has a QR code which you can scan with your smart phone app to connect to it for the first time. The QR code has the WiFi Master SSID encoded into it.

So what did I do with the camera? I mounted it in my car as my dashcam. The problem is that the camera does not have a battery backed real time clock (RTC) and there is no internet (which would have helped re-sync the clock because the camera supports network time protocol or NTP) in my car so the camera does not maintain the time when I turn off the car (and power to the camera is cut). I use a 2 amp 5V smart phone charger that plugs into my cigarette lighter to power the camera. I could have connected the camera directly to the 12V battery and that would have ensure that the camera ran continuously and never lost time, but then my car battery would get drained as well.






I installed the base (made of iron) of the camera onto my rear view mirror and the camera heat got attached to the base thanks to the magnet present inside the head.

About RTC and NTP:
As mentioned above, this camera does not have any battery (neither Li-ion or coin cell) to main clock when the car is turned off. The camera can act as a WiFi client and can synchronize its time at boot up using an NTP server. But for I would need to get an internet connection for my car - possibly a 3G/4G hotspot. In cases where one does not want to do all that, there is a way out. The Global Positioning System is essentially made up of atomic clocks orbiting Earth. We can use a GPS receiver along with a WiFi capable microcontroller and implement the NTP server functionality. There is a tutorial on instructable on how to go about it: http://www.instructables.com/id/ESP8266-Wireless-NTP-Stratum1-Server/ - what this instructable allows you to do is to make a small WiFi Access Point (using ESP8266) that doubles up as an NTP server. When you connect your devices (this camera or your smart phone) it would be able to access the internet but you can configure the NTP server address to point to this ESP8266 device and atleast your time synchronization will work. (Another similar project)

About handling the .264 media files on a Windows desktop
You can connect to the camera using the Android app to watch live video and configure the camera, but there is no way for you to review the video recorded by the camera while the memory card is inside the camera. The android app has no option to playback the recordings. The only way is for you to remove the memory card and insert it into a memory card reader and either connected it to your computer or to your android phone using a USB OTG cable.
The moment you remove the microSD card and insert it into a card reader and attach it to your Windows PC, this is what you see:


The camera fills up the microSD card with equal sized stub files

The camera has filled up the memory card with stub files. The camera starts recording the video into first file. When one file fills up (around 5 minutes worth of video), it continues recording in to the next file in sequence. This ensures that in case the power is cut to the camera (e.g. car accident/break in), atleast the last file would have been saved properly and would help in investigation.

Playing back on Desktop:
You can use VLC Media Player to play these .264 files. You have to make a small change in the settings:

  1. Select View>Preferences
  2. Switch to "All" under Show Settings
  3. Click on "Demuxers" under Input/Codecso
  4. Select "H264 video demuxer" from the drop down list and click Save
  5. Now you can drag and drop .264 files into VLC media player and play them back
  6. Make sure to change the Demux module setting from "H264 video demuxer" back to "Automatic" once you are done with playing .264 files. This will allow you to play your regular video files correctly.
Here are the screenshots to clarify the above steps:





You will note that when playing .264 files, you don't hear any audio. I am not sure if the camera records audio or not but, according to what I have read in discussion forums, no one has gotten any audio playback from these raw H264 format video files.

Converting videos on Desktop:
In case you want to convert the video to mp4 format, there are two steps that you have to follow:

  1. Use ffmpeg to convert the .264 file in to raw uncompressed AVI file. This will take care of stream errors which usually creep in when the power supply to the camera (like in a car) is not reliable or when the camera experiences vibration.
  2. Use ffmpeg to convert the uncompressed file to mp4

Here are the steps in the conversion process:
  1. Visit http://ffmpeg.zeranoe.com/builds/ and download the latest stable, static 32bit version.
  2. Unzip the file into a folder on your computer.
  3. Move your .264 video file into the bin folder of the ffmpeg folder
  4. Open a command prompt and cd to the ffmpeg/bin folder
  5. Issue the command ffmpeg -i apf00047.264 -vcodec rawvideo -y output.avi
    This will begin converting the file apf00047.264 into a raw uncompressed file.
    During the conversion process, ffmpeg will display a number of messages about finding errors in the stream and compensating for it. Once the conversion process is done, you will realise that the 64MB file has been converted to an .avi file occupying 6.8 GB of space. You can try playing this file in VLC player (make sure to set Demux module back to Automatic)
  6. Finally you can convert the uncompressed avi file to mp4 format using the command:
    ffmpeg -i output.avi output.mp4
    Once that is done, you can delete the output.avi uncompressed video file. The output.mp4 video file can be uploaded to Youtube and Facebook if required. The .mp4 file can also be played using VLC player. It of course wont have any audio - that's the issue of the camera. The output.mp4 would be around 107 MB in size.
Here some screenshots outlining the above steps:

Downloading the ffmpeg utility for Windows

Copy your .264 video file to the bin folder within the extracted ffmpeg folder

Opening a command prompt and navigating to the ffmpeg's bin folder

Issuing a command to start conversion of .264 to uncompressed .avi format
ffmpeg spews messages regarding stream errors while creating uncompressed .avi file
The uncompressed output.avi file is huge - 6.8 GB
You can play the uncompressed output.avi in VLC Player

Executing ffmpeg a second time to convert uncompressed .avi to .mp4 format

Playing output.mp4 in VLC Player


Playing back on Android Phone:
You can use a USB OTG cable along with a card reader to connect the microSD card to your android phone. Once you do that - and if your phone supports USB OTG - a notification will pop up allowing you to explore the files on the microSD card. Now you can use this file explore feature and long press the .264 file that you are interested in to select it. Once selected, copy it to your phones internal memory. Then start the MX player app. The MX player (Free version with ads | pro version) app scans the phone's memory for video files, and this .264 file will show up as well. Just tap on it and the video will start playing.

Use the OTG Cable to connect card reader to the Android phone.
Explore the file using Android's builtin feature an copy the files to phone's internal memory.

MX Player app shows the recently copied .264 video file.


Tap on the file to play it.





1 comments:

saurabh said...

Cool

Post a Comment