Skip to content

Commit

Permalink
fix: parse all clip info from the "disk list" command into separate f…
Browse files Browse the repository at this point in the history
…ields
  • Loading branch information
Alex Van Camp committed Mar 17, 2023
1 parent 29fa5bf commit 8d2dcb9
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/commands/diskList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ import { SlotId } from '../enums'
import { NamedMessage, ResponseMessage } from '../message'
import { AbstractCommand } from './abstractCommand'

const deserializeRegex = /(?<fileName>.+) (?<codec>\w+) (?<format>\w+) (?<timecode>[\d:]+)$/i
const framerateRegex = /\d+[ip](?<frameRate>\d+)/i
const timecodeRegex = /(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+):(?<frames>\d+)/i

export interface Clip {
/** 1, 2, 3, etc */
clipId: string
/** ExampleVideo.mov, etc */
name: string
/** QuickTimeProRes, etc */
codec: string
/** 1080i50, 720p60, etc */
format: string
/** hh:mm:ss:ff */
timecode: string
/** milliseconds */
duration: number
}

export interface DiskListCommandResponse {
Expand All @@ -25,14 +39,33 @@ export class DiskListCommand extends AbstractCommand {

deserialize(msg: ResponseMessage): DiskListCommandResponse {
const clipIds = Object.keys(msg.params).filter((x) => x !== 'slot id')
const clips = clipIds.map((x) => {
const clip: Clip = {
clipId: x,
name: msg.params[x],
}

return clip
})
const clips = clipIds
.map((x) => {
const match = msg.params[x].match(deserializeRegex)
const frameRateMatch = match?.groups?.format.match(framerateRegex)
const timecodeMatch = match?.groups?.timecode.match(timecodeRegex)
if (match?.groups && frameRateMatch?.groups && timecodeMatch?.groups) {
const frameRate = parseInt(frameRateMatch.groups.frameRate, 10)
const msPerFrame = 1000 / frameRate
const hoursMs = parseInt(timecodeMatch.groups.hours, 10) * 60 * 60 * 1000
const minutesMs = parseInt(timecodeMatch.groups.minutes, 10) * 60 * 1000
const secondsMs = parseInt(timecodeMatch.groups.seconds, 10) * 1000
const framesMs = parseInt(timecodeMatch.groups.frames) * msPerFrame
const clip: Clip = {
clipId: x,
name: match.groups.fileName,
codec: match.groups.codec,
format: match.groups.format,
timecode: match.groups.timecode,
duration: hoursMs + minutesMs + secondsMs + framesMs,
}

return clip
}

return undefined
})
.filter((clip): clip is Clip => Boolean(clip))

const res: DiskListCommandResponse = {
slotId: parseInt(msg.params['slot id'], 10),
Expand Down

0 comments on commit 8d2dcb9

Please sign in to comment.