pl_mpeg_extract_frames.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. PL_MPEG Example - extract all frames of an mpg file and store as PNG
  3. Dominic Szablewski - https://phoboslab.org
  4. -- LICENSE: The MIT License(MIT)
  5. Copyright(c) 2019 Dominic Szablewski
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. this software and associated documentation files(the "Software"), to deal in
  8. the Software without restriction, including without limitation the rights to
  9. use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies
  10. of the Software, and to permit persons to whom the Software is furnished to do
  11. so, subject to the following conditions :
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. -- Usage
  22. pl_mpeg_extract_frames <video-file.mpg>
  23. -- About
  24. This program demonstrates how to extract all video frames from an MPEG-PS file.
  25. Frames are saved as PNG via stb_image_write: https://github.com/nothings/stb
  26. */
  27. #include <stdio.h>
  28. #define PL_MPEG_IMPLEMENTATION
  29. #include "pl_mpeg.h"
  30. #define STB_IMAGE_WRITE_IMPLEMENTATION
  31. #include "stb_image_write.h"
  32. int main(int argc, char *argv[]) {
  33. if (argc < 2) {
  34. printf("Usage: pl_mpeg_extract_frames <file.mpg>\n");
  35. return 1;
  36. }
  37. plm_t *plm = plm_create_with_filename(argv[1]);
  38. if (!plm) {
  39. printf("Couldn't open file %s\n", argv[1]);
  40. return 1;
  41. }
  42. plm_set_audio_enabled(plm, FALSE);
  43. int w = plm_get_width(plm);
  44. int h = plm_get_height(plm);
  45. uint8_t *rgb_buffer = (uint8_t *)malloc(w * h * 3);
  46. char png_name[16];
  47. plm_frame_t *frame = NULL;
  48. for (int i = 1; frame = plm_decode_video(plm); i++) {
  49. plm_frame_to_rgb(frame, rgb_buffer, w * 3);
  50. sprintf(png_name, "%06d.png", i);
  51. printf("Writing %s\n", png_name);
  52. stbi_write_png(png_name, w, h, 3, rgb_buffer, w * 3);
  53. }
  54. return 0;
  55. }