mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 16:18:01 +00:00
Add Add Subtitles To Existing Mux Video Asset as a Workflow TIL
This commit is contained in:
@@ -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).
|
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
|
### 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)
|
- [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)
|
- [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)
|
- [Create A Local Sanity Dataset Backup](workflow/create-a-local-sanity-dataset-backup.md)
|
||||||
|
|||||||
49
workflow/add-subtitles-to-existing-mux-video-asset.md
Normal file
49
workflow/add-subtitles-to-existing-mux-video-asset.md
Normal file
@@ -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.
|
||||||
Reference in New Issue
Block a user