From d25e8890e0670d698632d991497e610d17440fbc Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 19 Mar 2024 15:46:29 -0500 Subject: [PATCH] feat: script to add and remove SRT track from Mux --- .../src/data/add-srt-to-specific-video.ts | 50 +++++++++++++++++++ .../src/data/remove-track-from-video.ts | 42 ++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 apps/testing-javascript/src/data/add-srt-to-specific-video.ts create mode 100644 apps/testing-javascript/src/data/remove-track-from-video.ts diff --git a/apps/testing-javascript/src/data/add-srt-to-specific-video.ts b/apps/testing-javascript/src/data/add-srt-to-specific-video.ts new file mode 100644 index 000000000..506771d96 --- /dev/null +++ b/apps/testing-javascript/src/data/add-srt-to-specific-video.ts @@ -0,0 +1,50 @@ +// Running this script: +// +// Be sure to set `MUX_ACCESS_TOKEN` and `MUX_SECRET_KEY` in your +// `.env.local` environment variables file. And assign string values +// to the `assetId` and `srtUrl` variables below. +// +// Execute the script with: +// +// ``` +// npx ts-node --files --skipProject src/data/add-srt-to-specific-video.ts +// ``` + +import Mux from '@mux/mux-node' + +require('dotenv-flow').config({ + default_node_env: 'development', +}) + +const assetId = undefined // Set me! +const srtUrl = undefined // Set me! + +const addSrtToSpecificVideo = async ( + assetId: string | undefined, + srtUrl: string | undefined, +) => { + if (!assetId) { + throw new Error('Must define assetId at the beginning of this script') + } + if (!srtUrl) { + throw new Error('Must define srtUrl at the beginning of this script') + } + + // Set up Mux API Client + const MUX_ACCESS_TOKEN = process.env.MUX_ACCESS_TOKEN as string + const MUX_SECRET_KEY = process.env.MUX_SECRET_KEY as string + const muxClient = new Mux(MUX_ACCESS_TOKEN, MUX_SECRET_KEY) + const {Video} = muxClient + + await Video.Assets.createTrack(assetId, { + url: srtUrl, + type: 'text', + text_type: 'subtitles', + closed_captions: false, + language_code: 'en-US', + name: 'English', + passthrough: 'English', + }) +} + +addSrtToSpecificVideo(assetId, srtUrl) diff --git a/apps/testing-javascript/src/data/remove-track-from-video.ts b/apps/testing-javascript/src/data/remove-track-from-video.ts new file mode 100644 index 000000000..e6dd9c204 --- /dev/null +++ b/apps/testing-javascript/src/data/remove-track-from-video.ts @@ -0,0 +1,42 @@ +// Running this script: +// +// Be sure to set `MUX_ACCESS_TOKEN` and `MUX_SECRET_KEY` in your +// `.env.local` environment variables file. And assign string values +// to the `assetId` and `trackId` variables below. +// +// Execute the script with: +// +// ``` +// npx ts-node --files --skipProject src/data/remove-track-from-video.ts +// ``` + +import Mux from '@mux/mux-node' + +require('dotenv-flow').config({ + default_node_env: 'development', +}) + +const assetId = undefined // Set me! +const trackId = undefined // Set me! + +const removeTrackFromVideo = async ( + assetId: string | undefined, + trackId: string | undefined, +) => { + if (!assetId) { + throw new Error('Must define assetId at the beginning of this script') + } + if (!trackId) { + throw new Error('Must define trackId at the beginning of this script') + } + + // Set up Mux API Client + const MUX_ACCESS_TOKEN = process.env.MUX_ACCESS_TOKEN as string + const MUX_SECRET_KEY = process.env.MUX_SECRET_KEY as string + const muxClient = new Mux(MUX_ACCESS_TOKEN, MUX_SECRET_KEY) + const {Video} = muxClient + + await Video.Assets.deleteTrack(assetId, trackId) +} + +removeTrackFromVideo(assetId, trackId)