async_openai/
video.rs

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