All files transcode.ts

90.28% Statements 65/72
86.96% Branches 40/46
94.12% Functions 16/17
89.86% Lines 62/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 1601x 1x     1x               1x 1x 1x     1x                           10x 10x     1x 10x 10x 10x     1x 10x 6x   4x     10x     1x 10x 7x   3x   10x     1x 2x 2x     1x 9x 9x     1x 3x 3x     1x 10x 10x     1x 10x       10x       10x   10x       10x 10x       10x       10x                           10x   10x 10x   1x   1x           10x 10x 10x     1x 1x 1x     1x         1x 10x 10x     1x 10x           1x  
import { API } from "./api";
import { removeUndefinedFromObj } from "./utils";
import { Blob } from "./blob";
import { Watermark } from "./watermark";
import { Job } from "./job";
import { Webhooks } from "./webhooks";
import { Executable } from "./Executable";
 
export type Encoder = "h264" | "h265" | "vp8" | "vp9";
 
export type Bitrate = "1000" | "2000" | "4000";
 
export enum Container {
  MP4 = "mp4",
  WEBM = "webm",
}
 
export class TranscodeJob implements Executable {
  apiKey: string;
  successUrl?: string;
  failureUrl?: string;
  inputUrl?: string;
  inputBlob?: Blob;
  outputUrl?: string;
  outputBlob?: Blob;
  transcodeWidth?: number;
  transcodeHeight?: number;
  transcodeWatermark?: Watermark;
  transcodeOpts: TranscodeOpts;
 
  constructor(apiKey: string) {
    this.transcodeWidth = 720;
    this.apiKey = apiKey;
  }
 
  webhooks(webhooks: Webhooks): TranscodeJob {
    this.successUrl = webhooks.successUrl;
    this.failureUrl = webhooks.failureUrl;
    return this;
  }
 
  from(source: string | Blob): TranscodeJob {
    if (typeof source === "string") {
      this.inputUrl = source;
    } else {
      this.inputBlob = source;
    }
 
    return this;
  }
 
  to(destination: string | Blob): TranscodeJob {
    if (typeof destination === "string") {
      this.outputUrl = destination;
    } else {
      this.outputBlob = destination;
    }
    return this;
  }
 
  watermark(watermark: Watermark): TranscodeJob {
    this.transcodeWatermark = watermark;
    return this;
  }
 
  width(value: number): TranscodeJob {
    this.transcodeWidth = value;
    return this;
  }
 
  height(value: number): TranscodeJob {
    this.transcodeHeight = value;
    return this;
  }
 
  opts(value: TranscodeOpts): TranscodeJob {
    this.transcodeOpts = value;
    return this;
  }
 
  async execute() {
    Iif (this.apiKey === null) {
      throw new Error("Missing apiKey");
    }
 
    Iif (this.apiKey.trim() == "") {
      throw new Error("Missing apiKey");
    }
 
    const emptyInputUrl = !this.inputUrl || this.inputUrl.trim() === "";
 
    Iif (!this.inputBlob && emptyInputUrl) {
      throw new Error("Missing inputBlob or inputUrl");
    }
 
    const emptyOutputUrl = !this.outputUrl || this.outputUrl.trim() == "";
    Iif (!this.outputBlob && emptyOutputUrl) {
      throw new Error("Missing outputBlob or outputUrl");
    }
 
    Iif (!this.transcodeOpts) {
      throw new Error("Missing transcodeOpts");
    }
 
    const body = {
      apiKey: this.apiKey,
      successURL: this.successUrl,
      failureURL: this.failureUrl,
      inputCreds: this.inputBlob?.toApiCredentials(),
      outputCreds: this.outputBlob?.toApiCredentials(),
      inputURL: this.inputUrl || this.inputBlob.toApiUrl(),
      outputURL: this.outputUrl || this.outputBlob.toApiUrl(),
      width: `${this.transcodeWidth}`,
      height: !!this.transcodeHeight ? `${this.transcodeHeight}` : undefined,
      watermark: this.transcodeWatermark?.toJSON(),
      ...this.transcodeOpts?.toJSON(),
    };
 
    const resp = await API.createJob("transcode", removeUndefinedFromObj(body));
 
    const job = new Job(resp.data.id);
    return job;
  }
}
 
export class TranscodeOpts {
  transcoderEncoder: Encoder;
  transcoderBitrateKbps: Bitrate;
  transcoderContainer: Container;
 
  constructor() {
    this.transcoderEncoder = "h264";
    this.transcoderBitrateKbps = "2000";
    this.transcoderContainer = Container.MP4;
  }
 
  encoder(value: Encoder): TranscodeOpts {
    this.transcoderEncoder = value;
    return this;
  }
 
  bitrateKbps(value: Bitrate): TranscodeOpts {
    this.transcoderBitrateKbps = value;
    return this;
  }
 
  container(value: Container): TranscodeOpts {
    this.transcoderContainer = value;
    return this;
  }
 
  toJSON() {
    return {
      encoder: this.transcoderEncoder,
      bitrateKBPS: this.transcoderBitrateKbps,
      container: this.transcoderContainer,
    };
  }
}