logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

In-praise-of-ffmpeg.md (4378B)


  1. ---
  2. title: In praise of ffmpeg
  3. date: 2022-10-12
  4. ---
  5. My last "[In praise of][0]" article covered qemu, a project founded by Fabrice
  6. Bellard, and today I want to take a look at another work by Bellard:
  7. [ffmpeg][1]. Bellard has a knack for building high-quality software which solves
  8. a problem so well that every other solution becomes obsolete shortly thereafter,
  9. and ffmpeg is no exception.
  10. [0]: https://drewdevault.com/2022/09/02/2022-09-02-In-praise-of-qemu.html
  11. [1]: https://ffmpeg.org
  12. ffmpeg has been described as the Swiss army knife of multimedia. It incorporates
  13. hundreds of video, audio, and image decoders and encoders, muxers and demuxers,
  14. filters and devices. It provides a CLI and a set of libraries for working with
  15. its tools, and is the core component of many video and audio players as a result
  16. (including my preferred multimedia player, [mpv][2]). If you want to do almost
  17. anything with multimedia files — re-encode them, re-mux them, live stream
  18. it, whatever — ffmpeg can handle it with ease.
  19. [2]: https://mpv.io
  20. Let me share an example.
  21. I was recently hanging out at my local hackerspace and wanted to play some PS2
  22. games on my laptop. My laptop is not powerful enough to drive [PCSX2][3], but my
  23. workstation on the other side of town certainly was. So I forwarded my game
  24. controller to my workstation via USB/IP and pulled up the ffmpeg manual to
  25. figure out how to live-stream the game to my laptop. ffmpeg can capture video
  26. from KMS buffers directly, use the GPU to efficiently downscale them, grab audio
  27. from pulse, encode them with settings tuned for low-latency, and mux it into a
  28. UDP socket. On the other end I set up mpv to receive the stream and play it
  29. back.
  30. [3]: https://pcsx2.net
  31. ```
  32. ffmpeg \
  33. -f pulse \
  34. -i alsa_output.platform-snd_aloop.0.analog-surround-51.monitor \
  35. -f kmsgrab \
  36. -thread_queue_size 64 \ # reduce input latency
  37. -i - \
  38. # Capture and downscale frames on the GPU:
  39. -vf 'hwmap=derive_device=vaapi,scale_vaapi=1280:720,hwdownload,format=bgr0' \
  40. -c:v libx264 \
  41. -preset:v superfast \ # encode video as fast as possible
  42. -tune zerolatency \ # tune encoder for low latency
  43. -intra-refresh 1 \ # reduces latency and mitigates dropped packets
  44. -f mpegts \ # mux into mpegts stream, well-suited to this use-case
  45. -b:v 3M \ # configure target video bandwidth
  46. udp://$hackerspace:41841
  47. ```
  48. With an hour of tinkering and reading man pages, I was able to come up with a
  49. single command which produced a working remote video game streaming setup *from
  50. scratch* thanks to ffmpeg. ffmpeg is *amazing*.
  51. <video autoplay mute loop controls>
  52. <source src="https://mirror.drewdevault.com/ffx.webm"></source>
  53. </video>
  54. I have relied on ffmpeg for many tasks and for many years. It has always been
  55. there to handle any little multimedia-related task I might put it to for
  56. personal use &mdash; re-encoding audio files so they fit on my phone, taking
  57. clips from videos to share, muxing fonts into mkv files, capturing video from my
  58. webcam, [live streaming hacking sessions on my own platform][4], or anything
  59. else I can imagine. It formed the foundation of [MediaCrush][5] back in the day,
  60. where we used it to optimize multimedia files for efficient viewing on the web,
  61. back when that was more difficult than "just transcode it to a webm".
  62. [4]: https://drewdevault.com/2018/08/26/Self-hosted-livestreaming.html
  63. [5]: https://github.com/mediacrush/mediacrush
  64. ffmpeg is notable for being one of the first large-scale FOSS projects to
  65. completely eradicate proprietary software in its niche. Virtually all
  66. multimedia-related companies rely on ffmpeg to do their heavy lifting. It took a
  67. complex problem and solved it, with free software. The book is now closed on
  68. multimedia: ffmpeg is the solution to almost all of your problems. And if it's
  69. not, you're more likely to patch ffmpeg than to develop something new. The code
  70. is accessible and the community are experts in your problem domain.
  71. ffmpeg is one of the foremost pillars of achievement in free software. It has
  72. touched the lives of every reader, whether they know it or not. If you've ever
  73. watched TV, or gone to a movie, or watched videos online, or listened to a
  74. podcast, odds are that ffmpeg was involved in making it possible. It is one of
  75. the most well-executed and important software projects of all time.