Newest 'libx264' Questions - Stack Overflow

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

Les articles publiés sur le site

  • How to use libx264 to encode video in ffmpeg ?

    2 juin 2017, par wzjing

         I want to compress a video to smaller size, and if the video not encode by h264, then I transcode to h264. I have compiled ffmpeg 3.3.2 with libx264, When I using avcodec_send_frame(), it always return an error code, I use av_err2str(ret), and it said "Generic error in an external library" . Is this becouse my code is wrong or my lib is wrong? any way to crrect it or get more details?
         so here is my code of the transcode part, I do encode in the while loop, and the method to encode frame is encodeFrame(), the error happend in the last 3 line in method encodeFrame(), that means avcodec_send_frame(outCxt, outFrame) return whith an error:

    int transcode(){
        ......
    
        /******** Section 1 arguments configure ********/
        AVCodec outCodec = NULL;
        outCodec = avcodec_find_encoder_by_name("libx264");
        //tried avcodec_find_encoder(AV_CODEC_ID_H264) got same error
        if (outCodec == NULL) {
            LOGE("Unable to find encoder libx264");
            exit(1);
        }
        AVCodecContext *outCtx = NULL;
        outCtx = avcodec_alloc_context3(outCodec);
        outCtx->bit_rate = 40000;
        outCtx->width = inCtx->width;
        outCtx->height = inCtx->height;
        outCtx->time_base = (AVRational) {1, 25};
        outCtx->framerate = (AVRational) {25, 1};
        outCtx->gop_size = inCtx->gop_size;
        outCtx->max_b_frames = inCtx->max_b_frames;
        outCtx->pix_fmt = AV_PIX_FMT_YUV420P;
        outCtx->sample_fmt = inCtx->sample_fmt;
        outCtx->sample_rate = inCtx->sample_rate;
        outCtx->frame_size = inCtx->frame_size;
        outCtx->channel_layout = inCtx->channel_layout;
        outCtx->channels = inCtx->channels;
        av_opt_set(outCtx->priv_data, "preset", "slow", 0);
        ret = avcodec_open2(outCtx, outCodec, NULL);
        if (ret < 0)
            error(ret, "Error while inilizing outCtx instanceof AVCodecContext");
    
        /******** Section 2 init AVPacket and AVFrame ********/
        AVPacket *inPacket = NULL;
        inPacket = av_packet_alloc();
        av_init_packet(inPacket);
    
        AVFrame *vFrame;
        vFrame = av_frame_alloc();
        if (!vFrame)
            LOGE("Unable to allocate vFrame");
        vFrame->width = outCtx->width;
        vFrame->height = outCtx->height;
        vFrame->format = outCtx->pix_fmt;
    
        ret = av_frame_get_buffer(vFrame, 32);
        if (ret < 0)
            error(ret, "Unable to execute av_frame_get_buffer");
    
        /******** Section 3 start transcode ********/
        int k = 0;
        while (av_read_frame(inFormatContext, inPacket) == 0) {
            LOGI("--------------------- index:%d -------------------", k);
            resend:
            ret = avcodec_send_packet(inCtx, inPacket);
    
            if (ret == AVERROR(EOF))
                LOGW("> avcodec_send_packet: reache EOF");
            else if (ret == AVERROR(EAGAIN)) {
                LOGW("> avcodec_send_packet: last frame not been read");
                goto resend;
            }
            else if (ret == 0) {
                if (inPacket->stream_index == video_index) {
                    LOGD(">> receiving video frame");
                    avcodec_receive_frame(inCtx, vFrame);
                    encodeFrame(outFormatContext, outCtx, vFrame);
                } else {
                    //Currently, I igonred audio stream for now
                    LOGD(">> receiving audio frame");
                    avcodec_receive_frame(inCtx, aFrame);
                }
                av_packet_unref(inPacket);
                k++;
            } else {
                LOGE("> Error: avcodec_send_packet(%s)", av_err2str(ret));
                exit(1);
            }
    
        }
    
        ......
    }
    
    void encodeFrame(AVFormatContext *outFCxt, AVCodecContext *outCxt, AVFrame *outFrame) {
    
        LOGD(">> Encode Start");
        int ret;
    
        AVPacket *packet;
        packet = av_packet_alloc();
        ret = avcodec_send_frame(outCxt, outFrame);
        switch (ret) {
            case AVERROR(EAGAIN):
                LOGW(">>> avcodec_send_frame(last packet not received yet)");
                av_packet_unref(packet);
                encodeFrame(outFCxt, outCxt, outFrame);
                break;
            case AVERROR(EOF):
                LOGW(">>> avocdec_send_frame(reach end)");
                av_packet_unref(packet);
                break;
            case 0:
                av_frame_unref(outFrame);
                LOGD(">>> avcodec_send_frame(packet send success)");
                while (1){
                    ret = avcodec_receive_packet(outCxt, packet);
                    switch (ret) {
                        case AVERROR(EAGAIN):
                            LOGW(">>>> avcodec_receive_packet(no input)");
                            goto end;
                        case AVERROR(EOF):
                            LOGW(">>>> avcodec_receive_packet(reach end)");
                            goto end;
                        case 0:
                            LOGD(">>>> Writting frame");
                            ret = av_write_frame(outFCxt, packet);
                            if (ret == 0){
                                LOGD(">>>>> Write finished");
                                av_packet_unref(packet);
                            }
                            else {
                                LOGE(">>>>> Error:av_write_frame(%s)", av_err2str(ret));
                                exit(1);
                            }
                            goto end;
                        default:
                            LOGE(">>>> Error:avcode_receive_packet(%s)", av_err2str(ret));
                            exit(1);
    
                    }
                    end:
                    break;
                }
                break;
            default:
                /** I got a "Generic error in an external library" here **/
                LOGE(">>> Error: avcodec_send_frame(%s)", av_err2str(ret));
                exit(1);
        }
    

    }

  • FFMPEG concat causing audio glitches in segments boundaries

    28 mai 2017, par Idan

    I am segmenting a video file to 10sec segments, transcoding each of them and then run a concat to merge them all back together. While the whole process works without errors and the final file is valid and playable, I've noticed some sort of glitches/interruptions in the audio stream when passing over the original segments boundaries.

    For example, in a 60sec video that was segmented to 10sec segments and stitched back again, the final output will show audio interruptions in 10/20/30/40/50 sec timestamps.

    For segmenting the files I use:

    ffmpeg -y -i $INPUT_FILE -c copy -flags -global_header -segment_time 10 -break_non_keyframes 0 -reset_timestamps 1 -segment_list file_segs.list -segment_list_type ffconcat -write_empty_segments 0 -segment_format mp4  -f segment file_seg-%d.mp4
    

    And for concat I use:

    ffmpeg -y -f concat -i file_segs.list -c copy -movflags +faststart file_video.mp4
    

    Transcoding is to libx264 / aac

    What might be the cause? Is there any way to work around this problem?

  • ffmpeg -movflags +faststart in clipbucket video script

    22 mai 2017, par Spirog

    i am a little new at ffmpeg.

    Have a video script ( ClipBucket ) and would like to know how to add -movflags +faststart to the script to take advantage of this for .mp4 videos being converted.

    here is the code ffmpeg.class.php. Not sure where I can add this to work properly

    https://github.com/arslancb/clipbucket/tree/master/upload/includes/classes/conversion

    ffmpeg.class.php and conversion.class.php

    Seems to be the correct file(s), just not sure where to add it

    Thanks so much in advance.

    Spiro

  • FFMPEG (libx264) "height not divisible by 2"

    16 mai 2017, par Andy Hin

    I am trying to encode a .mp4 video from a set of frames using FFMPEG using the libx264 codec.

    This is the command I am running:

    /usr/local/bin/ffmpeg -r 24 -i frame_%05d.jpg -vcodec libx264 -y -an video.mp4
    

    I sometimes get the following error:

    [libx264 @ 0xa3b85a0] height not divisible by 2 (520x369)
    

    After searching around a bit it seems that the issue has something to do with the scaling algorithm and can be fixed by adding a -vf argument.

    However, in my case I don't want to do any scaling. Ideally, I want to keep the dimensions exactly the same as the frames. Any advice? Is there some sort of aspect ratio that h264 enforces?

  • ffmpeg x264 CPU core scaling

    12 mai 2017, par alex416

    Does anyone have any experence with this type of thing? Currently using a Ryzen 7 8c/16T CPU, and using a program called BES. Using 10% CPU, is almost no slower at transcoding from mkv to mp4 (both x264, i know i can just copy them) than using 100% cpu on all 16 threads. So, Since this is the case, What is the ideal core count for libx264? I mean, Does the same type of thing happen with 32 threads? Or 8 threads, etc.