videocapture_audio.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/videoio.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <iostream>
  5. using namespace cv;
  6. using namespace std;
  7. int main(int argc, char** argv)
  8. {
  9. CommandLineParser parser(argc, argv, "{@audio||}");
  10. string file = parser.get<string>("@audio");
  11. if (file.empty())
  12. {
  13. return 1;
  14. }
  15. Mat frame;
  16. vector<vector<Mat>> audioData;
  17. VideoCapture cap;
  18. vector<int> params { CAP_PROP_AUDIO_STREAM, 0,
  19. CAP_PROP_VIDEO_STREAM, -1,
  20. CAP_PROP_AUDIO_DATA_DEPTH, CV_16S };
  21. cap.open(file, CAP_MSMF, params);
  22. if (!cap.isOpened())
  23. {
  24. cerr << "ERROR! Can't to open file: " + file << endl;
  25. return -1;
  26. }
  27. const int audioBaseIndex = (int)cap.get(CAP_PROP_AUDIO_BASE_INDEX);
  28. const int numberOfChannels = (int)cap.get(CAP_PROP_AUDIO_TOTAL_CHANNELS);
  29. cout << "CAP_PROP_AUDIO_DATA_DEPTH: " << depthToString((int)cap.get(CAP_PROP_AUDIO_DATA_DEPTH)) << endl;
  30. cout << "CAP_PROP_AUDIO_SAMPLES_PER_SECOND: " << cap.get(CAP_PROP_AUDIO_SAMPLES_PER_SECOND) << endl;
  31. cout << "CAP_PROP_AUDIO_TOTAL_CHANNELS: " << numberOfChannels << endl;
  32. cout << "CAP_PROP_AUDIO_TOTAL_STREAMS: " << cap.get(CAP_PROP_AUDIO_TOTAL_STREAMS) << endl;
  33. int numberOfSamples = 0;
  34. audioData.resize(numberOfChannels);
  35. for (;;)
  36. {
  37. if (cap.grab())
  38. {
  39. for (int nCh = 0; nCh < numberOfChannels; nCh++)
  40. {
  41. cap.retrieve(frame, audioBaseIndex+nCh);
  42. audioData[nCh].push_back(frame);
  43. numberOfSamples+=frame.cols;
  44. }
  45. }
  46. else { break; }
  47. }
  48. cout << "Number of samples: " << numberOfSamples << endl;
  49. return 0;
  50. }