2011-11-30 60 views
1

我試圖按照ffmpeg文件中的編碼示例代碼,併成功構建應用程序來編碼並生成mp4文件,但我面臨以下問題:如何在Android中使用FFMpeg進行編碼(使用H263)

1)我使用H263進行編碼,但我只能將AVCodecContext的寬度和高度設置爲176x144,對於其他情況(如720x480或640x480),它將返回失敗。

2)我無法使用默認的Android播放器播放mp4文件,是不是支持H263 mp4文件?附:我可以通過使用其他播放器播放它

3)是否有任何代碼編碼其他視頻幀來製作一個新的視頻(這意味着解碼視頻和編碼回不同的質量設置,我也想修改幀內容)?

這是我的代碼,謝謝!

JNIEXPORT jint JNICALL Java_com_ffmpeg_encoder_FFEncoder_nativeEncoder(JNIEnv* env, jobject thiz, jstring filename){ 

LOGI("nativeEncoder()"); 

avcodec_register_all(); 
avcodec_init(); 
av_register_all(); 

AVCodec   *codec; 
AVCodecContext *codecCtx; 
int    i; 
int    out_size; 
int    size; 
int    x; 
int    y; 
int    output_buffer_size; 
FILE   *file; 
AVFrame   *picture; 
uint8_t   *output_buffer; 
uint8_t   *picture_buffer; 

/* Manual Variables */ 
int    l; 
int    fps = 30; 
int    videoLength = 5; 

/* find the H263 video encoder */ 
codec = avcodec_find_encoder(CODEC_ID_H263); 
if (!codec) { 
    LOGI("avcodec_find_encoder() run fail."); 
} 

codecCtx = avcodec_alloc_context(); 
picture = avcodec_alloc_frame(); 

/* put sample parameters */ 
codecCtx->bit_rate = 400000; 
/* resolution must be a multiple of two */ 
codecCtx->width = 176; 
codecCtx->height = 144; 
/* frames per second */ 
codecCtx->time_base = (AVRational){1,fps}; 
codecCtx->pix_fmt = PIX_FMT_YUV420P; 
codecCtx->codec_id = CODEC_ID_H263; 
codecCtx->codec_type = AVMEDIA_TYPE_VIDEO; 

/* open it */ 
if (avcodec_open(codecCtx, codec) < 0) { 
    LOGI("avcodec_open() run fail."); 
} 

const char* mfileName = (*env)->GetStringUTFChars(env, filename, 0); 

file = fopen(mfileName, "wb"); 
if (!file) { 
    LOGI("fopen() run fail."); 
} 

(*env)->ReleaseStringUTFChars(env, filename, mfileName); 

/* alloc image and output buffer */ 
output_buffer_size = 100000; 
output_buffer = malloc(output_buffer_size); 

size = codecCtx->width * codecCtx->height; 
picture_buffer = malloc((size * 3)/2); /* size for YUV 420 */ 

picture->data[0] = picture_buffer; 
picture->data[1] = picture->data[0] + size; 
picture->data[2] = picture->data[1] + size/4; 
picture->linesize[0] = codecCtx->width; 
picture->linesize[1] = codecCtx->width/2; 
picture->linesize[2] = codecCtx->width/2; 

for(l=0;l<videoLength;l++){ 
    //encode 1 second of video 
    for(i=0;i<fps;i++) { 
     //prepare a dummy image YCbCr 
     //Y 
     for(y=0;y<codecCtx->height;y++) { 
      for(x=0;x<codecCtx->width;x++) { 
       picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3; 
      } 
     } 

     //Cb and Cr 
     for(y=0;y<codecCtx->height/2;y++) { 
      for(x=0;x<codecCtx->width/2;x++) { 
       picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2; 
       picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5; 
      } 
     } 

     //encode the image 
     out_size = avcodec_encode_video(codecCtx, output_buffer, output_buffer_size, picture); 
     fwrite(output_buffer, 1, out_size, file); 
    } 

    //get the delayed frames 
    for(; out_size; i++) { 
     out_size = avcodec_encode_video(codecCtx, output_buffer, output_buffer_size, NULL); 
     fwrite(output_buffer, 1, out_size, file); 
    } 
} 

//add sequence end code to have a real mpeg file 
output_buffer[0] = 0x00; 
output_buffer[1] = 0x00; 
output_buffer[2] = 0x01; 
output_buffer[3] = 0xb7; 

fwrite(output_buffer, 1, 4, file); 
fclose(file); 
free(picture_buffer); 
free(output_buffer); 
avcodec_close(codecCtx); 
av_free(codecCtx); 
av_free(picture); 

LOGI("finish"); 

return 0; } 
+0

您是否設法編碼可在Android設備上播放的視頻?我正在努力處理類似的代碼。 Android設備上沒有任何東西可以播放,但在PC上的VLC中運行良好。 – Petrus

+0

我只想留下一個說明,只有少量工作將其轉換爲Java,因此我可以在Android上製作此編碼視頻。謝謝! – gtcompscientist

回答

3

H263只接受某些決議:

128 x 96 
176 x 144 
352 x 288 
704 x 576 
1408 x 1152 

它將失敗,其他任何東西。

相關問題