async_openai/
video.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::videos::{
5        CreateVideoRequest, ListVideosResponse, RemixVideoRequest, VideoJob, VideoJobMetadata,
6    },
7    Client, RequestOptions,
8};
9use bytes::Bytes;
10
11/// Video generation with Sora
12/// Related guide: [Video generation](https://platform.openai.com/docs/guides/video-generation)
13pub struct Videos<'c, C: Config> {
14    client: &'c Client<C>,
15    pub(crate) request_options: RequestOptions,
16}
17
18impl<'c, C: Config> Videos<'c, C> {
19    pub fn new(client: &'c Client<C>) -> Self {
20        Self {
21            client,
22            request_options: RequestOptions::new(),
23        }
24    }
25
26    /// Create a video
27    #[crate::byot(
28        T0 = Clone,
29        R = serde::de::DeserializeOwned,
30        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
31    )]
32    pub async fn create(&self, request: CreateVideoRequest) -> Result<VideoJob, OpenAIError> {
33        self.client
34            .post_form("/videos", request, &self.request_options)
35            .await
36    }
37
38    /// Create a video remix
39    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
40    pub async fn remix(
41        &self,
42        video_id: &str,
43        request: RemixVideoRequest,
44    ) -> Result<VideoJob, OpenAIError> {
45        self.client
46            .post(
47                &format!("/videos/{video_id}/remix"),
48                request,
49                &self.request_options,
50            )
51            .await
52    }
53
54    /// Retrieves a video by its ID.
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn retrieve(&self, video_id: &str) -> Result<VideoJob, OpenAIError> {
57        self.client
58            .get(&format!("/videos/{}", video_id), &self.request_options)
59            .await
60    }
61
62    /// Delete a Video
63    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
64    pub async fn delete(&self, video_id: &str) -> Result<VideoJobMetadata, OpenAIError> {
65        self.client
66            .delete(&format!("/videos/{}", video_id), &self.request_options)
67            .await
68    }
69
70    /// List Videos
71    #[crate::byot(R = serde::de::DeserializeOwned)]
72    pub async fn list(&self) -> Result<ListVideosResponse, OpenAIError> {
73        self.client.get("/videos", &self.request_options).await
74    }
75
76    /// Download video content.
77    /// Variant can be provided as query parameter
78    pub async fn download_content(&self, video_id: &str) -> Result<Bytes, OpenAIError> {
79        let (bytes, _headers) = self
80            .client
81            .get_raw(
82                &format!("/videos/{video_id}/content"),
83                &self.request_options,
84            )
85            .await?;
86        Ok(bytes)
87    }
88}