mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-10 06:34:18 +00:00
Separate flac
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
parent
5dd38003d6
commit
730ce2ff6a
1 changed files with 53 additions and 0 deletions
53
src/components/logic/flac.rs
Normal file
53
src/components/logic/flac.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
#![cfg(feature = "format-flac")]
|
||||
|
||||
use crate::{Picture, Result};
|
||||
|
||||
use metaflac::BlockType;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
|
||||
pub(crate) fn write_to<T>(
|
||||
mut data: T,
|
||||
vendor: &str,
|
||||
comments: &HashMap<String, String>,
|
||||
pictures: &Option<Cow<'static, [Picture]>>,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: Read + Write + Seek,
|
||||
{
|
||||
let mut tag = metaflac::Tag::read_from(&mut data)?;
|
||||
|
||||
tag.remove_blocks(BlockType::VorbisComment);
|
||||
tag.remove_blocks(BlockType::Picture);
|
||||
tag.remove_blocks(BlockType::Padding);
|
||||
|
||||
let mut comment_collection: HashMap<String, Vec<String>> = HashMap::new();
|
||||
|
||||
if let Some(pics) = pictures.clone() {
|
||||
let mut pics_final = Vec::new();
|
||||
|
||||
for pic in pics.iter() {
|
||||
pics_final.push(base64::encode(pic.as_apic_bytes()));
|
||||
}
|
||||
|
||||
comment_collection.insert(String::from("METADATA_BLOCK_PICTURE"), pics_final);
|
||||
}
|
||||
|
||||
for (k, v) in comments.clone() {
|
||||
comment_collection.insert(k, vec![v]);
|
||||
}
|
||||
|
||||
let comments = metaflac::Block::VorbisComment(metaflac::block::VorbisComment {
|
||||
vendor_string: vendor.to_string(),
|
||||
comments: comment_collection,
|
||||
});
|
||||
|
||||
tag.push_block(comments);
|
||||
|
||||
data.seek(SeekFrom::Start(0))?;
|
||||
|
||||
tag.write_to(&mut data)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue