fix examples and documentation
Some checks failed
Build and test / build (pull_request) Successful in 1m57s
Build and test / check (pull_request) Successful in 1m57s
Build and test / test (pull_request) Failing after 2m0s
Build and test / docs (pull_request) Successful in 2m56s

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

View File

@ -1,12 +1,15 @@
[package]
name = "mpvipc"
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."
license = "GPL-3.0"
homepage = "https://gitlab.com/mpv-ipc/mpvipc"
repository = "https://gitlab.com/mpv-ipc/mpvipc"
documentation = "https://docs.rs/mpvipc/"
homepage = "https://git.pvv.ntnu.no/oysteikt/mpvipc"
repository = "https://git.pvv.ntnu.no/oysteikt/mpvipc"
documentation = "https://pvv.ntnu.no/~oysteikt/gitea/mpvipc/master/docs/mpvipc/"
edition = "2021"
rust-version = "1.75"

View File

@ -5,46 +5,30 @@
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
- `mpv`
- `cargo` (makedep)
## Install
- [Cargo](https://crates.io/crates/mpvipc)
You can use this package with cargo.
- `cargo` (make dependency)
- `cargo-nextest` (test depencency)
- `grcov` (test depencency)
## Example
Make sure mpv is started with the following option:
`
```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
extern crate mpvipc;
use mpvipc::*;
use std::sync::mpsc::channel;
fn main() {
let mpv = Mpv::connect("/tmp/mpv.sock").unwrap();
let paused: bool = mpv.get_property("pause").unwrap();
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).
Here is a small code example which connects to the socket `/tmp/mpv.sock` and toggles playback.
## Bugs / Ideas
```rust
use mpvipc::*;
Check out the [Issue Tracker](https://gitlab.com/mpv-ipc/mpvipc/issues)
#[tokio::main]
async fn main() -> Result<(), MpvError> {
let mpv = Mpv::connect("/tmp/mpv.sock").await?;
let paused: bool = mpv.get_property("pause").await?;
mpv.set_property("pause", !paused).expect("Error pausing");
}
```

View File

@ -1,15 +1,19 @@
use mpvipc::{MpvError, Mpv, MpvExt};
use mpvipc::{Mpv, MpvError, MpvExt};
#[tokio::main]
async fn main() -> Result<(), MpvError> {
env_logger::init();
let mpv = Mpv::connect("/tmp/mpv.sock").await?;
let meta = mpv.get_metadata().await?;
println!("metadata: {:?}", meta);
let playlist = mpv.get_playlist().await?;
println!("playlist: {:?}", playlist);
let playback_time: f64 = mpv.get_property("playback-time").await?;
println!("playback-time: {}", playback_time);
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 {
let total = total as u64;
@ -14,55 +15,49 @@ async fn main() -> Result<(), MpvError> {
env_logger::init();
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(2, "pause").await?;
mpv.observe_property(3, "playback-time").await?;
mpv.observe_property(4, "duration").await?;
mpv.observe_property(5, "metadata").await?;
loop {
// TODO:
// let event = mpv.event_listen()?;
// match event {
// Event::PropertyChange { id: _, property } => match property {
// Property::Path(Some(value)) => println!("\nPlaying: {}", value),
// Property::Path(None) => (),
// Property::Pause(value) => 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();
let mut events = mpv.get_event_stream().await;
while let Some(Ok(event)) = events.next().await {
match event {
mpvipc::Event::PropertyChange { .. } => match parse_event_property(event)? {
(1, Property::Path(Some(value))) => println!("\nPlaying: {}", value),
(2, Property::Pause(value)) => {
println!("Pause: {}", value);
}
(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(())
}