Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- extern "C" {
- #include <libavcodec/avcodec.h> // required headers
- #include <libavformat/avformat.h>
- }
- #ifndef AV_ERROR_MAX_STRING_SIZE
- #define AV_ERROR_MAX_STRING_SIZE 256
- #endif
- #define checkResult(condition, message) if(! (condition) ) { \
- char errorString[AV_ERROR_MAX_STRING_SIZE]; \
- av_strerror(result, errorString, AV_ERROR_MAX_STRING_SIZE ); \
- cerr << message << ": " << errorString << endl; \
- throw std::runtime_error(string(message) + ": " + errorString) ;\
- }
- #include <map>
- #include <vector>
- #include <sstream>
- #include <chrono>
- #include <iomanip>
- using namespace std;
- int main( int argc, char **argv )
- {
- av_register_all();
- AVFormatContext *pFormatCtx = 0;
- if( argc < 2 )
- {
- cerr << "Usage: " << argv[0] << " filename" << endl;
- return 1;
- }
- int result = avformat_open_input( &pFormatCtx, argv[1], NULL, NULL );
- checkResult( result == 0, "Error opening file" );
- result = avformat_find_stream_info( pFormatCtx, NULL );
- checkResult( result >= 0, "Error finding infos" );
- AVPacket pkt;
- auto start = chrono::steady_clock::now();
- auto startPkt = start;
- cerr << "Media duration: " << pFormatCtx->duration << endl;
- int64_t mediaDurationSeconds = pFormatCtx->duration / AV_TIME_BASE;
- int64_t lastPts;
- while( av_read_frame( pFormatCtx, &pkt ) >= 0) {
- auto now = chrono::steady_clock::now();
- if(now - startPkt > chrono::milliseconds(700)) {
- auto time_base = pFormatCtx->streams[pkt.stream_index]->time_base;
- int64_t packetTime = pkt.pts * time_base.num / time_base.den;
- double percent = packetTime * 100.0 / mediaDurationSeconds;
- cerr << "Current percent: " << percent << "%" << endl;
- startPkt = now;
- }
- av_free_packet(&pkt);
- }
- auto elapsed = chrono::steady_clock::now() - start;
- cerr << "Finished\n" << "elapsed: " << chrono::duration_cast<chrono::seconds>(elapsed).count() << " seconds; last pts: " << lastPts << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement