CCTV Storage Calculator
Tags: ios, swift, productivity
Published: 2019-10-13 16:08:44

CCTV storage is getting pretty intense lately. With the introduction of higher resolution cameras, it's important to calculate the proper amount of storage to ensure the end user has enough record time.

Let's jump right into it!

So what factors do we need to calculate the storage?

```swift
// Total cameras
var totalCameras: Int = 0
// We'll get to cameraBitrate later
var cameraBitrate: Int = FrameSize.twoMegapixel.rawValue
// Total days
var totalDays: Int = 30
// Total hours per day
var totalHours: Int = 24
// Storage in GB in-case we are below 1000GB (1TB)
var storageGB: Double = 0.0
// Storage in TB in-case we are above 1000GB (1TB)
var storageTB: Double = 0.0
// Compression will refer to H.264 or H.265. We'll also get to this later
var compression: Double = CompressionMethod.h264.rawValue
// Frames per second
var framesPerSecond: Int = 7
```

Awesome! Now let's get back to the cameraBitrate. For this, let's create an enum to grab different values based on the current selected camera bitrate.

```swift
enum FrameSize: Int {
   case twoMegapixel = 20
   case threeMegapixel = 26
   case fourMegapixel = 36
   case fiveMegapixel = 46
   case eightMegapixel = 72
}
```

These values are the size of each frame for each megapixel. We'll multiply the selected megapixel by the framesPerSecond to get the total storage per second. Once we have that number, we can simply multiply it by 3,600 to get our total storage per hour in GB.

```swift
// Example: 7 (FPS) * 50KB (twoMegapixel) = 350KB/s
let totalFrameSizePerSecond = (framesPerSecond * cameraBitrate)      
// Example: 350KB/s * 3600 (seconds/hour) = 1_260_000KB/hr
let totalSizePerHour = totalFrameSizePerSecond * convertSecondsToHour
```

Now that we have our total storage size per hour, let's multiple that number by the total number of cameras to get our total storage per day

```swift
storageGB = totalSizePerHour * totalCameras
```

Sweet! Now we have our total size per hour for the total number of cameras. Now we need to multiply that number by the number of recording hours (totalHours). Some systems do not require recording 24 hours per day. So we'll use totalHours given to us by the user to calculate storage according to their needs. We could simply add this to the original storageGB calculation above, but I wanted to break it out to explain.

```swift
storageGB = storageGB * totalHours
```

The last calculation we need to make before determining whether we will display GB or TB is the compression method. In the CCTV world, there are a few compression methods available. For this instance we will stick with the main two: H.264 & H.265. Our calculation so far has been based on H.264, so if we determine the user is utilizing H.264, we will simply display the storage already calculated. On the other hand, if the user is utilizing H.265, we will need to recalculate the total based on that compression method. H.265 compression saves storage by roughly 30%, so our calculation isn't too difficult.

For this, let's create a simple enum to make life easier.

```swift
enum CompressionMethod: Double {
   case h264 = 1.0
   case h265 = 1.3
}
```

Now, we can write a simple if statement to determine the storageGB based on the users compression method.

```swift
if compression == CompressionMethod.h265 {
   storageGB = storageGB * CompressionMethod.h265.rawValue
} else {
   storageGB = storageGB * CompressionMethod.h264.rawValue
}
```

Now that we have our storage in GB with all variable calculated, let's determine if we need to convert to TB or not using a simple if statement.

```swift
if storageGB > 1000 {
   storageTB = storageGB / 1000
}
```

That's it! We've calculated total storage for our CCTV system using some simple calculations.

Thanks for reading!