new notes, termshrek ascii mode
This commit is contained in:
@@ -12,8 +12,11 @@ use ffmpeg::software::scaling::{context::Context as ScalingContext, flag::Flags}
|
||||
use ffmpeg::util::format::pixel::Pixel;
|
||||
use ffmpeg_next as ffmpeg;
|
||||
|
||||
const ASCII_GRADIENT: &[char] = &[
|
||||
' ', '.', ',', '-', '~', ':', ';', '=', '!', '*', '#', '$', '@',
|
||||
];
|
||||
// const ASCII_GRADIENT: &[char] = &[' ', '░', '▒', '▓', '█'];
|
||||
const ASCII_GRADIENT: &[char] = &[' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
|
||||
// const ASCII_GRADIENT: &[char] = &[' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
||||
enum ArtStyle {
|
||||
@@ -23,6 +26,8 @@ enum ArtStyle {
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Args {
|
||||
#[arg(short, long)]
|
||||
path: String,
|
||||
#[arg(short, long, default_value_t = true)]
|
||||
keep_ar: bool,
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
@@ -34,7 +39,6 @@ struct Args {
|
||||
struct VideoReader {
|
||||
input: ffmpeg::format::context::Input,
|
||||
fps: f32,
|
||||
aspect_ratio: f32,
|
||||
dims: (u32, u32),
|
||||
decoder: ffmpeg::decoder::Video,
|
||||
scaler: ScalingContext,
|
||||
@@ -65,33 +69,29 @@ impl VideoReader {
|
||||
|
||||
let decoder = codec_context.decoder().video().unwrap();
|
||||
|
||||
// Find aspect ratio
|
||||
let width = decoder.width();
|
||||
let height = decoder.height();
|
||||
|
||||
let sar = decoder.aspect_ratio();
|
||||
let ar = width as f32 / height as f32;
|
||||
|
||||
let sar_f32 = if sar.denominator() != 0 {
|
||||
sar.numerator() as f32 / sar.denominator() as f32
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
|
||||
let dar = (width as f32 / height as f32) * sar_f32;
|
||||
|
||||
// Find target resolution
|
||||
let target_width;
|
||||
let target_height;
|
||||
|
||||
let prep_term_height = (term_height - 2) * 2;
|
||||
|
||||
if dar > term_width as f32 / (prep_term_height) as f32 {
|
||||
target_height = (term_width as f32 / dar).floor() as u32;
|
||||
dbg!(&term_width, &term_height, &prep_term_height);
|
||||
if ar > term_width as f32 / (prep_term_height) as f32 {
|
||||
target_height = (term_width as f32 / ar).floor() as u32;
|
||||
target_width = term_width as u32;
|
||||
} else {
|
||||
target_height = (prep_term_height) as u32;
|
||||
target_width = ((prep_term_height) as f32 * dar).floor() as u32;
|
||||
target_height = prep_term_height as u32;
|
||||
target_width = (prep_term_height as f32 * ar).floor() as u32;
|
||||
}
|
||||
dbg!(
|
||||
&target_width,
|
||||
(prep_term_height as f32 * ar).floor(),
|
||||
&target_height
|
||||
);
|
||||
|
||||
let scaler = ScalingContext::get(
|
||||
decoder.format(),
|
||||
@@ -107,7 +107,6 @@ impl VideoReader {
|
||||
Self {
|
||||
input,
|
||||
fps: framerate_f32,
|
||||
aspect_ratio: dar,
|
||||
dims: (target_width, target_height.div_euclid(2) * 2),
|
||||
decoder,
|
||||
scaler,
|
||||
@@ -158,10 +157,12 @@ impl VideoReader {
|
||||
if let Ok(_) = self.decoder.receive_frame(&mut self.frame) {
|
||||
return Some(self.extract_frame_data());
|
||||
}
|
||||
} else {
|
||||
} else if self.loop_video {
|
||||
self.decoder.flush();
|
||||
self.input.seek(0, ..).unwrap();
|
||||
packets_iter = self.input.packets();
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,10 +201,7 @@ fn render_pixel_art(frame: &Vec<[u8; 2]>, reader: &VideoReader, buf: &mut Vec<u8
|
||||
}
|
||||
|
||||
fn luminance(r: u8, g: u8, b: u8) -> f32 {
|
||||
let res = 0.2126 * f32::try_from(r).unwrap_or_else(|e| unreachable!("{e}"))
|
||||
+ 0.7152 * f32::try_from(g).unwrap_or_else(|e| unreachable!("{e}"))
|
||||
+ 0.0722 * f32::try_from(b).unwrap_or_else(|e| unreachable!("{e}"));
|
||||
res
|
||||
0.2126 * f32::from(r) + 0.7152 * f32::from(g) + 0.0722 * f32::from(b)
|
||||
}
|
||||
|
||||
fn render_ascii_art(frame: &Vec<[u8; 2]>, reader: &VideoReader, buf: &mut Vec<u8>) {
|
||||
@@ -216,19 +214,18 @@ fn render_ascii_art(frame: &Vec<[u8; 2]>, reader: &VideoReader, buf: &mut Vec<u8
|
||||
let greens = frame.get((idx + 1) as usize).unwrap();
|
||||
let blues = frame.get((idx + 2) as usize).unwrap();
|
||||
|
||||
// color
|
||||
// buf.extend_from_slice(
|
||||
// format!(
|
||||
// "\x1b[38;2;{tr};{tg};{tb}m",
|
||||
// tr = reds[0],
|
||||
// tg = greens[0],
|
||||
// tb = blues[0],
|
||||
// )
|
||||
// .as_bytes(),
|
||||
// );
|
||||
buf.extend_from_slice(
|
||||
format!(
|
||||
"\x1b[38;2;{tr};{tg};{tb}m",
|
||||
tr = reds[0],
|
||||
tg = greens[0],
|
||||
tb = blues[0],
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
let mut extend = [0u8; 4];
|
||||
buf.extend_from_slice(
|
||||
ASCII_GRADIENT[(luminance(reds[0], greens[0], blues[0])) as usize
|
||||
ASCII_GRADIENT[luminance(reds[0], greens[0], blues[0]) as usize
|
||||
* (ASCII_GRADIENT.len() - 1)
|
||||
/ 255]
|
||||
.encode_utf8(&mut extend)
|
||||
@@ -246,7 +243,7 @@ fn main() {
|
||||
term_size::dimensions().unwrap_or_else(|| panic!("Could not find terminal dimensions"));
|
||||
|
||||
let mut reader = VideoReader::new(
|
||||
Path::new("/home/jan/Videos/rotating_panda.mkv"),
|
||||
Path::new(&args.path),
|
||||
args.loop_video,
|
||||
term_dims.0,
|
||||
term_dims.1,
|
||||
|
||||
Reference in New Issue
Block a user