Newest 'libx264' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/libx264

Les articles publiés sur le site

  • ffmpeg on Ubuntu-Linux - compile fails

    3 janvier 2018, par A.b.t.13

    (First of all i would like to sorry for any grammar mistakes, and to mention that I'm pretty much new to Linux :)) I'm having a really hard time trying to compile all the files related to the 'ffmpeg' command-line tool in my Ubuntu-16.04 environment.

    I've download the ffmpeg files from this link - http://www.ffmpeg.org/ extracted the files, and installed it by the instruction that supplied in the INSTALL.md file inside the folder.

    The process went well, but when i tried to run by this example :

    ffmpeg -i band_8k8k_3dv_projection.mp4 -filter_complex "[0:v]split=13[pr1][pr2][pr3][pr4][pr5][pr6][pr7][pr8][pr9][pr10][pr11][pr12][pr13];[pr1]crop=2048:2048:0:0[crp1];[pr2]crop=2048:2048:2048:0[crp2];[pr3]crop=2048:2048:4096:0[crp3];[pr4]crop=2048:2048:0:2048[crp4];[pr5]crop=2048:2048:2048:2048[crp5];[pr6]crop=2048:2048:4096:2048[crp6];[pr7]crop=2048:2048:0:4096[crp7];[pr8]crop=2048:2048:2048:4096[crp8];[pr9]crop=2048:2048:4096:4096[crp9];[pr10]crop=2048:2048:0:6144[crp10];[pr11]crop=2048:2048:2048:6144[crp11];[pr12]crop=2048:2048:4096:6144[crp12];[pr13]scale=6144:h=8192[pr13s];[pr13s][crp1]overlay=4096:0[ov1];[ov1][crp2]overlay=0:0[ov2];[ov2][crp3]overlay=4096:2048[ov3];[ov3][crp4]overlay=2048:2048[ov4];[ov4][crp5]overlay=2048:0[ov5];[ov5][crp6]overlay=0:2048[ov6];[ov6][crp7]overlay=4096:4096[ov7];[ov7][crp8]overlay=0:4096[ov8];[ov8][crp9]overlay=4096:6144[ov9];[ov9][crp10]overlay=2048:6144[ov10];[ov10][crp11]overlay=2048:4096[ov11];[ov11][crp12]overlay=0:6144[first];[first]split=22[prj1][prj2][prj3][prj4][prj5][prj6][prj7][prj8][prj9][prj10][prj11][prj12][prj13][prj14][prj15][prj16][prj17][prj18][prj19][prj20][prj21][prj22];[prj1]crop=1034:1034:0:0,pad=iw*2:ih[pd1]; [prj2]crop=1034:1034:0:0[cp1]; [pd1][cp1]overlay=w, scale=1504:h=464,setsar=1/1[crop1];[prj3]crop=1034:1034:0:0,pad=iw*2:ih[pd2]; [prj4]crop=1034:1034:0:0[cp2]; [pd2][cp2]overlay=w, scale=1504:h=464,setsar=1/1[crop2];[prj5]crop=1034:1034:0:0,pad=iw*2:ih[pd3]; [prj6]crop=1034:1034:0:0[cp3]; [pd3][cp3]overlay=w, scale=1504:h=464,setsar=1/1[crop3];[prj7]crop=1034:1034:0:0,pad=iw*2:ih[pd4]; [prj8]crop=1034:1034:0:4096[cp4]; [pd4][cp4]overlay=w, scale=1504:h=464,setsar=1/1[crop4];[prj9]crop=1034:1034:1014:0,pad=iw*2:ih[pd5]; [prj10]crop=1034:1034:1014:4096[cp5]; [pd5][cp5]overlay=w, scale=1504:h=464,setsar=1/1[crop5];[prj11]crop=1034:1034:2048:0,pad=iw*2:ih[pd6]; [prj12]crop=1034:1034:2048:4096[cp6]; [pd6][cp6]overlay=w, scale=1504:h=464,setsar=1/1[crop6];[prj13]crop=1034:1034:3062:0,pad=iw*2:ih[pd7]; [prj14]crop=1034:1034:3062:4096[cp7]; [pd7][cp7]overlay=w, scale=1504:h=464,setsar=1/1[crop7];[prj15]crop=1034:1034:4096:0,pad=iw*2:ih[pd8]; [prj16]crop=1034:1034:4096:4096[cp8]; [pd8][cp8]overlay=w, scale=1504:h=464,setsar=1/1[crop8];[prj17]crop=1034:1034:5110:0,pad=iw*2:ih[pd9]; [prj18]crop=1034:1034:5110:4096[cp9]; [pd9][cp9]overlay=w, scale=1504:h=464,setsar=1/1[crop9];[prj19]crop=1034:1034:0:1014,pad=iw*2:ih[pd10]; [prj20]crop=1034:1034:0:5110[cp10]; [pd10][cp10]overlay=w, scale=1504:h=464,setsar=1/1[crop10];[prj21]crop=1034:1034:1014:1014,pad=iw*2:ih[pd11]; [prj22]crop=1034:1034:1014:5110[cp11]; [pd11][cp11]overlay=w, scale=1504:h=464,setsar=1/1[crop11];[crop1]pad=iw:480*11,split=2[paded1][paded1a];[paded1][crop2]overlay=0:480*1[paded2];[paded2][crop3]overlay=0:480*2[paded3];[paded3][crop4]overlay=0:480*3[paded4];[paded4][crop5]overlay=0:480*4[paded5];[paded5][crop6]overlay=0:480*5[paded6];[paded6][crop7]overlay=0:480*6[paded7];[paded7][crop8]overlay=0:480*7[paded8];[paded8][crop9]overlay=0:480*8[paded9];[paded9][crop10]overlay=0:480*9[paded10];[paded10][crop11]overlay=0:480*10[out_no_black0];[paded1a]crop=1504:480,split=3[black0][black1][black2];[out_no_black0][black0]overlay=0:480*0[out_no_black1];[out_no_black1][black1]overlay=0:480*1[out_no_black2];[out_no_black2][black2]overlay=0:480*2[out_no_black3];[out_no_black3]crop=in_w:in_h" -preset slow -r 30 -pix_fmt yuv420p -movflags +faststart -coder 1 -cmp chroma -me_range 48 -me_method umh -subq 9 -refs 6 -b_strategy 2 -bf 16 -flags +loop+mv4+cgop -flags2 +local_header -forced-idr 1 -threads 0 -c:v libx264 -profile:v high -level 5.1 -x264opts "keyint=30:no-scenecut:min-keyint=30:fps=30" -b:v 70000k -maxrate 70000k -bufsize 140000k -slices 11 -c:a copy -f mp4 "band_8k8k_3dv_line_0_70000k.mp4" -y

    I've got this error: enter image description here

    I've looked for this problem, and found out that this unrecognized option may related to the lack of the library called "libx264". According to instruction i've found in this link - http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

    I configured the ffmpeg again with those flags --enable-gpl --enable-libx264 , installed th libx264 by this command - sudo apt-get install libx264-dev, all of those action went well, but when i try to compile it all together - i do it by running the command of 'make -j 10' or just 'make', when i'm inside the folder where all of the ffmpeg files are extracted to, i face this error - enter image description here

    Thanks for anyone who could help !

  • ffmpeg on Ubuntu-Linux - Cross-compile fails

    3 janvier 2018, par A.b.t.13

    (First of all i would like to sorry for any grammar mistakes, and to mention that I'm pretty much new to Linux :)) I'm having a really hard time trying to compile all the files related to the 'ffmpeg' command-line tool in my Ubuntu-16.04 environment.

    I've download the ffmpeg files from this link - http://www.ffmpeg.org/ extracted the files, and installed it by the instruction that supplied in the INSTALL.md file inside the folder.

    The process went well, but when i tried to run by this example :

    ffmpeg -i band_8k8k_3dv_projection.mp4 -filter_complex "[0:v]split=13[pr1][pr2][pr3][pr4][pr5][pr6][pr7][pr8][pr9][pr10][pr11][pr12][pr13];[pr1]crop=2048:2048:0:0[crp1];[pr2]crop=2048:2048:2048:0[crp2];[pr3]crop=2048:2048:4096:0[crp3];[pr4]crop=2048:2048:0:2048[crp4];[pr5]crop=2048:2048:2048:2048[crp5];[pr6]crop=2048:2048:4096:2048[crp6];[pr7]crop=2048:2048:0:4096[crp7];[pr8]crop=2048:2048:2048:4096[crp8];[pr9]crop=2048:2048:4096:4096[crp9];[pr10]crop=2048:2048:0:6144[crp10];[pr11]crop=2048:2048:2048:6144[crp11];[pr12]crop=2048:2048:4096:6144[crp12];[pr13]scale=6144:h=8192[pr13s];[pr13s][crp1]overlay=4096:0[ov1];[ov1][crp2]overlay=0:0[ov2];[ov2][crp3]overlay=4096:2048[ov3];[ov3][crp4]overlay=2048:2048[ov4];[ov4][crp5]overlay=2048:0[ov5];[ov5][crp6]overlay=0:2048[ov6];[ov6][crp7]overlay=4096:4096[ov7];[ov7][crp8]overlay=0:4096[ov8];[ov8][crp9]overlay=4096:6144[ov9];[ov9][crp10]overlay=2048:6144[ov10];[ov10][crp11]overlay=2048:4096[ov11];[ov11][crp12]overlay=0:6144[first];[first]split=22[prj1][prj2][prj3][prj4][prj5][prj6][prj7][prj8][prj9][prj10][prj11][prj12][prj13][prj14][prj15][prj16][prj17][prj18][prj19][prj20][prj21][prj22];[prj1]crop=1034:1034:0:0,pad=iw*2:ih[pd1]; [prj2]crop=1034:1034:0:0[cp1]; [pd1][cp1]overlay=w, scale=1504:h=464,setsar=1/1[crop1];[prj3]crop=1034:1034:0:0,pad=iw*2:ih[pd2]; [prj4]crop=1034:1034:0:0[cp2]; [pd2][cp2]overlay=w, scale=1504:h=464,setsar=1/1[crop2];[prj5]crop=1034:1034:0:0,pad=iw*2:ih[pd3]; [prj6]crop=1034:1034:0:0[cp3]; [pd3][cp3]overlay=w, scale=1504:h=464,setsar=1/1[crop3];[prj7]crop=1034:1034:0:0,pad=iw*2:ih[pd4]; [prj8]crop=1034:1034:0:4096[cp4]; [pd4][cp4]overlay=w, scale=1504:h=464,setsar=1/1[crop4];[prj9]crop=1034:1034:1014:0,pad=iw*2:ih[pd5]; [prj10]crop=1034:1034:1014:4096[cp5]; [pd5][cp5]overlay=w, scale=1504:h=464,setsar=1/1[crop5];[prj11]crop=1034:1034:2048:0,pad=iw*2:ih[pd6]; [prj12]crop=1034:1034:2048:4096[cp6]; [pd6][cp6]overlay=w, scale=1504:h=464,setsar=1/1[crop6];[prj13]crop=1034:1034:3062:0,pad=iw*2:ih[pd7]; [prj14]crop=1034:1034:3062:4096[cp7]; [pd7][cp7]overlay=w, scale=1504:h=464,setsar=1/1[crop7];[prj15]crop=1034:1034:4096:0,pad=iw*2:ih[pd8]; [prj16]crop=1034:1034:4096:4096[cp8]; [pd8][cp8]overlay=w, scale=1504:h=464,setsar=1/1[crop8];[prj17]crop=1034:1034:5110:0,pad=iw*2:ih[pd9]; [prj18]crop=1034:1034:5110:4096[cp9]; [pd9][cp9]overlay=w, scale=1504:h=464,setsar=1/1[crop9];[prj19]crop=1034:1034:0:1014,pad=iw*2:ih[pd10]; [prj20]crop=1034:1034:0:5110[cp10]; [pd10][cp10]overlay=w, scale=1504:h=464,setsar=1/1[crop10];[prj21]crop=1034:1034:1014:1014,pad=iw*2:ih[pd11]; [prj22]crop=1034:1034:1014:5110[cp11]; [pd11][cp11]overlay=w, scale=1504:h=464,setsar=1/1[crop11];[crop1]pad=iw:480*11,split=2[paded1][paded1a];[paded1][crop2]overlay=0:480*1[paded2];[paded2][crop3]overlay=0:480*2[paded3];[paded3][crop4]overlay=0:480*3[paded4];[paded4][crop5]overlay=0:480*4[paded5];[paded5][crop6]overlay=0:480*5[paded6];[paded6][crop7]overlay=0:480*6[paded7];[paded7][crop8]overlay=0:480*7[paded8];[paded8][crop9]overlay=0:480*8[paded9];[paded9][crop10]overlay=0:480*9[paded10];[paded10][crop11]overlay=0:480*10[out_no_black0];[paded1a]crop=1504:480,split=3[black0][black1][black2];[out_no_black0][black0]overlay=0:480*0[out_no_black1];[out_no_black1][black1]overlay=0:480*1[out_no_black2];[out_no_black2][black2]overlay=0:480*2[out_no_black3];[out_no_black3]crop=in_w:in_h" -preset slow -r 30 -pix_fmt yuv420p -movflags +faststart -coder 1 -cmp chroma -me_range 48 -me_method umh -subq 9 -refs 6 -b_strategy 2 -bf 16 -flags +loop+mv4+cgop -flags2 +local_header -forced-idr 1 -threads 0 -c:v libx264 -profile:v high -level 5.1 -x264opts "keyint=30:no-scenecut:min-keyint=30:fps=30" -b:v 70000k -maxrate 70000k -bufsize 140000k -slices 11 -c:a copy -f mp4 "band_8k8k_3dv_line_0_70000k.mp4" -y

    I've got this error: enter image description here

    I've looked for this problem, and found out that this unrecognized option may related to the lack of the library called "libx264". According to instruction i've found in this link - http://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

    I configured the ffmpeg again with those flags --enable-gpl --enable-libx264 , installed th libx264 by this command - sudo apt-get install libx264-dev, all of those action went well, but when i try to compile it all together - i do it by running the command of 'make -j 10' or just 'make', when i'm inside the folder where all of the ffmpeg files are extracted to, i face this error - enter image description here

    Thanks for anyone who could help !

  • How to set variable FPS in libx264 and what encoder parameters to use ?

    20 décembre 2017, par Iceman

    I'm trying to encode a webcam frames with libx264 in realtime, and face with one problem - the resulting video length is exactly what I set, but camera is delays somtimes and the real capture time is more than video length. As a result the picture in video changes to fast.
    I think it is due to constant FPS in x264 settings, so I need to make it dynamic somehow. Is it possible? If I wrong about FPS, so what I need to do, to synchronize capturing and writing?

    Also I would like to know what are the optimal encoder parameters for streaming via internet and for recording to disk (the client is streaming from camera or screen, and the server is recording)?

    Here is console logs screenshot and my code:

    #include 
    #include "stringf.h"
    #include "Capture.h"
    #include "x264.h"
    
    int main( int argc, char **argv ){
        Camera instance;
        if(!instance.Enable(0)){printf("Camera not available\n");return 1;}
        // Initializing metrics and buffer of frame
        unsigned int width, height, size = instance.GetMetrics(width, height);
        unsigned char *data = (unsigned char *)malloc(size);
        // Setting encoder (I'm not sure about all parameters)
        x264_param_t param;
        x264_param_default_preset(&param, "ultrafast", "zerolatency");
        param.i_threads = 1;
        param.i_width = width;
        param.i_height = height;
        param.i_fps_num = 20;
        param.i_fps_den = 1;
        // Intra refres:
        param.i_keyint_max = 8;
        param.b_intra_refresh = 1;
        // Rate control:
        param.rc.i_rc_method = X264_RC_CRF;
        param.rc.f_rf_constant = 25;
        param.rc.f_rf_constant_max = 35;
        // For streaming:
        param.b_repeat_headers = 1;
        param.b_annexb = 1;
    
        x264_param_apply_profile(&param, "baseline");
        x264_t* encoder = x264_encoder_open(&param);
    
        int seconds, expected_time, operation_start, i_nals, frame_size, frames_count;
        expected_time = 1000/param.i_fps_num;
        operation_start = 0;
        seconds = 1;
        frames_count = param.i_fps_num * seconds;
        int *Timings = new int[frames_count];
    
        x264_picture_t pic_in, pic_out;
        x264_nal_t* nals;
        x264_picture_alloc(&pic_in, X264_CSP_I420, param.i_width, param.i_height);
        // Capture-Encode-Write loop
        for(int i = 0; i < frames_count; i++){
            operation_start = GetTickCount();
            size = instance.GrabBGR(&data);
            instance.BGRtoI420(data, &pic_in.img.plane[0], &pic_in.img.plane[1], &pic_in.img.plane[2], param.i_width, param.i_height);
            frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
            if( frame_size > 0){
                stringf::WriteBufferToFile("test.h264",std::string(reinterpret_cast(nals->p_payload), frame_size),1);
            }
            Timings[i] = GetTickCount() - operation_start;
        }
        while( x264_encoder_delayed_frames( encoder ) ){ // Flush delayed frames
            frame_size = x264_encoder_encode(encoder, &nals, &i_nals, NULL, &pic_out);
            if( frame_size > 0 ){stringf::WriteBufferToFile("test.h264",std::string(reinterpret_cast(nals->p_payload), frame_size),1);}
        }
        unsigned int total_time = 0;
        printf("Expected operation time was %d ms per frame at %u FPS\n",expected_time, param.i_fps_num);
        for(unsigned int i = 0; i < frames_count; i++){
            total_time += Timings[i];
            printf("Frame %u takes %d ms\n",(i+1), Timings[i]);
        }
        printf("Record takes %u ms\n",total_time);
        free(data);
        x264_encoder_close( encoder );
        x264_picture_clean( &pic_in );
        return 0;
    }
    

    log_x264 The capture takes 1453 ms and the output file plays exactly 1 sec.

    So, in general, the video length must be the same as a capture time, but not as encoder "wants".
    How to do it?

  • libavformat/libavcodec providing invalid container header

    14 décembre 2017, par seanr8

    I'm using libavcodec to encode a stream to h264 and libavformat to store it in an mp4. The resulting container has an invalid header that can be played in VLC, but not any other player.

    I've found that using the mp4 container and the "mpeg4" codec produces a valid mp4 file, but using libx265 (HEVC) or the libx264 codec produces invalid mp4s.

    I can use ffmpeg -i invalid.mp4 -vcodec copy valid.mp4 and I get a file of almost the exact same size, but in a valid container.

    Examples of these files are here: Broken file and Repaied file [use the download links in the upper right to examine]

    I used a hex editor to see the differences in the headers of the two files and the invalid one is 1 byte smaller than the valid one.

    The code I'm using to open the container and codec and to write the header is here:

    AVOutputFormat *container_format;
    AVFormatContext *container_format_context;
    AVStream *video_stream;
    int ret;
    
    /* allocate the output media context */
    avformat_alloc_output_context2(&container_format_context, NULL, NULL, out_file);
    if (!container_format_context) {
        log(INFO, "Unable to determine container format from filename, exiting\n");
        exit(1);
    }
    else {
        log(INFO, "Using container %s\n", container_format_context->oformat->name);
    }
    
    if (!container_format_context) {
        log(ERROR, "Could not build container format context. Encoding failed.");
        exit(1);
    }
    
    container_format = container_format_context->oformat;
    
    /* Pull codec based on name */
    AVCodec* codec = avcodec_find_encoder_by_name(codec_name);
    if (codec == NULL) {
        log(ERROR, "Failed to locate codec \"%s\".",
                codec_name);
        exit(1);
    }
    
    /* create stream */
    video_stream = NULL;
    video_stream = avformat_new_stream(container_format_context, codec);
    if (!video_stream) {
        log(ERROR, "Could not allocate encoder stream. Cannot continue.\n");
        exit(1);
    }
    video_stream->id = container_format_context->nb_streams - 1;
    
    video_stream->time_base = video_stream->codec->time_base = (AVRational) { 1, 25};
    
    av_dump_format(container_format_context, 0, out_file, 1);
    
    
    
    /* Retrieve encoding context */
    AVCodecContext* avcodec_context = video_stream->codec;
    if (avcodec_context == NULL) {
        log(ERROR, "Failed to allocate context for "
                "codec \"%s\".", codec_name);
        exit(1);
    }
    
    
    /* Init context with encoding parameters */
    avcodec_context->bit_rate = bitrate;
    avcodec_context->width = width;
    avcodec_context->height = height;
    avcodec_context->gop_size = 10;
    avcodec_context->max_b_frames = 1;
    avcodec_context->qmax = 31;
    avcodec_context->qmin = 2;
    avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P;
    
    av_dump_format(container_format_context, 0, out_file, 1);
    
    /* Open codec for use */
    if (avcodec_open2(avcodec_context, codec, NULL) < 0) {
        log(ERROR, "Failed to open codec \"%s\".", codec_name);
        exit(1);
    }
    
    /* Allocate corresponding frame */
    AVFrame* frame = av_frame_alloc();
    if (frame == NULL) {
        exit(1);
    }
    
    /* Copy necessary data for frame from avcodec_context */
    frame->format = avcodec_context->pix_fmt;
    frame->width = avcodec_context->width;
    frame->height = avcodec_context->height;
    
    /* Allocate actual backing data for frame */
    if (av_image_alloc(frame->data, frame->linesize, frame->width,
                frame->height, frame->format, 32) < 0) {
        exit(1);
    }
    
    /* open the output file, if the container needs it */
    if (!(container_format->flags & AVFMT_NOFILE)) {
        ret = avio_open(&container_format_context->pb, out_file, AVIO_FLAG_WRITE);
        if (ret < 0) {
            log(ERROR, "Error occurred while opening output file: %s\n",
                    av_err2str(ret));
            exit(1);
        }
    }
    
    /* write the stream header, if needed */
    ret = avformat_write_header(container_format_context, NULL);
    if (ret < 0) {
        log(ERROR, "Error occurred while writing output file header: %s\n",
                    av_err2str(ret));
    }
    

    The code to encode a frame is here:

    /* Init video packet */
    AVPacket packet;
    av_init_packet(&packet);
    
    /* Request that encoder allocate data for packet */
    packet.data = NULL;
    packet.size = 0;
    
    /* Write frame to video */
    int got_data;
    if (avcodec_encode_video2(avcontext, &packet, frame, &got_data) < 0) {
        log(WARNING, "Error encoding frame #%" PRId64,
                video_struct->next_pts);
        return -1;
    }
    
    /* Write corresponding data to file */
    if (got_data) {
        if (packet.pts != AV_NOPTS_VALUE) {
            packet.pts = av_rescale_q(packet.pts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
        }
        if (packet.dts != AV_NOPTS_VALUE) {
            packet.dts = av_rescale_q(packet.dts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
        }
        write_packet(video_struct, &packet, packet.size);
        av_packet_unref(&packet);
    }
    

    And the code to write the packet to the video stream:

    static int write_packet(video_struct* video, void* data, int size) {
    
    int ret;
    
    /* use AVStream is not null, otherwise write to output fd */
    AVPacket *pkt = (AVPacket*) data;
    pkt->stream_index = video->output_stream->index;
    ret = av_interleaved_write_frame(video->container_format_context, pkt);
    if (ret != 0) {
        return -1;
    }
    
    /* Data was written successfully */
    return ret;
    }
    
  • erroneous pipeline : no element "x264enc"

    27 novembre 2017, par Cerato

    I have a gstreamer command that requires x264enc and the error I get is:

    WARNING: erroneous pipeline: no element "x264enc"

    I saw posts that the solution is to install gstreamer1.0-plugins-ugly, but I need to launch the command in Windows while I was managed to find the plugin only for Linux.

    Please help.