extract_frames.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. extract_frams: utlize the framebyframe API and mpg123_framedata to extract the MPEG frames out of a stream (strip off anything else).
  3. copyright 2011 by the mpg123 project - free software under the terms of the LGPL 2.1
  4. see COPYING and AUTHORS files in distribution or http://mpg123.org
  5. initially written by Thomas Orgis
  6. */
  7. #include <mpg123.h>
  8. /* unistd.h is not available under MSVC,
  9. io.h defines the read and write functions */
  10. #ifndef _MSC_VER
  11. #include <unistd.h>
  12. #else
  13. #include <io.h>
  14. #endif
  15. #include <stdio.h>
  16. int do_work(mpg123_handle *m);
  17. int main(int argc, char **argv)
  18. {
  19. int ret = 0;
  20. mpg123_handle *m;
  21. mpg123_init();
  22. m = mpg123_new(NULL, &ret);
  23. if(m == NULL)
  24. {
  25. fprintf(stderr, "Cannot create handle: %s", mpg123_plain_strerror(ret));
  26. }
  27. else
  28. {
  29. fprintf(stderr, "I'll take your dirty MPEG audio from standard input and will write the extracted pure MPEG data to standard output.\n");
  30. if(argc > 1 && strcmp(argv[1], "--noinfo") == 0)
  31. {
  32. fprintf(stderr, "Enabling parsing/consuming of the Info frame so that it will not appear in output.\n");
  33. ret = mpg123_param(m, MPG123_REMOVE_FLAGS, MPG123_IGNORE_INFOFRAME, 0.);
  34. }
  35. else
  36. {
  37. fprintf(stderr, "If you'd have given --noinfo as argument, I would omit a LAME/Xing info frame.\n");
  38. ret = mpg123_param(m, MPG123_ADD_FLAGS, MPG123_IGNORE_INFOFRAME, 0.);
  39. }
  40. if(ret == 0) ret = do_work(m);
  41. if(ret != 0) fprintf(stderr, "Some error occured: %s\n", mpg123_strerror(m));
  42. mpg123_delete(m); /* Closes, too. */
  43. }
  44. mpg123_exit();
  45. return ret;
  46. }
  47. int do_work(mpg123_handle *m)
  48. {
  49. int ret;
  50. size_t count = 0;
  51. ret = mpg123_open_fd(m, STDIN_FILENO);
  52. if(ret != MPG123_OK) return ret;
  53. while( (ret = mpg123_framebyframe_next(m)) == MPG123_OK || ret == MPG123_NEW_FORMAT )
  54. {
  55. unsigned long header;
  56. unsigned char *bodydata;
  57. size_t bodybytes;
  58. if(mpg123_framedata(m, &header, &bodydata, &bodybytes) == MPG123_OK)
  59. {
  60. /* Need to extract the 4 header bytes from the native storage in the correct order. */
  61. unsigned char hbuf[4];
  62. int i;
  63. for(i=0; i<4; ++i) hbuf[i] = (unsigned char) ((header >> ((3-i)*8)) & 0xff);
  64. /* Now write out both header and data, fire and forget. */
  65. write(STDOUT_FILENO, hbuf, 4);
  66. write(STDOUT_FILENO, bodydata, bodybytes);
  67. fprintf(stderr, "%zu: header 0x%08x, %zu body bytes\n", ++count, header, bodybytes);
  68. }
  69. }
  70. if(ret != MPG123_DONE)
  71. fprintf(stderr, "Some error occured (non-fatal?): %s\n", mpg123_strerror(m));
  72. fprintf(stderr, "Done with %zu MPEG frames.\n", count);
  73. return 0;
  74. }