Advertisement
imbear

DLNAUtil

Nov 13th, 2014
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. int DLNAUtil_GetMediaThumbnailAtPath(char * fromMediaPath, char * toMediaPath, float atDuration) {
  2.     float duration = DLNAUtil_GetMediaDurationSecAtPath(fromMediaPath);
  3.     float targetDuration = 0;
  4.    
  5.     AVCodecContext *codec_ctx;
  6.     AVCodec *codec;
  7.    
  8.     int video_stream_index;
  9.  
  10.    
  11.     if (atDuration < 0) targetDuration = duration / 2;
  12.     else if (atDuration > duration) targetDuration = duration;
  13.     else targetDuration = atDuration;
  14.    
  15.     init();
  16.     AVFormatContext *fmt_ctx = NULL;
  17.     int ret = av_open_input_file(&fmt_ctx, fromMediaPath, NULL, 0, NULL);
  18.     if (ret >= 0 && fmt_ctx) {
  19.         codec = NULL;
  20.         codec_ctx = NULL;
  21.         video_stream_index  = -1;
  22.         for (int i = 0; i < fmt_ctx->nb_streams; i++) {
  23.             codec_ctx = fmt_ctx->streams[i]->codec;
  24.             if (CODEC_TYPE_VIDEO != codec_ctx->codec_type) {
  25.                 continue;
  26.             }
  27.             if (NULL == (codec = avcodec_find_decoder(codec_ctx->codec_id))) {
  28.                 printf("\n%s, codec not found.\n", __func__);
  29.                 continue;
  30.             }
  31.             if (0 != (avcodec_open(codec_ctx, codec))) {
  32.                 printf("\n%s, can not open codec\n", __func__);
  33.                 codec = NULL;
  34.                 continue;
  35.             }
  36.             video_stream_index = i;
  37.             break;
  38.         }
  39.         if (video_stream_index == -1 || codec_ctx->width == 0 || codec_ctx->height == 0) {
  40.             if (codec != NULL) {
  41.                 printf("\n%s, codec:%d, %dx%d\n", __func__, codec, codec_ctx->width, codec_ctx->height);
  42.                 avcodec_close(codec_ctx);
  43.             }
  44.             av_close_input_file(fmt_ctx);
  45.             return -1;
  46.         }
  47.         printf("\n%s, videoIndex:%d, codec:%d, %dx%d\n", __func__, video_stream_index, codec, codec_ctx->width, codec_ctx->height);
  48.         return video_stream_index   ;
  49.        
  50.        
  51.         int ret2 = av_seek_frame(fmt_ctx, 0, (targetDuration * AV_TIME_BASE), 0);
  52.         if (ret2 >= 0) {
  53.             AVPacket *pkt = NULL;
  54.             //int ret3 = av_read_frame(fmt_ctx, pkt);
  55.             while(av_read_frame(fmt_ctx, pkt) >= 0) {
  56.                 if (pkt->stream_index == 0);
  57.             }
  58.         }
  59.        
  60.     }
  61.    
  62.    
  63.    
  64.    
  65.    
  66.     return -1;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement