Move old plugins (#4721)

This commit is contained in:
JT
2022-03-04 09:36:03 -05:00
committed by GitHub
parent 89b7f4d57c
commit eef3de2d05
61 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,174 @@
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, ReturnValue, TaggedDictBuilder, UntaggedValue, Value};
use nu_source::Tag;
use std::fs::File;
use std::io::Write;
use std::path::Path;
#[derive(Default)]
pub struct FromMp4 {
pub state: Vec<u8>,
pub name_tag: Tag,
}
impl FromMp4 {
pub fn new() -> Self {
Self {
state: vec![],
name_tag: Tag::unknown(),
}
}
}
pub fn convert_mp4_file_to_nu_value(path: &Path, tag: Tag) -> Result<Value, mp4::Error> {
let mp4 = mp4::read_mp4(File::open(path).expect("Could not open mp4 file to read metadata"))?;
let mut dict = TaggedDictBuilder::new(tag.clone());
// Build tracks table
let mut tracks = Vec::new();
for track in mp4.tracks().values() {
let mut curr_track_dict = TaggedDictBuilder::new(tag.clone());
curr_track_dict.insert_untagged("track id", UntaggedValue::int(track.track_id()));
curr_track_dict.insert_untagged(
"track type",
match track.track_type() {
Ok(t) => UntaggedValue::string(t.to_string()),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged(
"media type",
match track.media_type() {
Ok(t) => UntaggedValue::string(t.to_string()),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged(
"box type",
match track.box_type() {
Ok(t) => UntaggedValue::string(t.to_string()),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged("width", UntaggedValue::int(track.width()));
curr_track_dict.insert_untagged("height", UntaggedValue::int(track.height()));
curr_track_dict.insert_untagged("frame_rate", UntaggedValue::from(track.frame_rate()));
curr_track_dict.insert_untagged(
"sample freq index",
match track.sample_freq_index() {
Ok(sfi) => UntaggedValue::string(sfi.freq().to_string()), // this is a string for formatting reasons
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged(
"channel config",
match track.channel_config() {
Ok(cc) => UntaggedValue::string(cc.to_string()),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged("language", UntaggedValue::string(track.language()));
curr_track_dict.insert_untagged("timescale", UntaggedValue::int(track.timescale()));
curr_track_dict.insert_untagged(
"duration",
UntaggedValue::duration(track.duration().as_nanos()),
);
curr_track_dict.insert_untagged("bitrate", UntaggedValue::int(track.bitrate()));
curr_track_dict.insert_untagged("sample count", UntaggedValue::int(track.sample_count()));
curr_track_dict.insert_untagged(
"video profile",
match track.video_profile() {
Ok(vp) => UntaggedValue::string(vp.to_string()),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged(
"audio profile",
match track.audio_profile() {
Ok(ap) => UntaggedValue::string(ap.to_string()),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged(
"sequence parameter set",
match track.sequence_parameter_set() {
Ok(sps) => UntaggedValue::string(format!("{:X?}", sps)),
Err(_) => UntaggedValue::from("Unknown"),
},
);
curr_track_dict.insert_untagged(
"picture parameter set",
match track.picture_parameter_set() {
Ok(pps) => UntaggedValue::string(format!("{:X?}", pps)),
Err(_) => UntaggedValue::from("Unknown"),
},
);
// push curr track to tracks vec
tracks.push(curr_track_dict.into_value());
}
dict.insert_untagged("size", UntaggedValue::big_int(mp4.size()));
dict.insert_untagged(
"major brand",
UntaggedValue::string(mp4.major_brand().to_string()),
);
dict.insert_untagged("minor version", UntaggedValue::int(mp4.minor_version()));
dict.insert_untagged(
"compatible brands",
UntaggedValue::string(format!("{:?}", mp4.compatible_brands())),
);
dict.insert_untagged(
"duration",
UntaggedValue::duration(mp4.duration().as_nanos()),
);
dict.insert_untagged("timescale", UntaggedValue::int(mp4.timescale()));
dict.insert_untagged("is fragmented", UntaggedValue::boolean(mp4.is_fragmented()));
dict.insert_untagged("tracks", UntaggedValue::Table(tracks).into_value(&tag));
Ok(dict.into_value())
}
pub fn from_mp4_bytes_to_value(mut bytes: Vec<u8>, tag: Tag) -> Result<Value, std::io::Error> {
let mut tempfile = tempfile::NamedTempFile::new()?;
tempfile.write_all(bytes.as_mut_slice())?;
match convert_mp4_file_to_nu_value(tempfile.path(), tag) {
Ok(value) => Ok(value),
Err(e) => Err(std::io::Error::new(std::io::ErrorKind::Other, e)),
}
}
pub fn from_mp4(bytes: Vec<u8>, name_tag: Tag) -> Result<Vec<ReturnValue>, ShellError> {
match from_mp4_bytes_to_value(bytes, name_tag.clone()) {
Ok(x) => match x {
Value {
value: UntaggedValue::Table(list),
..
} => Ok(list.into_iter().map(ReturnSuccess::value).collect()),
_ => Ok(vec![ReturnSuccess::value(x)]),
},
Err(_) => Err(ShellError::labeled_error(
"Could not parse as MP4",
"input cannot be parsed as MP4",
&name_tag,
)),
}
}

View File

@ -0,0 +1,4 @@
mod from_mp4;
mod nu;
pub use from_mp4::FromMp4;

View File

@ -0,0 +1,6 @@
use nu_plugin::serve_plugin;
use nu_plugin_from_mp4::FromMp4;
fn main() {
serve_plugin(&mut FromMp4::new())
}

View File

@ -0,0 +1,46 @@
#[cfg(test)]
mod tests;
use crate::FromMp4;
use nu_errors::ShellError;
use nu_plugin::Plugin;
use nu_protocol::{CallInfo, Primitive, ReturnValue, Signature, UntaggedValue, Value};
use nu_source::Tag;
impl Plugin for FromMp4 {
fn config(&mut self) -> Result<Signature, ShellError> {
Ok(Signature::build("from mp4")
.desc("Get meta-data of mp4 file")
.filter())
}
fn begin_filter(&mut self, call_info: CallInfo) -> Result<Vec<ReturnValue>, ShellError> {
self.name_tag = call_info.name_tag;
Ok(vec![])
}
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
match input {
Value {
value: UntaggedValue::Primitive(Primitive::Binary(b)),
..
} => {
self.state.extend_from_slice(&b);
}
Value { tag, .. } => {
return Err(ShellError::labeled_error_with_secondary(
"Expected binary from pipeline",
"requires binary input",
self.name_tag.clone(),
"value originates from here",
tag,
));
}
}
Ok(vec![])
}
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
crate::from_mp4::from_mp4(self.state.clone(), Tag::unknown())
}
}

View File

@ -0,0 +1 @@
mod integration {}