|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + muxgo "github.com/muxinc/mux-go/v6" |
| 9 | + "github.com/muxinc/mux-go/v6/examples/common" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + |
| 14 | + // API Client Initialization |
| 15 | + client := muxgo.NewAPIClient( |
| 16 | + muxgo.NewConfiguration( |
| 17 | + muxgo.WithBasicAuth(os.Getenv("MUX_TOKEN_ID"), os.Getenv("MUX_TOKEN_SECRET")), |
| 18 | + )) |
| 19 | + |
| 20 | + // ========== create-asset ========== |
| 21 | + asset, err := client.AssetsApi.CreateAsset(muxgo.CreateAssetRequest{ |
| 22 | + Input: []muxgo.InputSettings{ |
| 23 | + muxgo.InputSettings{ |
| 24 | + Url: "https://storage.googleapis.com/muxdemofiles/mux-video-intro.mp4", |
| 25 | + }, |
| 26 | + muxgo.InputSettings{ |
| 27 | + Url: "https://tears-of-steel-subtitles.s3.amazonaws.com/tears-fr.vtt", |
| 28 | + TextType: "subtitles", |
| 29 | + Type: "text", |
| 30 | + LanguageCode: "fr", |
| 31 | + ClosedCaptions: false, |
| 32 | + Name: "French", |
| 33 | + }, |
| 34 | + }, |
| 35 | + NormalizeAudio: true, |
| 36 | + }) |
| 37 | + common.AssertNoError(err) |
| 38 | + common.AssertNotNil(asset.Data) |
| 39 | + fmt.Println("create-asset OK ✅") |
| 40 | + |
| 41 | + // ========== list-assets ========== |
| 42 | + lr, err := client.AssetsApi.ListAssets() |
| 43 | + common.AssertNoError(err) |
| 44 | + common.AssertNotNil(lr.Data) |
| 45 | + common.AssertStringEqualsValue(asset.Data.Id, lr.Data[0].Id) |
| 46 | + fmt.Println("list-assets OK ✅") |
| 47 | + |
| 48 | + // Wait for the asset to become ready |
| 49 | + if asset.Data.Status != "ready" { |
| 50 | + fmt.Println(" Waiting for asset to become ready...") |
| 51 | + for { |
| 52 | + // ========== get-asset ========== |
| 53 | + gr, err := client.AssetsApi.GetAsset(asset.Data.Id) |
| 54 | + common.AssertNoError(err) |
| 55 | + common.AssertNotNil(gr) |
| 56 | + common.AssertStringEqualsValue(asset.Data.Id, gr.Data.Id) |
| 57 | + if gr.Data.Status != "ready" { |
| 58 | + fmt.Println(" Asset still not ready.") |
| 59 | + time.Sleep(1 * time.Second) |
| 60 | + } else { |
| 61 | + // ========== get-asset-input-info ========== |
| 62 | + ir, err := client.AssetsApi.GetAssetInputInfo(asset.Data.Id) |
| 63 | + common.AssertNoError(err) |
| 64 | + common.AssertNotNil(ir.Data) |
| 65 | + break |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + fmt.Println("get-asset OK ✅") |
| 70 | + fmt.Println("get-asset-input-info OK ✅") |
| 71 | + |
| 72 | + // ========== clipping ========== |
| 73 | + clipAsset, err := client.AssetsApi.CreateAsset(muxgo.CreateAssetRequest{ |
| 74 | + Input: []muxgo.InputSettings{ |
| 75 | + muxgo.InputSettings{ |
| 76 | + Url: "mux://assets/" + asset.Data.Id, |
| 77 | + StartTime: 0, |
| 78 | + EndTime: 5, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | + common.AssertNoError(err) |
| 83 | + common.AssertNotNil(clipAsset.Data.Id) |
| 84 | + fmt.Println("clipping OK ✅") |
| 85 | + |
| 86 | + // ========== create-asset-playback-id ========== |
| 87 | + capr := muxgo.CreatePlaybackIdRequest{Policy: muxgo.PUBLIC} |
| 88 | + capre, err := client.AssetsApi.CreateAssetPlaybackId(asset.Data.Id, capr) |
| 89 | + common.AssertNoError(err) |
| 90 | + common.AssertNotNil(capre.Data) |
| 91 | + common.AssertStringEqualsValue(string(capre.Data.Policy), "public") |
| 92 | + fmt.Println("create-asset-playback-id OK ✅") |
| 93 | + |
| 94 | + // ========== get-asset-playback-id ========== |
| 95 | + pbre, err := client.AssetsApi.GetAssetPlaybackId(asset.Data.Id, capre.Data.Id) |
| 96 | + common.AssertNoError(err) |
| 97 | + common.AssertNotNil(pbre.Data) |
| 98 | + common.AssertStringEqualsValue(capre.Data.Id, pbre.Data.Id) |
| 99 | + fmt.Println("get-asset-playback-id OK ✅") |
| 100 | + |
| 101 | + // ========== get-asset-or-livestream-id ========= |
| 102 | + playbackId := pbre.Data.Id |
| 103 | + pbResp, err := client.PlaybackIDApi.GetAssetOrLivestreamId(playbackId) |
| 104 | + common.AssertNoError(err) |
| 105 | + common.AssertNotNil(pbResp.Data) |
| 106 | + common.AssertStringEqualsValue(pbResp.Data.Object.Id, asset.Data.Id) |
| 107 | + common.AssertStringEqualsValue(pbResp.Data.Object.Type, "asset") |
| 108 | + fmt.Println("get-asset-or-livestream-id OK ✅") |
| 109 | + |
| 110 | + // ========== update-asset-mp4-support ========== |
| 111 | + mp4r := muxgo.UpdateAssetMp4SupportRequest{Mp4Support: "standard"} |
| 112 | + mp4, err := client.AssetsApi.UpdateAssetMp4Support(asset.Data.Id, mp4r) |
| 113 | + common.AssertNoError(err) |
| 114 | + common.AssertNotNil(mp4.Data) |
| 115 | + common.AssertStringEqualsValue(asset.Data.Id, mp4.Data.Id) |
| 116 | + common.AssertStringEqualsValue(mp4.Data.Mp4Support, "standard") |
| 117 | + fmt.Println("update-asset-mp4-support OK ✅") |
| 118 | + |
| 119 | + // ========== update-asset-master-access ========== |
| 120 | + mr := muxgo.UpdateAssetMasterAccessRequest{MasterAccess: "temporary"} |
| 121 | + mas, err := client.AssetsApi.UpdateAssetMasterAccess(asset.Data.Id, mr) |
| 122 | + common.AssertNoError(err) |
| 123 | + common.AssertNotNil(mas.Data) |
| 124 | + common.AssertStringEqualsValue(asset.Data.Id, mas.Data.Id) |
| 125 | + common.AssertStringEqualsValue(mas.Data.MasterAccess, "temporary") |
| 126 | + fmt.Println("update-asset-master-access OK ✅") |
| 127 | + |
| 128 | + // ========== create-asset-track ========== |
| 129 | + cat := muxgo.CreateTrackRequest{ |
| 130 | + Url: "https://tears-of-steel-subtitles.s3.amazonaws.com/tears-en.vtt", |
| 131 | + TextType: "subtitles", |
| 132 | + Type: "text", |
| 133 | + LanguageCode: "en", |
| 134 | + ClosedCaptions: false, |
| 135 | + Name: "English", |
| 136 | + } |
| 137 | + s, err := client.AssetsApi.CreateAssetTrack(asset.Data.Id, cat) |
| 138 | + common.AssertNoError(err) |
| 139 | + common.AssertNotNil(s.Data) |
| 140 | + common.AssertNotNil(s.Data.Id) |
| 141 | + common.AssertStringEqualsValue(s.Data.Name, "English") |
| 142 | + a2s, err := client.AssetsApi.GetAsset(asset.Data.Id) |
| 143 | + common.AssertIntEqualsValue(len(a2s.Data.Tracks), 4) // Audio, Video, French that we ingested with the asset, and the English we added here! |
| 144 | + fmt.Println("create-asset-track OK ✅") |
| 145 | + |
| 146 | + // ========== delete-asset-track ========== |
| 147 | + time.Sleep(5 * time.Second) |
| 148 | + err = client.AssetsApi.DeleteAssetTrack(asset.Data.Id, s.Data.Id) |
| 149 | + common.AssertNoError(err) |
| 150 | + a1s, err := client.AssetsApi.GetAsset(asset.Data.Id) |
| 151 | + common.AssertIntEqualsValue(len(a1s.Data.Tracks), 3) // Audio, Video, French that we ingested with the asset |
| 152 | + fmt.Println("delete-asset-track OK ✅") |
| 153 | + |
| 154 | + // ========== delete-asset-playback-id ========== |
| 155 | + err = client.AssetsApi.DeleteAssetPlaybackId(asset.Data.Id, capre.Data.Id) |
| 156 | + common.AssertNoError(err) |
| 157 | + dpba, err := client.AssetsApi.GetAsset(asset.Data.Id) |
| 158 | + common.AssertNoError(err) |
| 159 | + if len(dpba.Data.PlaybackIds) > 0 { |
| 160 | + fmt.Println("List of playback IDs wasn't empty") |
| 161 | + os.Exit(255) |
| 162 | + } |
| 163 | + fmt.Println("delete-asset-playback-id OK ✅") |
| 164 | + |
| 165 | + // ========== delete-asset ========== |
| 166 | + err = client.AssetsApi.DeleteAsset(asset.Data.Id) |
| 167 | + common.AssertNoError(err) |
| 168 | + _, err = client.AssetsApi.GetAsset(asset.Data.Id) |
| 169 | + common.AssertNotNil(err) |
| 170 | + fmt.Println("delete-asset OK ✅") |
| 171 | +} |
0 commit comments