Newest 'libx264' Questions - Stack Overflow

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

Les articles publiés sur le site

  • FFMPEG - Width/ Height Not Divisible by 2 (Scaling to Generate MBR Output)

    15 avril 2020, par Sanjeev Pandey

    I am trying to generate multilple variants of videos in my library (Mp4 formats) and have renditions planned ranging from 1080p to 240p and popular sizes in between. For that I am taking a video with a AxB resolution and then running through a code (on bash) which scales them to desired following sizes - 426x240 640x360 842x480 1280x720 1920x1080, with different bitrates of course, and then saves as Mp4 again.

    Now, this works just fine if source video has height and width divisible by 2, but code breaks on the following line for the videos with odd width and height: -vf scale=w=${width}:h=${height}:force_original_aspect_ratio=decrease"

    Where 'width' and 'height' are the desired (and hardcoded) for every iteration: E.g. "426x240", and "640x360"

    The Error: [libx264 @ 00000187da2a1580] width not divisible by 2 (639x360) Error initializing output stream 1:0 -- Error while opening encoder for output stream #1:0 - maybe incorrect parameters such as bit_rate, rate, width or height

    Now approaches those are explained in this one doesn't work for me since I am scaling - FFMPEG (libx264) "height not divisible by 2"

    And, I tried this one too but it seems all qualities are getting the same size -ffmpeg : width not divisible by 2 (when keep proportions)

    • This is how I tried to use this one: scale='bitand(oh*dar,65534)':'min(${height},ih)'

    Kindly suggest how to solve this, keeping in view that: 1. I have a very large library and I can't do manual change for every video 2. I need to scale the video and keep the aspect ratio

    Thanks!

    PS: [Edit] One way that I can see is padding all of the odd height/ weight videos using a second script in advance. This however doubles my work time and load. I would prefer to keep it in single script. This is the script I see that I can use for padding: ```ffmpeg -r 24 -i -vcodec libx264 -y -an -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"`` (from: FFMPEG (libx264) "height not divisible by 2")

  • How would I send x264 encoded frames correctly over a network using UDP ?

    5 avril 2020, par Eoin McBennett

    I'm trying to send the encoded h264 frames I have over a network as I get them, currently I'm only streaming the nal units I get from each frame as it is encoded, is this the correct approach?

    I wrote a receiver application on a different computer to get the nals and wrote them all to a file sequentially, when played with vlc I didn't get any video and instead just got a screeching noise. I'm not sure exactly where the problem would lie here. I have included the result of the FFmpeg -I command on the file created.

    Encoder and sender code

    
        //Udp initialisation
        struct sockaddr_in broadcastAddr;
        int sock;
        int yes = 1;
        int addr_len;
        int count;
        fd_set readfd;
        char buffer[1024];
        int i;
    
        sock = socket(AF_INET, SOCK_DGRAM,0);
    
        if(sock < 0){
            std::cout << "Failed to initialise socket!" << std::endl;
        }
    
        int ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes));
        if(ret < 0){
            std::cout << "setsockopt error!" </ the size of the address
    
        memset((void*)&broadcastAddr,0,addr_len); //0 out the address bits
    
        broadcastAddr.sin_family = AF_INET;
        broadcastAddr.sin_addr.s_addr = INADDR_BROADCAST;
        broadcastAddr.sin_port = PORT;
    
    
    
        //Set the encoder parameters
        x264_param_t param;
        x264_param_default_preset(&param,"veryfast","zerolatency");
        param.i_threads = 1;
        param.i_width = camera.getWidth();
        param.i_height = camera.getHeight();
        param.i_fps_num = 30;
        param.i_fps_den = 1;
    // Intra refres:
        param.i_keyint_max = 30;
        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); //H.264 encoder object
        x264_picture_t pic_in, pic_out;
        x264_picture_alloc(&pic_in, X264_CSP_I420,camera.getWidth(), camera.getHeight());
    
        //Network abstraction layer units for broadcast
        x264_nal_t *nals;
        int i_nals;
    
        while(true){
    
            //If there is valid data in the processing queue
            if(!encoderQueue.empty()){
    
                //File the x264 input data structure with the file data
                fillImage(encoderQueue.front(),camera.getWidth(),camera.getHeight(),&pic_in);
    
                //Encode and send
                int frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
                if (frame_size >= 0) {
                    //The frame is ready to be sent over UDP!
                    for(int i = 0; i < i_nals; i++){
                        ret = sendto(sock, &nals[0].p_payload, frame_size,0,(struct sockaddr*)&broadcastAddr,addr_len);
                        if(ret > 0){
                            std::cout << "Streamed frame nal unit " << i  << std::endl;
                        } else{
                            std::cout << "Failed to stream nal unit " << i << std::endl;
                        }
                    }
                }
                else{
                    std::cout<<"Failed to encode h264 frame!" << std::endl;
                }
                //Finsihed with the current frame, pop it off the queue and remove any nals to do with it
                encoderQueue.pop();
                frame_size = 0;
                nals = nullptr;
                i_nals = 0;
            }
    
    
        }
    

    Receiver application

    #include 
    #include 
    #include Network.h>
    #include in.h>
    #include inet.h>
    #include types.h>
    #include socket.h>
    #include 
    
    
    #define BUFFER_LEN 10000
    #define PORT_NO 3879
    
    
    
    int main(int argc, const char * argv[]) {
    
    
        FILE *file; //File to write the h264 nals too
    
        //Declare the address memory space
        struct sockaddr_in sockAddr , bcAddr;
        socklen_t bcAddr_len = sizeof(&bcAddr); //Store the length of the broadcast address structure
        //0 out the assigned memory
        memset(&sockAddr, 0, sizeof(sockAddr));
        memset(&bcAddr, 0 ,sizeof(bcAddr));
    
        //Set the address parameters to look for incoming IpV4/UDP data
        sockAddr.sin_family = AF_INET;
        sockAddr.sin_port = htons(PORT_NO);
        sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
        bcAddr.sin_family = AF_INET;
        bcAddr.sin_port = PORT_NO;
        inet_aton("255.255.255.255",&bcAddr.sin_addr);
    
        //Initialise a udp socket to read broadcast bytes
        int soc = socket(AF_INET, SOCK_DGRAM,0);
    
        //Check socket init
        if(soc < 0){
            std::cout << "Failed to initialise UDP socket!" << std::endl;
            return -1;
        }
    
        //Bind the address details to the socket, check for errors
        if(bind(soc, (struct sockaddr*)&sockAddr, sizeof(sockAddr)) < 0){
            std::cout << "Failed to bind address structure to socket!" << std::endl;
            return -2;
        }
    
        file = fopen("stream.h264","wb"); // Open the file for writing
    
        unsigned char buffer[BUFFER_LEN];
    
        while(true){
    
    
            memset(&buffer, 0, sizeof(unsigned char) * BUFFER_LEN);
    
            int recv_len = recvfrom(soc, buffer, BUFFER_LEN, 0, (struct sockaddr *)&bcAddr, &bcAddr_len);
    
            std::cout<< "Received " << recv_len << "bytes on broadcast address" << std::endl;
    
            fwrite(&buffer, sizeof(unsigned char), recv_len, file);
        }
    
        return 0;
    }
    

    FFMPEG -I output

    FFMPEG -I output

    Any help would be greatly appreciated.

  • How can i build x264 and fdk-aac for android with ndk ?

    4 avril 2020, par Pradeep Simba

    How can i build x264 and fdk-aac for android with ndk?

    I build x264 and fdk-aac with ndk. But, it not done.

    I modified this build file and I build x264 and fdk-aac.But,it not done.

    How can I solve this?

  • How to fix grainy recoding with ffmpeg mp4 x264 ?

    21 mars 2020, par teenserie

    I recorded the audio and video stream from a streaming with ffmpeg. when I go to re-encode the file using libx264, the video in the movements looks bad and grainy as in the image. Where did I go wrong?

    Sample

    this is the code I used

    ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4
    

    and these are mediainfo of original file

        Metadata:
        major_brand     : isom
        minor_version   : 1
        compatible_brands: isom
        creation_time   : 2020-03-19T22:43:32.000000Z
      Duration: 00:39:51.99, start: 0.000000, bitrate: 1300 kb/s
        Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x1080 [SAR 3:2 DAR 16:9], Closed Captions, 1268 kb/s, 59.94 fps, 59.94 tbr, 90k tbn, 59.94 tbc (default)
        Metadata:
          handler_name    : VideoHandler
        Stream #0:1(spa): Audio: aac (HE-AACv2) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 22 kb/s (default)
        Metadata:
          handler_name    : SoundHandler
    

    mediainfo of the file recoded

     Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        encoder         : Lavf58.29.100
      Duration: 00:39:51.99, start: 0.000000, bitrate: 7924 kb/s
        Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], Closed Captions, 7892 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 119.88 tbc (default)
        Metadata:
          handler_name    : VideoHandler
        Stream #0:1(spa): Audio: aac (HE-AACv2) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 22 kb/s (default)
        Metadata:
          handler_name    : SoundHandler
    

    How can I recode the file without loss of quality? (sorry for my poor english)

  • How to benchmark x264 in ARM9 environment

    8 mars 2020, par curious_beast

    1.How to benchmark x264 video encoder in Dual-core ARM Cortex™-A9?. 2.How to calculate the encoding speed with respect to the CPU clock frequency?.