Advertisement
Guest User

libav decoding frames

a guest
Oct 1st, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. extern "C" {
  5. #include <libavcodec/avcodec.h>    // required headers
  6. #include <libavformat/avformat.h>
  7. }
  8. #ifndef AV_ERROR_MAX_STRING_SIZE
  9. #define AV_ERROR_MAX_STRING_SIZE 256
  10. #endif
  11.  
  12. #define checkResult(condition, message) if(! (condition) ) { \
  13.     char errorString[AV_ERROR_MAX_STRING_SIZE]; \
  14.     av_strerror(result, errorString, AV_ERROR_MAX_STRING_SIZE ); \
  15.     cerr << message << ": " << errorString << endl; \
  16.     throw std::runtime_error(string(message) + ": " + errorString) ;\
  17.   }
  18.  
  19. #include <map>
  20. #include <vector>
  21. #include <sstream>
  22. #include <chrono>
  23. #include <iomanip>
  24.  
  25. using namespace std;
  26. int main( int argc, char **argv )
  27. {
  28.   av_register_all();
  29.   AVFormatContext *pFormatCtx = 0;
  30.  
  31.   if( argc < 2 )
  32.   {
  33.     cerr << "Usage: " << argv[0] << " filename" << endl;
  34.     return 1;
  35.   }
  36.  
  37.   int result = avformat_open_input( &pFormatCtx, argv[1], NULL, NULL );
  38.   checkResult( result == 0, "Error opening file" );
  39.   result = avformat_find_stream_info( pFormatCtx, NULL );
  40.   checkResult( result >= 0, "Error finding infos" );
  41.  
  42.   AVPacket pkt;
  43.   auto start = chrono::steady_clock::now();
  44.   auto startPkt = start;
  45.   cerr << "Media duration: " << pFormatCtx->duration << endl;
  46.   int64_t mediaDurationSeconds = pFormatCtx->duration / AV_TIME_BASE;
  47.   int64_t lastPts;
  48.   while( av_read_frame( pFormatCtx, &pkt ) >= 0) {
  49.     auto now = chrono::steady_clock::now();    
  50.     if(now - startPkt > chrono::milliseconds(700)) {
  51.       auto time_base = pFormatCtx->streams[pkt.stream_index]->time_base;
  52.       int64_t packetTime = pkt.pts * time_base.num / time_base.den;
  53.       double percent = packetTime * 100.0 / mediaDurationSeconds;
  54.       cerr << "Current percent: " << percent << "%" << endl;
  55.       startPkt = now;
  56.     }
  57.    av_free_packet(&pkt);
  58.   }
  59.   auto elapsed = chrono::steady_clock::now() - start;
  60.   cerr << "Finished\n" << "elapsed: " << chrono::duration_cast<chrono::seconds>(elapsed).count() << " seconds; last pts: " << lastPts << endl;
  61.   return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement