fix examples and documentation
Build and test / check (pull_request) Successful in 1m56s Details
Build and test / build (pull_request) Successful in 1m58s Details
Build and test / test (pull_request) Failing after 2m1s Details
Build and test / docs (pull_request) Successful in 2m9s Details

This commit is contained in:
Oystein Kristoffer Tveit 2024-05-04 00:06:43 +02:00
parent 5100224a26
commit 20ed20dd70
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 68 additions and 82 deletions

View File

@ -1,12 +1,15 @@
[package] [package]
name = "mpvipc" name = "mpvipc"
version = "1.3.0" version = "1.3.0"
authors = ["Jonas Frei <freijon@pm.me>"] authors = [
"Jonas Frei <freijon@pm.me>",
"h7x4 <h7x4@nani.wtf>"
]
description = "A small library which provides bindings to control existing mpv instances through sockets." description = "A small library which provides bindings to control existing mpv instances through sockets."
license = "GPL-3.0" license = "GPL-3.0"
homepage = "https://gitlab.com/mpv-ipc/mpvipc" homepage = "https://git.pvv.ntnu.no/oysteikt/mpvipc"
repository = "https://gitlab.com/mpv-ipc/mpvipc" repository = "https://git.pvv.ntnu.no/oysteikt/mpvipc"
documentation = "https://docs.rs/mpvipc/" documentation = "https://pvv.ntnu.no/~oysteikt/gitea/mpvipc/master/docs/mpvipc/"
edition = "2021" edition = "2021"
rust-version = "1.75" rust-version = "1.75"

View File

@ -5,46 +5,30 @@
A small library which provides bindings to control existing mpv instances through sockets. A small library which provides bindings to control existing mpv instances through sockets.
To make use of this library, please make sure mpv is started with the following option:
`
$ mpv --input-ipc-server=/tmp/mpv.sock --idle ...
`
## Dependencies ## Dependencies
- `mpv` - `mpv`
- `cargo` (makedep) - `cargo` (make dependency)
- `cargo-nextest` (test depencency)
## Install - `grcov` (test depencency)
- [Cargo](https://crates.io/crates/mpvipc)
You can use this package with cargo.
## Example ## Example
Make sure mpv is started with the following option: Make sure mpv is started with the following option:
`
$ mpv --input-ipc-server=/tmp/mpv.sock --idle
`
Here is a small code example which connects to the socket /tmp/mpv.sock and toggles playback. ```bash
$ mpv --input-ipc-server=/tmp/mpv.sock --idle
```
Here is a small code example which connects to the socket `/tmp/mpv.sock` and toggles playback.
```rust ```rust
extern crate mpvipc;
use mpvipc::*; use mpvipc::*;
use std::sync::mpsc::channel;
fn main() { #[tokio::main]
let mpv = Mpv::connect("/tmp/mpv.sock").unwrap(); async fn main() -> Result<(), MpvError> {
let paused: bool = mpv.get_property("pause").unwrap(); let mpv = Mpv::connect("/tmp/mpv.sock").await?;
let paused: bool = mpv.get_property("pause").await?;
mpv.set_property("pause", !paused).expect("Error pausing"); mpv.set_property("pause", !paused).expect("Error pausing");
} }
``` ```
For a more extensive example and proof of concept, see project [mpvc](https://gitlab.com/mpv-ipc/mpvc).
## Bugs / Ideas
Check out the [Issue Tracker](https://gitlab.com/mpv-ipc/mpvipc/issues)

View File

@ -1,15 +1,19 @@
use mpvipc::{MpvError, Mpv, MpvExt}; use mpvipc::{Mpv, MpvError, MpvExt};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), MpvError> { async fn main() -> Result<(), MpvError> {
env_logger::init(); env_logger::init();
let mpv = Mpv::connect("/tmp/mpv.sock").await?; let mpv = Mpv::connect("/tmp/mpv.sock").await?;
let meta = mpv.get_metadata().await?; let meta = mpv.get_metadata().await?;
println!("metadata: {:?}", meta); println!("metadata: {:?}", meta);
let playlist = mpv.get_playlist().await?; let playlist = mpv.get_playlist().await?;
println!("playlist: {:?}", playlist); println!("playlist: {:?}", playlist);
let playback_time: f64 = mpv.get_property("playback-time").await?; let playback_time: f64 = mpv.get_property("playback-time").await?;
println!("playback-time: {}", playback_time); println!("playback-time: {}", playback_time);
Ok(()) Ok(())
} }

View File

@ -1,4 +1,5 @@
use mpvipc::{MpvError, Mpv, MpvExt}; use futures::StreamExt;
use mpvipc::{parse_event_property, Event, Mpv, MpvDataType, MpvError, MpvExt, Property};
fn seconds_to_hms(total: f64) -> String { fn seconds_to_hms(total: f64) -> String {
let total = total as u64; let total = total as u64;
@ -14,55 +15,49 @@ async fn main() -> Result<(), MpvError> {
env_logger::init(); env_logger::init();
let mpv = Mpv::connect("/tmp/mpv.sock").await?; let mpv = Mpv::connect("/tmp/mpv.sock").await?;
let pause = false;
let playback_time = std::f64::NAN;
let duration = std::f64::NAN;
mpv.observe_property(1, "path").await?; mpv.observe_property(1, "path").await?;
mpv.observe_property(2, "pause").await?; mpv.observe_property(2, "pause").await?;
mpv.observe_property(3, "playback-time").await?; mpv.observe_property(3, "playback-time").await?;
mpv.observe_property(4, "duration").await?; mpv.observe_property(4, "duration").await?;
mpv.observe_property(5, "metadata").await?; mpv.observe_property(5, "metadata").await?;
loop {
// TODO: let mut events = mpv.get_event_stream().await;
// let event = mpv.event_listen()?; while let Some(Ok(event)) = events.next().await {
// match event { match event {
// Event::PropertyChange { id: _, property } => match property { mpvipc::Event::PropertyChange { .. } => match parse_event_property(event)? {
// Property::Path(Some(value)) => println!("\nPlaying: {}", value), (1, Property::Path(Some(value))) => println!("\nPlaying: {}", value),
// Property::Path(None) => (), (2, Property::Pause(value)) => {
// Property::Pause(value) => pause = value, println!("Pause: {}", value);
// Property::PlaybackTime(Some(value)) => playback_time = value,
// Property::PlaybackTime(None) => playback_time = std::f64::NAN,
// Property::Duration(Some(value)) => duration = value,
// Property::Duration(None) => duration = std::f64::NAN,
// Property::Metadata(Some(value)) => {
// println!("File tags:");
// if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
// println!(" Artist: {}", value);
// }
// if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
// println!(" Album: {}", value);
// }
// if let Some(MpvDataType::String(value)) = value.get("TITLE") {
// println!(" Title: {}", value);
// }
// if let Some(MpvDataType::String(value)) = value.get("TRACK") {
// println!(" Track: {}", value);
// }
// }
// Property::Metadata(None) => (),
// Property::Unknown { name: _, data: _ } => (),
// },
// Event::Shutdown => return Ok(()),
// Event::Unimplemented => panic!("Unimplemented event"),
// _ => (),
// }
// print!(
// "{}{} / {} ({:.0}%)\r",
// if pause { "(Paused) " } else { "" },
// seconds_to_hms(playback_time),
// seconds_to_hms(duration),
// 100. * playback_time / duration
// );
// io::stdout().flush().unwrap();
} }
(3, Property::PlaybackTime(Some(value))) => {
println!("Playback time: {}", seconds_to_hms(value));
}
(4, Property::Duration(Some(value))) => {
println!("Duration: {}", seconds_to_hms(value));
}
(5, Property::Metadata(Some(value))) => {
println!("File tags:");
if let Some(MpvDataType::String(value)) = value.get("ARTIST") {
println!(" Artist: {}", value);
}
if let Some(MpvDataType::String(value)) = value.get("ALBUM") {
println!(" Album: {}", value);
}
if let Some(MpvDataType::String(value)) = value.get("TITLE") {
println!(" Title: {}", value);
}
if let Some(MpvDataType::String(value)) = value.get("TRACK") {
println!(" Track: {}", value);
}
}
_ => (),
},
Event::Shutdown => return Ok(()),
Event::Unimplemented(_) => panic!("Unimplemented event"),
_ => (),
}
}
Ok(())
} }