Version 1.0.0

This commit is contained in:
Ryze 2023-02-13 21:20:26 +03:00
parent d1cacecb48
commit 72937872cd
Signed by: ryze
GPG key ID: 9B296C5CEAEAAAC1
5 changed files with 109 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
/Cargo.lock

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "ff2mpv-rust"
version = "1.0.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
[profile.release-full]
inherits = "release"
strip = "symbols"
lto = "fat"

25
README.md Normal file
View file

@ -0,0 +1,25 @@
<div align="center">
# ff2mpv-rust
</div>
Native messaging host for ff2mpv written in Rust.
This is a native compiled binary, so it runs much faster and doesn't require external dependencies by itself, unlike Python and Ruby scripts.
# What is this?
This is a script for hooking mpv and [ff2mpv](https://github.com/woodruffw/ff2mpv), that allows opening any video link in mpv straight from the browser.
# Contributing
All issues and pull requests are welcome! Feel free to open an issue if you've got an idea or a problem. You can open a pull request if you are able to implement it yourself.
---
<p align="center">
<sub><strong>
Made with ponies and love!
<br/>
GNU GPL © Ryze 2023
</strong></sub>
</p>

42
src/lib.rs Normal file
View file

@ -0,0 +1,42 @@
use std::io::{self, Read, Write};
use serde::Deserialize;
#[derive(Deserialize)]
pub struct MpvMessage {
pub url: String
}
pub fn get_mpv_message() -> Result<MpvMessage, String> {
let message = match get_browser_message() {
Ok(msg) => msg,
Err(e) => return Err(format!("IO Error: {}", e))
};
match serde_json::from_str(&message) {
Ok(msg) => Ok(msg),
Err(e) => Err(format!("JSON Error: {}", e))
}
}
pub fn get_browser_message() -> io::Result<String> {
let mut stdin = io::stdin();
let mut buf: [u8; 4] = [0; 4];
stdin.read_exact(&mut buf)?;
let length = u32::from_ne_bytes(buf);
let mut reader = io::BufReader::new(stdin.take(length as u64));
let mut string = String::with_capacity(length as usize);
reader.read_to_string(&mut string)?;
Ok(string)
}
pub fn send_browser_message(message: &str) -> io::Result<()> {
let length = (message.len() as u32).to_ne_bytes();
let message = message.as_bytes();
let mut stdout = io::stdout();
stdout.write(&length)?;
stdout.write(&message)?;
Ok(())
}

27
src/main.rs Normal file
View file

@ -0,0 +1,27 @@
use std::process::{Command, self};
use ff2mpv_rust::{get_mpv_message, send_browser_message};
fn main() {
let message = match get_mpv_message() {
Ok(msg) => msg,
Err(e) => {
eprintln!("{}", e);
process::exit(-1)
}
};
let mpv = Command::new("mpv")
.arg(message.url)
.spawn();
if let Err(e) = mpv {
eprintln!("{}", e);
process::exit(-1);
}
if let Err(e) = send_browser_message("ok") {
eprintln!("{}", e);
process::exit(-1);
}
}