2017-05-05 1 views
0

Ich habe einen Code geschrieben, um ein Video in einer codierten Domäne zu lesen und Informationen wie Größe und Dauer von Frames abzurufen. AVPacket-Klasse besteht aus einer Variablen als Daten. Ich kann es lesen, aber da es ein Biss-Array ist, kann ich es nicht in lesbarem Format verwenden. Ich möchte diese Daten für den Vergleich mit einer anderen Videodatei verwenden. Bitte helfen Sie.So extrahieren Sie wichtige Informationen eines AV-Pakets aus einem codierten Video mit FFMPEG

void CFfmpegmethods::VideoRead(){ 
av_register_all(); 
avformat_network_init(); 
ofstream outdata; 
const char *url = "H:\\Sanduni_projects\\Sample_video.mp4"; 
AVDictionary *options = NULL; 
AVFormatContext *s = avformat_alloc_context(); //NULL; 

AVPacket *pkt = new AVPacket(); 

//open an input stream and read the header 
int ret = avformat_open_input(&s, url, NULL, NULL); 

//avformat_find_stream_info(s, &options); //finding the missing information 

if (ret < 0) 
    abort(); 

av_dict_set(&options, "video_size", "640x480", 0); 
av_dict_set(&options, "pixel_format", "rgb24", 0); 

if (avformat_open_input(&s, url, NULL, &options) < 0){ 
    abort(); 
} 

av_dict_free(&options); 

AVDictionaryEntry *e; 

if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) { 
    fprintf(stderr, "Option %s not recognized by the demuxer.\n", e->key); 
    abort(); 
} 

int i = 1; 
int j = 0; 
int64_t duration = 0; 
int size = 0; 
uint8_t *data = 0; //Unsigned integer type with a width of exactly 8 bits. 
int sum = 0; 

int total_size = 0; 
int total_duration = 0; 
int packet_size = 0; 
int64_t stream_index = 0; 
int64_t bit_rate = 0; 

//writing data to a file 
outdata.open("H:\\Sanduni_projects\\log.txt"); 

if (!outdata){ 
    cerr << "Error: file could not be opened" << endl; 
    exit(1); 
} 

//Split what is stored in the file into frames and return one for each call 
//returns the next frame of the stream 

while(1){ 
    int frame = av_read_frame(s, pkt); 
    if (frame < 0) break; 

    duration = pkt->duration; 
    size = pkt->size; 

    total_size = total_size + size; 
    total_duration = total_duration + duration; 

    cout << "frame:" << i << " " << size << " " << duration << endl; 
    data = pkt->data; 
    outdata << "Frame: " << i << " "; 
    outdata << data<< endl; 

    for (j = 0; j < size; j++){ 

    } 

    i++; 
    //pkt_no++; 
    //outdata << sum << endl;  
} 

//make the packet free 
av_packet_unref(pkt); 
delete pkt; 

cout << "total size: " << total_size << endl; 
cout << "total duration:" << total_duration << endl; 

outdata.close(); 

//Close the file after reading 
avformat_close_input(&s); 

}

+2

Was ist Ihre Frage? Sie beschreiben Probleme, die Sie haben, aber Sie haben keine Frage in Ihrer Frage ... – tambre

+0

Ich möchte drucken oder verwenden Sie die Informationen in "Daten" -Parameter und verwenden Sie es für Vergleiche und extrahieren nützliche Informationen enthalten. –

+0

Okay. Das willst du machen. Jetzt formuliere eine Frage und füge das zu deiner ... Frage hinzu. – tambre

Antwort

0

Ich kann es nicht in lesbarem Format verwenden

Warum Sie das wollen? Sie möchten Details von Ihnen ausgeben AVPAcket? Dann müssen Sie diese Details von Ihrem AVFormatContext bekommen. AVPacket hat stream_index und Sie können das verwenden, um Details des Streams zu erhalten, der das Paket darstellt. Die andere nützliche Information ist die pts/dts und die Größe des Pakets.

AVPacket pkt = ...; 
AVFormatContext *s = ... ; 

AVStream* stream = s->streams[pkt.stream_index]; // get the stream 

Grundsätzlich enthält jede Mediendatei mehrere Streams. Wenn Sie eine Datei öffnen, speichert AVFormatContext Informationen über die Datei, die Sie öffnen. AVFormatContext::streams sind die Streams, die Teil Ihrer Mediendatei sind (z. B. Audio, Video). Auf diese Weise können Sie von jedem AVPacketAVStream erhalten, dass das Paket darstellt. In AVStream können Sie codec, duration des Stroms und andere nützliche Parameter überprüfen.

+0

Eigentlich brauche ich es nicht lesbar. Ich möchte nur nützliche Informationen daraus extrahieren. Grundsätzlich möchte ich wissen, ob es möglich ist, DCT-Koeffizienten und Bewegungsvektoren zu extrahieren. Ich möchte sie zum Vergleich verwenden. –

+0

@SanduniWickramasinghe das wird nicht ganz einfach, wie Sie in Bitstream und zugehörige [AVCodecContext] (https://www.ffmpeg.org/doxygen/3.1/structAVCodecContext.html) gehen müssten. Möglicherweise müssen Sie selbst in den Bitstream einlesen. Versuchen Sie h264bitstream dafür (vorausgesetzt, Sie haben mit H264 zu tun) – Pavel

+0

Ich verstehe es nicht. Kannst du bitte mehr beschreiben. –

Verwandte Themen