Recherche avancée

Médias (0)

Mot : - Tags -/auteurs

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (68)

  • Personnaliser en ajoutant son logo, sa bannière ou son image de fond

    5 septembre 2013, par

    Certains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;

  • Activation de l’inscription des visiteurs

    12 avril 2011, par

    Il est également possible d’activer l’inscription des visiteurs ce qui permettra à tout un chacun d’ouvrir soit même un compte sur le canal en question dans le cadre de projets ouverts par exemple.
    Pour ce faire, il suffit d’aller dans l’espace de configuration du site en choisissant le sous menus "Gestion des utilisateurs". Le premier formulaire visible correspond à cette fonctionnalité.
    Par défaut, MediaSPIP a créé lors de son initialisation un élément de menu dans le menu du haut de la page menant (...)

  • Ecrire une actualité

    21 juin 2013, par

    Présentez les changements dans votre MédiaSPIP ou les actualités de vos projets sur votre MédiaSPIP grâce à la rubrique actualités.
    Dans le thème par défaut spipeo de MédiaSPIP, les actualités sont affichées en bas de la page principale sous les éditoriaux.
    Vous pouvez personnaliser le formulaire de création d’une actualité.
    Formulaire de création d’une actualité Dans le cas d’un document de type actualité, les champs proposés par défaut sont : Date de publication ( personnaliser la date de publication ) (...)

Sur d’autres sites (8333)

  • using ffmpeg libraray to write to a mp4, ffprobe shows there are 100 frames and 100 packets, but av_interleaved_write_frame only called 50 times

    2 mai 2023, par ollydbg23

    here is my code to generate a mp4 file by using ffmpeg and opencv library. The opencv library is only try to generate 100 images(frames), and ffmpeg library is to compress the images to a mp4 files.

    


    Here is the working code :

    


    #include <iostream>&#xA;#include <vector>&#xA;#include <cstring>&#xA;#include <fstream>&#xA;#include <sstream>&#xA;#include <stdexcept>&#xA;#include <opencv2></opencv2>opencv.hpp>&#xA;extern "C" {&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;}&#xA;&#xA;#include<cstdlib> // to generate time stamps&#xA;&#xA;using namespace std;&#xA;using namespace cv;&#xA;&#xA;int main()&#xA;{&#xA;    // Set up input frames as BGR byte arrays&#xA;    vector<mat> frames;&#xA;&#xA;    int width = 640;&#xA;    int height = 480;&#xA;    int num_frames = 100;&#xA;    Scalar black(0, 0, 0);&#xA;    Scalar white(255, 255, 255);&#xA;    int font = FONT_HERSHEY_SIMPLEX;&#xA;    double font_scale = 1.0;&#xA;    int thickness = 2;&#xA;&#xA;    for (int i = 0; i &lt; num_frames; i&#x2B;&#x2B;) {&#xA;        Mat frame = Mat::zeros(height, width, CV_8UC3);&#xA;        putText(frame, std::to_string(i), Point(width / 2 - 50, height / 2), font, font_scale, white, thickness);&#xA;        frames.push_back(frame);&#xA;    }&#xA;&#xA;    // generate a serial of time stamps which is used to set the PTS value&#xA;    // suppose they are in ms unit, the time interval is between 30ms to 59ms&#xA;    vector<int> timestamps;&#xA;&#xA;    for (int i = 0; i &lt; num_frames; i&#x2B;&#x2B;) {&#xA;        int timestamp;&#xA;        if (i == 0)&#xA;            timestamp = 0;&#xA;        else&#xA;        {&#xA;            int random = 30 &#x2B; (rand() % 30);&#xA;            timestamp = timestamps[i-0] &#x2B; random;&#xA;        }&#xA;&#xA;        timestamps.push_back(timestamp);&#xA;    }&#xA;&#xA;    // Populate frames with BGR byte arrays&#xA;&#xA;    // Initialize FFmpeg&#xA;    //av_register_all();&#xA;&#xA;    // Set up output file&#xA;    AVFormatContext* outFormatCtx = nullptr;&#xA;    //AVCodec* outCodec = nullptr;&#xA;    AVCodecContext* outCodecCtx = nullptr;&#xA;    //AVStream* outStream = nullptr;&#xA;    //AVPacket outPacket;&#xA;&#xA;    const char* outFile = "output.mp4";&#xA;    int outWidth = frames[0].cols;&#xA;    int outHeight = frames[0].rows;&#xA;    int fps = 25;&#xA;&#xA;    // Open the output file context&#xA;    avformat_alloc_output_context2(&amp;outFormatCtx, nullptr, nullptr, outFile);&#xA;    if (!outFormatCtx) {&#xA;        cerr &lt;&lt; "Error: Could not allocate output format context" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Open the output file&#xA;    if (avio_open(&amp;outFormatCtx->pb, outFile, AVIO_FLAG_WRITE) &lt; 0) {&#xA;        cerr &lt;&lt; "Error opening output file" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Set up output codec&#xA;    const AVCodec* outCodec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;    if (!outCodec) {&#xA;        cerr &lt;&lt; "Error: Could not find H.264 codec" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    outCodecCtx = avcodec_alloc_context3(outCodec);&#xA;    if (!outCodecCtx) {&#xA;        cerr &lt;&lt; "Error: Could not allocate output codec context" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;    outCodecCtx->codec_id = AV_CODEC_ID_H264;&#xA;    outCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    outCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;    outCodecCtx->width = outWidth;&#xA;    outCodecCtx->height = outHeight;&#xA;    //outCodecCtx->time_base = { 1, fps*1000 };   // 25000&#xA;    outCodecCtx->time_base = { 1, fps};   // 25000&#xA;    outCodecCtx->framerate = {fps, 1};          // 25&#xA;    outCodecCtx->bit_rate = 4000000;&#xA;&#xA;    //https://github.com/leandromoreira/ffmpeg-libav-tutorial&#xA;    //We set the flag AV_CODEC_FLAG_GLOBAL_HEADER which tells the encoder that it can use the global headers.&#xA;    if (outFormatCtx->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;    {&#xA;        outCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; //&#xA;    }&#xA;&#xA;    // Open output codec&#xA;    if (avcodec_open2(outCodecCtx, outCodec, nullptr) &lt; 0) {&#xA;        cerr &lt;&lt; "Error: Could not open output codec" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Create output stream&#xA;    AVStream* outStream = avformat_new_stream(outFormatCtx, outCodec);&#xA;    if (!outStream) {&#xA;        cerr &lt;&lt; "Error: Could not allocate output stream" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Configure output stream parameters (e.g., time base, codec parameters, etc.)&#xA;    // ...&#xA;&#xA;    // Connect output stream to format context&#xA;    outStream->codecpar->codec_id = outCodecCtx->codec_id;&#xA;    outStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    outStream->codecpar->width = outCodecCtx->width;&#xA;    outStream->codecpar->height = outCodecCtx->height;&#xA;    outStream->codecpar->format = outCodecCtx->pix_fmt;&#xA;    outStream->time_base = outCodecCtx->time_base;&#xA;&#xA;    int ret = avcodec_parameters_from_context(outStream->codecpar, outCodecCtx);&#xA;    if (ret &lt; 0) {&#xA;        cerr &lt;&lt; "Error: Could not copy codec parameters to output stream" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    outStream->avg_frame_rate = outCodecCtx->framerate;&#xA;    //outStream->id = outFormatCtx->nb_streams&#x2B;&#x2B;;  &lt;--- We shouldn&#x27;t modify outStream->id&#xA;&#xA;    ret = avformat_write_header(outFormatCtx, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        cerr &lt;&lt; "Error: Could not write output header" &lt;&lt; endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Convert frames to YUV format and write to output file&#xA;    int frame_count = -1;&#xA;    for (const auto&amp; frame : frames) {&#xA;        frame_count&#x2B;&#x2B;;&#xA;        AVFrame* yuvFrame = av_frame_alloc();&#xA;        if (!yuvFrame) {&#xA;            cerr &lt;&lt; "Error: Could not allocate YUV frame" &lt;&lt; endl;&#xA;            return -1;&#xA;        }&#xA;        av_image_alloc(yuvFrame->data, yuvFrame->linesize, outWidth, outHeight, AV_PIX_FMT_YUV420P, 32);&#xA;&#xA;        yuvFrame->width = outWidth;&#xA;        yuvFrame->height = outHeight;&#xA;        yuvFrame->format = AV_PIX_FMT_YUV420P;&#xA;&#xA;        // Convert BGR frame to YUV format&#xA;        Mat yuvMat;&#xA;        cvtColor(frame, yuvMat, COLOR_BGR2YUV_I420);&#xA;        memcpy(yuvFrame->data[0], yuvMat.data, outWidth * outHeight);&#xA;        memcpy(yuvFrame->data[1], yuvMat.data &#x2B; outWidth * outHeight, outWidth * outHeight / 4);&#xA;        memcpy(yuvFrame->data[2], yuvMat.data &#x2B; outWidth * outHeight * 5 / 4, outWidth * outHeight / 4);&#xA;&#xA;        // Set up output packet&#xA;        //av_init_packet(&amp;outPacket); //error C4996: &#x27;av_init_packet&#x27;: was declared deprecated&#xA;        AVPacket* outPacket = av_packet_alloc();&#xA;        memset(outPacket, 0, sizeof(outPacket)); //Use memset instead of av_init_packet (probably unnecessary).&#xA;        //outPacket->data = nullptr;&#xA;        //outPacket->size = 0;&#xA;&#xA;        // set the frame pts, do I have to set the package pts?&#xA;&#xA;        // yuvFrame->pts = av_rescale_q(timestamps[frame_count]*25, outCodecCtx->time_base, outStream->time_base); //Set PTS timestamp&#xA;        yuvFrame->pts = av_rescale_q(frame_count*frame_count, outCodecCtx->time_base, outStream->time_base); //Set PTS timestamp&#xA;&#xA;        // Encode frame and write to output file&#xA;        int ret = avcodec_send_frame(outCodecCtx, yuvFrame);&#xA;        if (ret &lt; 0) {&#xA;            cerr &lt;&lt; "Error: Could not send frame to output codec" &lt;&lt; endl;&#xA;            return -1;&#xA;        }&#xA;        while (ret >= 0)&#xA;        {&#xA;            ret = avcodec_receive_packet(outCodecCtx, outPacket);&#xA;&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            {&#xA;                int abc;&#xA;                abc&#x2B;&#x2B;;&#xA;                break;&#xA;            }&#xA;            else if (ret &lt; 0)&#xA;            {&#xA;                cerr &lt;&lt; "Error: Could not receive packet from output codec" &lt;&lt; endl;&#xA;                return -1;&#xA;            }&#xA;&#xA;            //av_packet_rescale_ts(&amp;outPacket, outCodecCtx->time_base, outStream->time_base);&#xA;&#xA;            outPacket->stream_index = outStream->index;&#xA;&#xA;            outPacket->duration = av_rescale_q(1, outCodecCtx->time_base, outStream->time_base);   // Set packet duration&#xA;&#xA;            ret = av_interleaved_write_frame(outFormatCtx, outPacket);&#xA;&#xA;            static int call_write = 0;&#xA;&#xA;            call_write&#x2B;&#x2B;;&#xA;            printf("av_interleaved_write_frame %d\n", call_write);&#xA;&#xA;            av_packet_unref(outPacket);&#xA;            if (ret &lt; 0) {&#xA;                cerr &lt;&lt; "Error: Could not write packet to output file" &lt;&lt; endl;&#xA;                return -1;&#xA;            }&#xA;        }&#xA;&#xA;        av_frame_free(&amp;yuvFrame);&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    ret = avcodec_send_frame(outCodecCtx, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error flushing encoder: " &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        AVPacket* pkt = av_packet_alloc();&#xA;        if (!pkt) {&#xA;            std::cerr &lt;&lt; "Error allocating packet" &lt;&lt; std::endl;&#xA;            return -1;&#xA;        }&#xA;        ret = avcodec_receive_packet(outCodecCtx, pkt);&#xA;&#xA;        // Write the packet to the output file&#xA;        if (ret == 0)&#xA;        {&#xA;            pkt->stream_index = outStream->index;&#xA;            pkt->duration = av_rescale_q(1, outCodecCtx->time_base, outStream->time_base);   // &lt;---- Set packet duration&#xA;            ret = av_interleaved_write_frame(outFormatCtx, pkt);&#xA;            av_packet_unref(pkt);&#xA;            if (ret &lt; 0) {&#xA;                std::cerr &lt;&lt; "Error writing packet to output file: " &lt;&lt; std::endl;&#xA;                return -1;&#xA;            }&#xA;        }&#xA;    }&#xA;&#xA;&#xA;    // Write output trailer&#xA;    av_write_trailer(outFormatCtx);&#xA;&#xA;    // Clean up&#xA;    avcodec_close(outCodecCtx);&#xA;    avcodec_free_context(&amp;outCodecCtx);&#xA;    avformat_free_context(outFormatCtx);&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;</int></mat></cstdlib></stdexcept></sstream></fstream></cstring></vector></iostream>

    &#xA;

    Note that I have used the ffprobe tool(one of the tool from ffmpeg) to inspect the generated mp4 files.

    &#xA;

    I see that the mp4 file has 100 frames and 100 packets, but in my code, I have such lines :

    &#xA;

                static int call_write = 0;&#xA;&#xA;            call_write&#x2B;&#x2B;;&#xA;            printf("av_interleaved_write_frame %d\n", call_write);&#xA;

    &#xA;

    I just see that the av_interleaved_write_frame function is only called 50 times, not the expected 100 times, anyone can explain it ?

    &#xA;

    Thanks.

    &#xA;

    BTW, from the ffmpeg document( see here : For video, it should typically contain one compressed frame ), I see that a packet mainly has one video frame, so the ffprobe's result looks correct.

    &#xA;

    Here is the command I used to inspect the mp4 file :

    &#xA;

    ffprobe -show_frames output.mp4 >> frames.txt&#xA;ffprobe -show_packets output.mp4 >> packets.txt&#xA;

    &#xA;

    My testing code is derived from an answer in another question here : avformat_write_header() function call crashed when I try to save several RGB data to a output.mp4 file

    &#xA;

  • 'ffmpeg' or 'handbrake cli' for video conversion on server ? [closed]

    18 juin 2013, par AaronJiang

    I want to convert videos on my server using command line, maily mp4 -> flv or flv -> mp4. I googled and found these two products 'ffmpeg' and 'HandBrake cli'.

    http://ffmpeg.org/ffmpeg.html

    https://trac.handbrake.fr/wiki/CLIGuide

    Which one is better ?

    Plus I am running on ubuntu server.

  • How to implement HTTP Live Streaming server on Unix ?

    11 juillet 2019, par alex

    I just realized that Apple required HTTP Live Streaming in order to view videos in iPhone apps. I was not aware of this before... I am now trying to understand what this involves so I can decide whether I want to do the work and make the videos available in 3G or limit video playing to users who are connected to wi-fi.

    I read the overview provided by Apple, and now understand that my server needs to segment and index my media files. I also understand that I don’t have to host the content to be able to stream it (I can point to a video hosted somewhere else, right ?).

    What’s not clear to me at this point is what to implement on my server (Ubuntu Hardy) to do the actual segmenting and indexing on the fly (once again, I do not host the videos I want to serve).

    I found a link explaining how to install FFmpeg and X264, but I don’t know if this is the best solution (since I have an Ubuntu server, I can’t use the Apple Live Streaming tools, is it correct ?). Also, I do not understand at which point my server knows that a video needs to be converted and starts the job...

    Any feedback that could help me understand exactly what to do on the server side to be able to stream videos on my iPhone app in 3G would be greatly appreciated ! (Oh, and just it makes any difference, my app back-end is in Rails)