metadonnée technique dans le cache audio
This commit is contained in:
@@ -8,6 +8,7 @@ use anyhow::Result;
|
||||
use pmocache::CacheConfig;
|
||||
use serde_json::{Number, Value};
|
||||
use std::sync::Arc;
|
||||
use pmocache::download::TransformMetadata;
|
||||
|
||||
/// Configuration pour le cache audio
|
||||
pub struct AudioConfig;
|
||||
@@ -56,6 +57,37 @@ pub fn new_cache(dir: &str, limit: usize) -> Result<Cache> {
|
||||
Cache::with_transformer(dir, limit, Some(transformer_factory))
|
||||
}
|
||||
|
||||
fn persist_transform_streaminfo(cache: &Cache, pk: &str, tmeta: &TransformMetadata) {
|
||||
if let Some(sr) = tmeta.sample_rate {
|
||||
let _ = cache
|
||||
.db
|
||||
.set_a_metadata(pk, "sample_rate", Value::Number(Number::from(sr)));
|
||||
}
|
||||
if let Some(bps) = tmeta.bits_per_sample {
|
||||
let _ = cache
|
||||
.db
|
||||
.set_a_metadata(pk, "bits_per_sample", Value::Number(Number::from(bps)));
|
||||
}
|
||||
if let Some(ch) = tmeta.channels {
|
||||
let _ = cache
|
||||
.db
|
||||
.set_a_metadata(pk, "channels", Value::Number(Number::from(ch)));
|
||||
}
|
||||
if let Some(ts) = tmeta.total_samples {
|
||||
let _ = cache
|
||||
.db
|
||||
.set_a_metadata(pk, "total_samples", Value::Number(Number::from(ts)));
|
||||
if let Some(sr) = tmeta.sample_rate {
|
||||
if sr > 0 {
|
||||
let secs = (ts as f64 / sr as f64).round() as u64;
|
||||
let _ = cache
|
||||
.db
|
||||
.set_a_metadata(pk, "duration_secs", Value::Number(Number::from(secs)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée un cache audio et lance la consolidation en arrière-plan
|
||||
///
|
||||
/// Cette fonction crée le cache et lance immédiatement une consolidation
|
||||
@@ -138,6 +170,10 @@ pub async fn add_with_metadata_extraction(
|
||||
// Attendre que le fichier soit téléchargé et converti
|
||||
cache.wait_until_finished(&pk).await?;
|
||||
|
||||
if let Some(transform) = cache.transform_metadata(&pk).await {
|
||||
persist_transform_streaminfo(cache, &pk, &transform);
|
||||
}
|
||||
|
||||
// Lire le fichier FLAC pour extraire les métadonnées
|
||||
let file_path = cache.get_file_path(&pk);
|
||||
let flac_bytes = tokio::fs::read(&file_path).await?;
|
||||
|
||||
@@ -9,6 +9,7 @@ use bytes::Bytes;
|
||||
use pmocache::download::TransformMetadata;
|
||||
use pmocache::StreamTransformer;
|
||||
use pmoflac::{transcode_to_flac_stream, AudioCodec, TranscodeOptions};
|
||||
use serde_json::json;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
/// Creates the transformer consumed by the audio cache.
|
||||
@@ -36,7 +37,19 @@ pub fn create_streaming_flac_transformer() -> StreamTransformer {
|
||||
.set_metadata(TransformMetadata {
|
||||
mode: Some(mode.to_string()),
|
||||
input_codec: Some(codec_to_string(codec)),
|
||||
details: None,
|
||||
details: Some(
|
||||
json!({
|
||||
"sample_rate": info.sample_rate,
|
||||
"bits_per_sample": info.bits_per_sample,
|
||||
"channels": info.channels,
|
||||
"total_samples": info.total_samples,
|
||||
})
|
||||
.to_string(),
|
||||
),
|
||||
sample_rate: Some(info.sample_rate),
|
||||
bits_per_sample: Some(info.bits_per_sample),
|
||||
channels: Some(info.channels),
|
||||
total_samples: info.total_samples,
|
||||
})
|
||||
.await;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user