Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:20:57+00:00 2026-05-27T06:20:57+00:00

I am trying to follow the sample code on encoding in the ffmpeg document

  • 0

I am trying to follow the sample code on encoding in the ffmpeg document and successfully build a application to encode and generate a mp4 file but I face the following problems:

1) I am using the H263 for encoding but I can only set the width and height of the AVCodecContext to 176×144, for other case (like 720×480 or 640×480) it will return fail.

2) I can’t play the output mp4 file by using the default Android player, isn’t it support H263 mp4 file? p.s. I can play it by using other player

3) Is there any sample code on encoding other video frame to make a new video (which mean decode the video and encode it back in different quality setting, also i would like to modify the frame content)?

Here is my code, thanks!

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; }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:20:58+00:00Added an answer on May 27, 2026 at 6:20 am

    H263 accepts only certain resolutions:

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

    It will fail with anything else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PROBLEM: Ok, I've been TRYING to follow the sample code on the MySQL Forge
I'm trying to follow code-to-interface on a project. Should I be creating an interface
I am trying to follow the steps on the following site http://msdn.microsoft.com/en-us/library/ms181089(VS.80).aspx But I
Trying to follow some basic examples and getting stuck by this code that is
I've been trying to follow this tutorial for deploying a WCF sample to IIS
I'm trying to follow this tutorial but instead of generating the expected hbm.xml files
I am trying to write a sample code for qt script. I thought I
I was trying to follow the instructions here but they seem to quickly be
I have been trying to follow this example (download the source code from a
I'm trying to follow THE magnificient Rails3 tutorial at http://railstutorial.org but ran into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.