diff --git a/README.md b/README.md index 34d3dfe..0cf9ff5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1401 TILs and counting..._ +_1402 TILs and counting..._ --- @@ -1622,6 +1622,7 @@ _1401 TILs and counting..._ ### Workflow +- [Add Subtitles To Existing Mux Video Asset](workflow/add-subtitles-to-existing-mux-video-asset.md) - [Change Window Name In iTerm](workflow/change-window-name-in-iterm.md) - [Convert An ePub Document To PDF On Mac](workflow/convert-an-epub-document-to-pdf-on-mac.md) - [Create A Local Sanity Dataset Backup](workflow/create-a-local-sanity-dataset-backup.md) diff --git a/workflow/add-subtitles-to-existing-mux-video-asset.md b/workflow/add-subtitles-to-existing-mux-video-asset.md new file mode 100644 index 0000000..9e931c4 --- /dev/null +++ b/workflow/add-subtitles-to-existing-mux-video-asset.md @@ -0,0 +1,49 @@ +# Add Subtitles To Existing Mux Video Asset + +There is a [JavaScript/TypeScript Mux +SDK](https://github.com/muxinc/mux-node-sdk) for interacting with Mux via their +[API](https://docs.mux.com/api-reference). + +With a Mux client (initialized from `MUX_ACCESS_TOKEN` and `MUX_SECRET_KEY` in +our environment), we can destructure `Video` which will allow us to call +`Video.Assets.createTrack`. We pass that function the ID of an existing video +asset and then an object of data defining the subtitle track. In this case, it +is an English subtitle track. We need to point Mux to a `url` where the WebVTT +formatted subtitles are hosted. + +```typescript +// add-srt-to-specific-video.ts + +import Mux from '@mux/mux-node' + +require('dotenv-flow').config({ + default_node_env: 'development', +}) + +const assetId = "mux-asset-id" // set this value +const srtUrl = "https://public-webvtt-file" // set this value + +// 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', +}) +``` + +We can run the above script with `ts-node` from the command-line like so: + +```bash +$ npx ts-node --files --skipProject add-srt-to-specific-video.ts +``` + +We'll see the new track on the existing asset in the Mux dashboard.