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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Public API methods for metadata management.
//!
//! This module provides the high-level public API for adding metadata
//! and thumbnails to downloaded files.
use std::path::PathBuf;
use std::str::FromStr;
use super::MetadataManager;
use crate::error::Result;
use crate::model::Video;
use crate::model::format::{Extension, Format};
impl MetadataManager {
/// Add metadata to a file based on its format.
///
/// This method automatically detects the file format and applies appropriate metadata.
/// Use this for standalone files when you don't have format details.
///
/// # Arguments
///
/// * `file_path` - Path to the file to add metadata to
/// * `video` - Video metadata to apply
///
/// # Errors
///
/// Returns an error if the file format is unsupported or if metadata writing fails
///
/// # Examples
///
/// ```rust,no_run
/// # use yt_dlp::metadata::MetadataManager;
/// # use yt_dlp::model::Video;
/// # use std::path::PathBuf;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let manager = MetadataManager::new();
/// # let video: Video = todo!();
/// // video obtained from a fetch_video_infos call
/// manager.add_metadata("video.mp4", &video).await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_metadata(&self, file_path: impl Into<PathBuf>, video: &Video) -> Result<()> {
let file_path: std::path::PathBuf = file_path.into();
tracing::debug!(
file_path = ?file_path,
video_id = %video.id,
title = %video.title,
"🏷️ Adding metadata to file"
);
let file_format = Self::get_file_extension(&file_path)?;
let extension = Extension::from_str(&file_format).unwrap_or(Extension::Unknown);
tracing::debug!(
file_path = ?file_path,
file_format = %file_format,
extension = ?extension,
"⚙️ Detected file format and extension"
);
let result = match extension {
Extension::Mp3 => Self::add_metadata_to_mp3(&file_path, video, None, None).await,
Extension::M4A | Extension::Mp4 => Self::add_metadata_to_m4a(&file_path, video, None, None, None).await,
Extension::Webm => self.add_metadata_to_webm(&file_path, video, None, None, None).await,
Extension::Flac | Extension::Ogg | Extension::Wav | Extension::Aac | Extension::Aiff => {
Self::add_metadata_with_lofty(&file_path, video, None, None, &file_format).await
}
_ => {
self.add_ffmpeg_metadata(&file_path, video, &file_format, None, None, None)
.await
}
};
match &result {
Ok(()) => tracing::debug!(
file_path = ?file_path,
video_id = %video.id,
"✅ Metadata added successfully"
),
Err(e) => tracing::warn!(
file_path = ?file_path,
video_id = %video.id,
error = %e,
"Failed to add metadata"
),
}
result
}
/// Add metadata to a file with format details for audio and video.
///
/// This method should be used when you have detailed format information,
/// typically for combined audio+video files. Technical metadata (resolution,
/// codecs, bitrates) will be included for MP4 and WebM formats.
///
/// # Arguments
///
/// * `file_path` - Path to the file to add metadata to
/// * `video` - Video metadata to apply
/// * `video_format` - Optional video format details (for technical metadata)
/// * `audio_format` - Optional audio format details (for technical metadata)
///
/// # Errors
///
/// Returns an error if the file format is unsupported or if metadata writing fails
pub async fn add_metadata_with_format(
&self,
file_path: impl Into<PathBuf>,
video: &Video,
video_format: Option<&Format>,
audio_format: Option<&Format>,
) -> Result<()> {
let file_path: PathBuf = file_path.into();
tracing::debug!(
file_path = ?file_path,
video_id = %video.id,
title = %video.title,
has_video_format = video_format.is_some(),
has_audio_format = audio_format.is_some(),
"🏷️ Adding metadata with format details to file"
);
let file_format = Self::get_file_extension(&file_path)?;
let extension = Extension::from_str(&file_format).unwrap_or(Extension::Unknown);
tracing::debug!(
file_path = ?file_path,
file_format = %file_format,
extension = ?extension,
"⚙️ Detected file format and extension"
);
let result = match extension {
Extension::Mp3 => Self::add_metadata_to_mp3(&file_path, video, audio_format, None).await,
Extension::M4A | Extension::Mp4 => {
Self::add_metadata_to_m4a(&file_path, video, audio_format, video_format, None).await
}
Extension::Webm => {
self.add_metadata_to_webm(&file_path, video, video_format, audio_format, None)
.await
}
Extension::Flac | Extension::Ogg | Extension::Wav | Extension::Aac | Extension::Aiff => {
Self::add_metadata_with_lofty(&file_path, video, audio_format, None, &file_format).await
}
_ => {
self.add_ffmpeg_metadata(&file_path, video, &file_format, video_format, audio_format, None)
.await
}
};
match &result {
Ok(()) => tracing::debug!(
file_path = ?file_path,
video_id = %video.id,
"✅ Metadata with format added successfully"
),
Err(e) => tracing::warn!(
file_path = ?file_path,
video_id = %video.id,
error = %e,
"Failed to add metadata with format"
),
}
result
}
/// Add a thumbnail to a file based on its format.
///
/// Thumbnails are embedded in the file metadata. Supported formats: MP3, M4A, MP4, WebM, MKV
///
/// # Arguments
///
/// * `file_path` - Path to the file to add thumbnail to
/// * `thumbnail_path` - Path to the thumbnail image file
///
/// # Errors
///
/// Returns an error if the file format doesn't support thumbnails or if embedding fails
///
/// # Examples
///
/// ```rust,no_run
/// # use yt_dlp::metadata::MetadataManager;
/// # use std::path::PathBuf;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let manager = MetadataManager::new();
/// manager
/// .add_thumbnail_to_file("video.mp3", "cover.jpg")
/// .await?;
/// # Ok(())
/// # }
/// ```
pub async fn add_thumbnail_to_file(
&self,
file_path: impl Into<PathBuf>,
thumbnail_path: impl Into<PathBuf>,
) -> Result<()> {
let file_path: PathBuf = file_path.into();
let thumbnail_path: PathBuf = thumbnail_path.into();
tracing::debug!(
file_path = ?file_path,
thumbnail_path = ?thumbnail_path,
"🏷️ Adding thumbnail to file"
);
let file_format = Self::get_file_extension(&file_path)?;
let extension = Extension::from_str(&file_format).unwrap_or(Extension::Unknown);
tracing::debug!(
file_path = ?file_path,
file_format = %file_format,
extension = ?extension,
"⚙️ Detected file format for thumbnail"
);
let result = match extension {
Extension::Mp3 => Self::add_thumbnail_to_mp3(&file_path, &thumbnail_path).await,
Extension::M4A | Extension::Mp4 => Self::add_thumbnail_to_m4a(&file_path, &thumbnail_path).await,
Extension::Webm => self.add_thumbnail_to_webm(&file_path, &thumbnail_path).await,
Extension::Flac | Extension::Ogg | Extension::Wav | Extension::Aac | Extension::Aiff => {
Self::add_thumbnail_with_lofty(&file_path, &thumbnail_path, &file_format).await
}
_ => {
tracing::debug!(
file_format = %file_format,
"⚙️ Thumbnails not supported for file format"
);
Ok(())
}
};
match &result {
Ok(()) => tracing::debug!(
file_path = ?file_path,
"✅ Thumbnail added successfully"
),
Err(e) => tracing::warn!(
file_path = ?file_path,
error = %e,
"Failed to add thumbnail"
),
}
result
}
}