-
Notifications
You must be signed in to change notification settings - Fork 3
cmd: add video download command (PRINFRA-139) #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "github.com/heygen-com/heygen-cli/internal/command" | ||
| clierrors "github.com/heygen-com/heygen-cli/internal/errors" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var downloadClient = &http.Client{Timeout: 10 * time.Minute} | ||
|
|
||
| type assetInfo struct { | ||
| field string | ||
| ext string | ||
| label string | ||
| } | ||
|
|
||
| var assetTypes = map[string]assetInfo{ | ||
| "video": {field: "video_url", ext: ".mp4", label: "video"}, | ||
| "captioned": {field: "captioned_video_url", ext: ".mp4", label: "captioned video"}, | ||
| } | ||
|
|
||
| func newVideoDownloadCmd(ctx *cmdContext) *cobra.Command { | ||
| var outputPath string | ||
| var asset string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "download <video-id>", | ||
| Short: "Download a video file or related asset to disk", | ||
| Args: cobra.ExactArgs(1), | ||
| Example: "heygen video download <video-id>\n" + | ||
| "heygen video download <video-id> --asset captioned\n" + | ||
| "heygen video download <video-id> --output-path my-video.mp4", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| videoID := args[0] | ||
| info, ok := assetTypes[asset] | ||
| if !ok { | ||
| return clierrors.NewUsage( | ||
| fmt.Sprintf("invalid --asset value %q: must be one of: video, captioned", asset)) | ||
| } | ||
|
|
||
| spec := &command.Spec{ | ||
| Endpoint: "/v3/videos/{video_id}", | ||
| Method: http.MethodGet, | ||
| } | ||
| inv := &command.Invocation{ | ||
| PathParams: map[string]string{"video_id": videoID}, | ||
| QueryParams: make(url.Values), | ||
| } | ||
| result, err := ctx.client.Execute(spec, inv) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| assetURL, err := extractAssetURL(result, videoID, info) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| dest := outputPath | ||
| if dest == "" { | ||
| // Sanitize: strip directory components from video ID to prevent | ||
| // path traversal. Handles IDs with / or \ safely. | ||
| dest = filepath.Base(videoID) + info.ext | ||
| } | ||
|
|
||
| if err := downloadFile(cmd.Context(), assetURL, dest); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| data, err := json.Marshal(map[string]string{ | ||
| "asset": asset, | ||
| "message": fmt.Sprintf("Downloaded %s to %s", info.label, dest), | ||
| "path": dest, | ||
| }) | ||
| if err != nil { | ||
| return clierrors.New(fmt.Sprintf("failed to encode response: %v", err)) | ||
| } | ||
|
|
||
| return ctx.formatter.Data(data, "", nil) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&asset, "asset", "video", "Asset to download: video, captioned") | ||
| cmd.Flags().StringVar(&outputPath, "output-path", "", "Output file path (default: {video-id}.mp4)") | ||
| return cmd | ||
| } | ||
|
|
||
| func extractAssetURL(raw json.RawMessage, videoID string, info assetInfo) (string, error) { | ||
| var resp struct { | ||
| Data map[string]json.RawMessage `json:"data"` | ||
| } | ||
| if err := json.Unmarshal(raw, &resp); err != nil { | ||
| return "", clierrors.New("failed to parse video response") | ||
| } | ||
|
|
||
| var status string | ||
| if rawStatus, ok := resp.Data["status"]; ok { | ||
| _ = json.Unmarshal(rawStatus, &status) | ||
| } | ||
|
|
||
| var assetURL string | ||
| if rawURL, ok := resp.Data[info.field]; ok { | ||
| _ = json.Unmarshal(rawURL, &assetURL) | ||
| } | ||
|
|
||
| if assetURL == "" { | ||
| switch status { | ||
| case "failed", "error": | ||
| return "", &clierrors.CLIError{ | ||
| Code: "video_failed", | ||
| Message: fmt.Sprintf("video rendering failed (status: %s)", status), | ||
| Hint: "Check details with: heygen video get " + videoID, | ||
| ExitCode: clierrors.ExitGeneral, | ||
| } | ||
| case "completed": | ||
| return "", &clierrors.CLIError{ | ||
| Code: "asset_not_available", | ||
| Message: fmt.Sprintf("%s not available for this video", info.label), | ||
| Hint: assetHint(info.field), | ||
| ExitCode: clierrors.ExitGeneral, | ||
| } | ||
| default: | ||
| msg := fmt.Sprintf("%s URL not available", info.label) | ||
| if status != "" { | ||
| msg = fmt.Sprintf("%s URL not available (status: %s)", info.label, status) | ||
| } | ||
| return "", &clierrors.CLIError{ | ||
| Code: "video_not_ready", | ||
| Message: msg, | ||
| Hint: "Use --wait when creating: heygen video create ... --wait", | ||
| ExitCode: clierrors.ExitGeneral, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return assetURL, nil | ||
| } | ||
|
|
||
| func assetHint(field string) string { | ||
| switch field { | ||
| case "captioned_video_url": | ||
| return "Video may not have been created with captions enabled." | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| func downloadFile(ctx context.Context, videoURL, dest string) error { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, videoURL, nil) | ||
| if err != nil { | ||
| return clierrors.New(fmt.Sprintf("failed to build download request: %v", err)) | ||
| } | ||
|
|
||
| resp, err := downloadClient.Do(req) | ||
| if err != nil { | ||
| return clierrors.New(fmt.Sprintf("failed to download video: %v", err)) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return clierrors.New(fmt.Sprintf("download failed with HTTP %d", resp.StatusCode)) | ||
| } | ||
|
|
||
| // Write to a temp file in the same directory, then rename on success. | ||
| // This prevents destroying an existing file on partial download failure. | ||
| dir := filepath.Dir(dest) | ||
| tmp, err := os.CreateTemp(dir, ".heygen-download-*.tmp") | ||
| if err != nil { | ||
| return clierrors.New(fmt.Sprintf("failed to create temp file in %q: %v", dir, err)) | ||
| } | ||
| tmpPath := tmp.Name() | ||
|
|
||
| _, copyErr := io.Copy(tmp, resp.Body) | ||
| closeErr := tmp.Close() | ||
| if copyErr != nil { | ||
| _ = os.Remove(tmpPath) | ||
| return clierrors.New(fmt.Sprintf("download interrupted: %v", copyErr)) | ||
| } | ||
| if closeErr != nil { | ||
| _ = os.Remove(tmpPath) | ||
| return clierrors.New(fmt.Sprintf("failed to finalize download: %v", closeErr)) | ||
| } | ||
|
|
||
| // Atomic rename. On Windows this may fail if dest is open elsewhere; | ||
| // os.Rename across filesystems also fails, but temp file is in the | ||
| // same directory so this is safe. | ||
| if err := os.Rename(tmpPath, dest); err != nil { | ||
| _ = os.Remove(tmpPath) | ||
| return clierrors.New(fmt.Sprintf("failed to move download to %q: %v", dest, err)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's potentially different files to download here i.e. captioned and non captioned videos
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I didn't notice that API response includes both
video_urlandcaptioned_video_url.We can add flags to choose which file to download. Let's discuss this during our pod sync up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a flag @jrusso1020