ffplay.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use std::time::Duration;
  2. use scrap::{Frame, TraitPixelBuffer};
  3. extern crate scrap;
  4. fn main() {
  5. use scrap::{Capturer, Display, TraitCapturer};
  6. use std::io::ErrorKind::WouldBlock;
  7. use std::io::Write;
  8. use std::process::{Command, Stdio};
  9. let d = Display::primary().unwrap();
  10. let (w, h) = (d.width(), d.height());
  11. let child = Command::new("ffplay")
  12. .args(&[
  13. "-f",
  14. "rawvideo",
  15. "-pixel_format",
  16. "bgr0",
  17. "-video_size",
  18. &format!("{}x{}", w, h),
  19. "-framerate",
  20. "60",
  21. "-",
  22. ])
  23. .stdin(Stdio::piped())
  24. .spawn()
  25. .expect("This example requires ffplay.");
  26. let mut capturer = Capturer::new(d).unwrap();
  27. let mut out = child.stdin.unwrap();
  28. loop {
  29. match capturer.frame(Duration::from_millis(0)) {
  30. Ok(frame) => {
  31. // Write the frame, removing end-of-row padding.
  32. let Frame::PixelBuffer(frame) = frame else {
  33. return;
  34. };
  35. let stride = frame.stride()[0];
  36. let rowlen = 4 * w;
  37. for row in frame.data().chunks(stride) {
  38. let row = &row[..rowlen];
  39. out.write_all(row).unwrap();
  40. }
  41. }
  42. Err(ref e) if e.kind() == WouldBlock => {
  43. // Wait for the frame.
  44. }
  45. Err(_) => {
  46. // We're done here.
  47. break;
  48. }
  49. }
  50. }
  51. }