easy_ffprobe/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Simple wrapper for the [ffprobe](https://ffmpeg.org/ffprobe.html) CLI utility,
//! which is part of the ffmpeg tool suite.
//!
//! This crate allows retrieving typed information about media files (images and videos)
//! by invoking `ffprobe` with JSON output options and deserializing the data
//! into convenient Rust types.
//!
//!
//!
//! ```rust
//! match ffprobe::ffprobe("path/to/video.mp4") {
//!    Ok(info) => {
//!        dbg!(info);
//!    },
//!    Err(err) => {
//!        eprintln!("Could not analyze file with ffprobe: {:?}", err);
//!     },
//! }
//!
//! ```

//! ## Features
//! - streams
//! - format
//! - chapters
//! - async
//!

use std::path::Path;
use std::process::Command;
use std::time::Duration;

use error::FfProbeError;
#[cfg(feature = "streams")]
mod attachment_stream;
#[cfg(feature = "streams")]
mod audio_stream;
#[cfg(feature = "chapters")]
mod chapter;
mod config;
#[cfg(feature = "streams")]
mod data_stream;
#[cfg(feature = "streams")]
mod disposition;
pub mod error;
mod ffprobe;
#[cfg(feature = "format")]
mod format;
mod ratio;
#[cfg(feature = "streams")]
mod streams;
#[cfg(feature = "streams")]
mod subtitle_stream;
#[cfg(feature = "streams")]
mod video_stream;

#[cfg(feature = "streams")]
pub use attachment_stream::AttachmentStream;
#[cfg(feature = "streams")]
pub use attachment_stream::AttachmentTags;
#[cfg(feature = "streams")]
pub use audio_stream::AudioStream;
#[cfg(feature = "streams")]
pub use audio_stream::AudioTags;
#[cfg(feature = "chapters")]
pub use chapter::Chapter;
#[cfg(feature = "chapters")]
pub use chapter::ChapterTags;
pub use config::Config;
#[cfg(feature = "streams")]
pub use data_stream::DataStream;
#[cfg(feature = "streams")]
pub use data_stream::DataTags;
#[cfg(feature = "streams")]
pub use disposition::Disposition;
pub use ffprobe::FfProbe;
#[cfg(feature = "format")]
pub use format::Format;
#[cfg(feature = "format")]
pub use format::FormatTags;
pub use ratio::Ratio;
use serde::Deserialize;
use serde::Deserializer;
#[cfg(feature = "streams")]
pub use streams::SideData;
#[cfg(feature = "streams")]
pub use streams::Stream;
#[cfg(feature = "streams")]
pub use streams::StreamKinds;
#[cfg(feature = "streams")]
pub use streams::StreamTags;
#[cfg(feature = "streams")]
pub use subtitle_stream::SubtititleTags;
#[cfg(feature = "streams")]
pub use subtitle_stream::SubtitleStream;
#[cfg(feature = "streams")]
pub use video_stream::VideoStream;
#[cfg(feature = "streams")]
pub use video_stream::VideoTags;
/// Execute ffprobe with default settings and return the extracted data.
///
/// See [`ffprobe_config`] if you need to customize settings.
pub fn ffprobe(path: impl AsRef<Path>) -> Result<FfProbe, FfProbeError> {
    ffprobe_config(Config::new(), path)
}

/// Run ffprobe with a custom config.
/// See [`ConfigBuilder`] for more details.
pub fn ffprobe_config(config: Config, path: impl AsRef<Path>) -> Result<FfProbe, FfProbeError> {
    let path = path.as_ref();
    let mut cmd = Command::new(config.ffprobe_bin);
    // Default args.
    cmd.args(["-v", "quiet", "-print_format", "json"]);
    #[cfg(feature = "chapters")]
    cmd.arg("-show_chapters");
    #[cfg(feature = "format")]
    cmd.arg("-show_format");
    #[cfg(feature = "streams")]
    cmd.arg("-show_streams");

    if config.count_frames {
        cmd.arg("-count_frames");
    }

    cmd.arg(path);

    let out = cmd.output().map_err(FfProbeError::Io)?;

    if !out.status.success() {
        return Err(FfProbeError::Status(out));
    }

    serde_json::from_slice::<FfProbe>(&out.stdout).map_err(FfProbeError::Deserialize)
}

#[cfg(feature = "async")]
pub async fn ffprobe_async(path: impl AsRef<std::path::Path>) -> Result<FfProbe, FfProbeError> {
    ffprobe_async_config(Config::new(), path).await
}

#[cfg(feature = "async")]
pub async fn ffprobe_async_config(
    config: Config,
    path: impl AsRef<Path>,
) -> Result<FfProbe, FfProbeError> {
    let mut cmd = tokio::process::Command::new("ffprobe");
    let path = path.as_ref();
    cmd.args(["-v", "quiet", "-print_format", "json"]);
    #[cfg(feature = "chapters")]
    cmd.arg("-show_chapters");
    #[cfg(feature = "format")]
    cmd.arg("-show_format");
    #[cfg(feature = "streams")]
    cmd.arg("-show_streams");

    if config.count_frames {
        cmd.arg("-count_frames");
    }

    cmd.arg(path);

    let out = cmd.output().await.map_err(FfProbeError::Io)?;

    if !out.status.success() {
        return Err(FfProbeError::Status(out));
    }

    serde_json::from_slice::<FfProbe>(&out.stdout).map_err(FfProbeError::Deserialize)
}

pub fn option_string_to_duration<'de, D>(deserializer: D) -> Result<Option<Duration>, D::Error>
where
    D: Deserializer<'de>,
{
    let s: Option<String> = Option::deserialize(deserializer)?;
    match s {
        Some(s) => s
            .parse::<f64>()
            .map(|v| v.max(0.0))
            .map(Duration::from_secs_f64)
            .map(Some)
            .map_err(serde::de::Error::custom),
        None => Ok(None),
    }
}