deserialize drawtext message with struct object and add single quotes around values
This commit is contained in:
parent
8b3a80f560
commit
1373182c2a
@ -6,7 +6,8 @@
|
||||
|
||||
- fix deb and rpm bundle [79e4d5d](https://github.com/ffplayout/ffplayout/pull/217/commits/79e4d5dda05e715df96a38070466ea7a4c8378b2)
|
||||
- rearrange custom filters (fix missing output mapping on multiple outputs) [9cb3a62](https://github.com/ffplayout/ffplayout/pull/217/commits/9cb3a6206938adcf1fbe4ce0ec763cad9e812c76)
|
||||
- use mp2 for loudnorm_ingest to prevent number of samples in frame too big error [45197c2](https://github.com/ffplayout/ffplayout/pull/218/commits/45197c276df1c0394c26b030991b7e0e5490b0ce)
|
||||
- switch decoder audio codec to pcm_bluray [8b3a80f](https://github.com/ffplayout/ffplayout/pull/218/commits/8b3a80f5602eda240c6a59178c33886c9e81cb1d)
|
||||
- deserialize drawtext message with struct object and add single quotes around values
|
||||
|
||||
## Development
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::{process::exit, sync::atomic::Ordering};
|
||||
use std::{fmt, process::exit, sync::atomic::Ordering};
|
||||
|
||||
mod zmq_cmd;
|
||||
|
||||
@ -8,16 +8,92 @@ use jsonrpc_http_server::{
|
||||
jsonrpc_core::{IoHandler, Params, Value},
|
||||
AccessControlAllowOrigin, DomainsValidation, Response, RestApi, ServerBuilder,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Map};
|
||||
use simplelog::*;
|
||||
|
||||
use ffplayout_lib::utils::{
|
||||
get_delta, get_filter_from_json, get_sec, sec_to_time, write_status, Ingest, Media,
|
||||
OutputMode::*, PlayerControl, PlayoutConfig, PlayoutStatus, ProcessControl,
|
||||
get_delta, get_sec, sec_to_time, write_status, Ingest, Media, OutputMode::*, PlayerControl,
|
||||
PlayoutConfig, PlayoutStatus, ProcessControl,
|
||||
};
|
||||
|
||||
use zmq_cmd::zmq_send;
|
||||
|
||||
#[derive(Default, Deserialize, Clone)]
|
||||
struct TextFilter {
|
||||
text: Option<String>,
|
||||
x: Option<String>,
|
||||
y: Option<String>,
|
||||
fontsize: Option<String>,
|
||||
line_spacing: Option<String>,
|
||||
fontcolor: Option<String>,
|
||||
alpha: Option<String>,
|
||||
r#box: Option<String>,
|
||||
boxcolor: Option<String>,
|
||||
boxborderw: Option<String>,
|
||||
}
|
||||
|
||||
impl fmt::Display for TextFilter {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut s = format!("text='{}'", self.text.clone().unwrap_or_default());
|
||||
|
||||
if let Some(v) = &self.x {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":x='{v}'"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.y {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":y='{v}'"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.fontsize {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":fontsize={v}"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.line_spacing {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":line_spacing={v}"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.fontcolor {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":fontcolor={v}"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.alpha {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":alpha='{v}'"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.r#box {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":box={v}"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.boxcolor {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":boxcolor={v}"));
|
||||
}
|
||||
}
|
||||
if let Some(v) = &self.boxborderw {
|
||||
if !v.is_empty() {
|
||||
s.push_str(&format!(":boxborderw={v}"));
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
/// Covert JSON string to ffmpeg filter command.
|
||||
fn filter_from_json(raw_text: serde_json::Value) -> String {
|
||||
let filter: TextFilter = serde_json::from_value(raw_text).unwrap_or_default();
|
||||
|
||||
filter.to_string()
|
||||
}
|
||||
|
||||
/// map media struct to json object
|
||||
fn get_media_map(media: Media) -> Value {
|
||||
json!({
|
||||
@ -83,7 +159,7 @@ pub fn json_rpc_server(
|
||||
&& &map["control"] == "text"
|
||||
&& map.contains_key("message")
|
||||
{
|
||||
let filter = get_filter_from_json(map["message"].to_string());
|
||||
let filter = filter_from_json(map["message"].clone());
|
||||
|
||||
// TODO: in Rust 1.65 use let_chains instead
|
||||
if !filter.is_empty() && config.text.zmq_stream_socket.is_some() {
|
||||
|
@ -282,21 +282,6 @@ pub fn json_writer(path: &PathBuf, data: JsonPlaylist) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Covert JSON string to ffmpeg filter command.
|
||||
pub fn get_filter_from_json(raw_text: String) -> String {
|
||||
let re1 = Regex::new(r#""|}|\{"#).unwrap();
|
||||
let re2 = Regex::new(r#"id:[0-9]+,?|name:[^,]?,?"#).unwrap();
|
||||
let re3 = Regex::new(r#"text:([^,]*)"#).unwrap();
|
||||
let text = re1.replace_all(&raw_text, "");
|
||||
let text = re2.replace_all(&text, "").clone();
|
||||
let filter = re3
|
||||
.replace_all(&text, "text:'$1'")
|
||||
.replace(':', "=")
|
||||
.replace(',', ":");
|
||||
|
||||
filter
|
||||
}
|
||||
|
||||
/// Write current status to status file in temp folder.
|
||||
///
|
||||
/// The status file is init in main function and mostly modified in RPC server.
|
||||
|
Loading…
Reference in New Issue
Block a user