-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|