vtx_sdk/host/
vtx_ffmpeg.rs1use crate::bindings::vtx::api::vtx_ffmpeg::{self, FfmpegOption, TranscodeProfile};
4use crate::bindings::vtx::api::vtx_types::HttpResponse;
5use crate::bindings::vtx::api::vtx_vfs::Buffer;
6use crate::vtx_error::{VtxError, VtxResult};
7
8pub struct FfmpegTask {
23 profile: String,
24 input_id: String,
25 options: Vec<FfmpegOption>,
26}
27
28impl FfmpegTask {
29 pub fn new(profile: impl Into<String>, input_id: impl Into<String>) -> Self {
34 Self {
35 profile: profile.into(),
36 input_id: input_id.into(),
37 options: Vec::new(),
38 }
39 }
40
41 pub fn new_pipe(profile: impl Into<String>) -> Self {
43 Self::new(profile, "pipe:0")
44 }
45
46 pub fn option(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
48 self.options.push(FfmpegOption {
49 key: key.into(),
50 value: Some(value.into()),
51 });
52 self
53 }
54
55 pub fn flag(mut self, key: impl Into<String>) -> Self {
57 self.options.push(FfmpegOption {
58 key: key.into(),
59 value: None,
60 });
61 self
62 }
63
64 pub fn options<I, K, V>(mut self, options: I) -> Self
66 where
67 I: IntoIterator<Item = (K, V)>,
68 K: Into<String>,
69 V: Into<String>,
70 {
71 for (key, value) in options {
72 self.options.push(FfmpegOption {
73 key: key.into(),
74 value: Some(value.into()),
75 });
76 }
77 self
78 }
79
80 pub fn format(self, format: &str) -> Self {
82 self.option("f", format)
83 }
84
85 pub fn seek(self, start: &str, duration: Option<&str>) -> Self {
87 let mut s = self.option("ss", start);
88 if let Some(d) = duration {
89 s = s.option("t", d);
90 }
91 s
92 }
93
94 pub fn execute_buffer(self) -> VtxResult<Buffer> {
98 let params = TranscodeProfile {
99 profile: self.profile,
100 input_id: self.input_id,
101 options: self.options,
102 };
103
104 vtx_ffmpeg::execute(¶ms).map_err(VtxError::from_host_message)
105 }
106
107 pub fn execute(self) -> VtxResult<HttpResponse> {
111 let buffer = self.execute_buffer()?;
112 Ok(HttpResponse {
113 status: 200,
114 body: Some(buffer),
115 })
116 }
117}