| 1 |
use std::{ |
| 2 |
fs::File, |
| 3 |
io::{self, BufReader, Read, Write}, |
| 4 |
}; |
| 5 |
|
| 6 |
use anyhow::Result; |
| 7 |
use av_scenechange::{ |
| 8 |
decoder::Decoder, |
| 9 |
detect_scene_changes, |
| 10 |
DetectionOptions, |
| 11 |
SceneDetectionSpeed, |
| 12 |
}; |
| 13 |
use clap::Parser; |
| 14 |
|
| 15 |
#[derive(Parser, Debug)] |
| 16 |
struct Args { |
| 17 |
/// Sets the input file to use |
| 18 |
#[clap(value_parser)] |
| 19 |
pub input: String, |
| 20 |
|
| 21 |
/// Optional file to write results to |
| 22 |
#[clap(long, short, value_parser)] |
| 23 |
pub output: Option<String>, |
| 24 |
|
| 25 |
/// Speed level for scene-change detection, 0: best quality, 1: fastest mode |
| 26 |
#[clap(long, short, value_parser, default_value_t = 0)] |
| 27 |
pub speed: u8, |
| 28 |
|
| 29 |
/// Do not detect short scene flashes and exclude them as scene cuts |
| 30 |
#[clap(long)] |
| 31 |
pub no_flash_detection: bool, |
| 32 |
|
| 33 |
/// Sets a minimum interval between two consecutive scenecuts |
| 34 |
#[clap(long, value_parser)] |
| 35 |
pub min_scenecut: Option<usize>, |
| 36 |
|
| 37 |
/// Sets a maximum interval between two consecutive scenecuts, |
| 38 |
/// after which a scenecut will be forced |
| 39 |
#[clap(long, value_parser)] |
| 40 |
pub max_scenecut: Option<usize>, |
| 41 |
} |
| 42 |
|
| 43 |
fn main() -> Result<()> { |
| 44 |
init_logger(); |
| 45 |
|
| 46 |
#[cfg(feature = "tracing")] |
| 47 |
let (chrome_layer, _guard) = tracing_chrome::ChromeLayerBuilder::new().build(); |
| 48 |
|
| 49 |
#[cfg(feature = "tracing")] |
| 50 |
{ |
| 51 |
use tracing_subscriber::layer::SubscriberExt; |
| 52 |
tracing::subscriber::set_global_default(tracing_subscriber::registry().with(chrome_layer)) |
| 53 |
.expect("Could not initialize tracing subscriber"); |
| 54 |
} |
| 55 |
|
| 56 |
let matches = Args::parse(); |
| 57 |
let input = match matches.input.as_str() { |
| 58 |
"-" => Box::new(io::stdin()) as Box<dyn Read>, |
| 59 |
f => Box::new(File::open(f)?) as Box<dyn Read>, |
| 60 |
}; |
| 61 |
let mut reader = BufReader::new(input); |
| 62 |
|
| 63 |
let mut opts = DetectionOptions { |
| 64 |
detect_flashes: !matches.no_flash_detection, |
| 65 |
min_scenecut_distance: matches.min_scenecut, |
| 66 |
max_scenecut_distance: matches.max_scenecut, |
| 67 |
..DetectionOptions::default() |
| 68 |
}; |
| 69 |
|
| 70 |
opts.analysis_speed = match matches.speed { |
| 71 |
0 => SceneDetectionSpeed::Standard, |
| 72 |
1 => SceneDetectionSpeed::Fast, |
| 73 |
_ => panic!("Speed mode must be in range [0; 1]"), |
| 74 |
}; |
| 75 |
|
| 76 |
let mut dec = Decoder::Y4m(y4m::Decoder::new(&mut reader)?); |
| 77 |
let bit_depth = dec.get_video_details()?.bit_depth; |
| 78 |
let results = if bit_depth == 8 { |
| 79 |
detect_scene_changes::<_, u8>(&mut dec, opts, None, None)? |
| 80 |
} else { |
| 81 |
detect_scene_changes::<_, u16>(&mut dec, opts, None, None)? |
| 82 |
}; |
| 83 |
print!("{}", serde_json::to_string(&results)?); |
| 84 |
|
| 85 |
if let Some(output_file) = matches.output { |
| 86 |
let mut file = File::create(output_file)?; |
| 87 |
|
| 88 |
let output = serde_json::to_string_pretty(&results)?; |
| 89 |
file.write_all(&output.into_bytes())?; |
| 90 |
} |
| 91 |
|
| 92 |
Ok(()) |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(not(feature = "devel"))] |
| 96 |
const fn init_logger() { |
| 97 |
// Do nothing |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(feature = "devel")] |
| 101 |
fn init_logger() { |
| 102 |
use std::str::FromStr; |
| 103 |
fn level_colored(l: log::Level) -> console::StyledObject<&'static str> { |
| 104 |
use console::style; |
| 105 |
use log::Level; |
| 106 |
match l { |
| 107 |
Level::Trace => style("??").dim(), |
| 108 |
Level::Debug => style("? ").dim(), |
| 109 |
Level::Info => style("> ").green(), |
| 110 |
Level::Warn => style("! ").yellow(), |
| 111 |
Level::Error => style("!!").red(), |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
let level = std::env::var("LOG") |
| 116 |
.ok() |
| 117 |
.and_then(|l| log::LevelFilter::from_str(&l).ok()) |
| 118 |
.unwrap_or(log::LevelFilter::Warn); |
| 119 |
|
| 120 |
fern::Dispatch::new() |
| 121 |
.format(move |out, message, record| { |
| 122 |
out.finish(format_args!( |
| 123 |
"{level} {message}", |
| 124 |
level = level_colored(record.level()), |
| 125 |
message = message, |
| 126 |
)); |
| 127 |
}) |
| 128 |
// set the default log level. to filter out verbose log messages from dependencies, set |
| 129 |
// this to Warn and overwrite the log level for your crate. |
| 130 |
.level(level) |
| 131 |
// output to stdout |
| 132 |
.chain(std::io::stderr()) |
| 133 |
.apply() |
| 134 |
.unwrap(); |
| 135 |
} |