Recherche avancée

Médias (91)

Autres articles (59)

  • MediaSPIP v0.2

    21 juin 2013, par

    MediaSPIP 0.2 est la première version de MediaSPIP stable.
    Sa date de sortie officielle est le 21 juin 2013 et est annoncée ici.
    Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
    Comme pour la version précédente, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
    Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...)

  • MediaSPIP version 0.1 Beta

    16 avril 2011, par

    MediaSPIP 0.1 beta est la première version de MediaSPIP décrétée comme "utilisable".
    Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
    Pour avoir une installation fonctionnelle, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
    Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...)

  • Gestion des droits de création et d’édition des objets

    8 février 2011, par

    Par défaut, beaucoup de fonctionnalités sont limitées aux administrateurs mais restent configurables indépendamment pour modifier leur statut minimal d’utilisation notamment : la rédaction de contenus sur le site modifiables dans la gestion des templates de formulaires ; l’ajout de notes aux articles ; l’ajout de légendes et d’annotations sur les images ;

Sur d’autres sites (3130)

  • using ffmpeg libavcodec to decode a video file then encode to H264, media file's duration is zero

    1er septembre 2017, par Qing

    Need help, recently, I am using ffmpeg libavcodec to decode a video file then encode to H264 and write to an mp4 media container, finally, the media file’s duration is zero, the following is my code’s workflow:

    AVFormatContext*    input_format_context = NULL;
    AVFormatContext*    output_format_context = NULL;
    AVIOContext*        output_io_context = NULL;
    AVCodecContext*     input_codec_context = NULL;
    AVCodecContext*     output_codec_context = NULL;
    AVCodec*            codec = NULL;
    AVStream*           input_stream = NULL;
    AVStream*           output_stream = NULL;
    AVFrame*            frame = NULL;

    int convert_init(const char* input_filename, const char* output_filename)
    {
       /** Allocate a new encode context */
       avformat_open_input(&input_format_context,
               input_filename, NULL, NULL);

       /** Get information on the input file (number of streams etc.). */
       avformat_find_stream_info(input_format_context, NULL);  

       /** Open the output file to write to it. */
       avio_open(&output_io_context, output_filename,
               AVIO_FLAG_WRITE);

       /** Create a new format context for the output container format. */
       output_format_context = avformat_alloc_context();

       /** Associate the output file (pointer) with the container format context. */
       output_format_context->pb = output_io_context;

       /** Guess the desired container format based on the file extension. */
       output_format_context->oformat = av_guess_format(NULL,
               output_filename, NULL);

       av_strlcpy((output_format_context)->filename, output_filename,
               sizeof(output_format_context->filename));

       /** stream0 is the video stream */
       AVStream* input_stream = input_format_context->streams[0];


       /**
        * Init the input_codec_context
        */

       /** Find a decoder for the audio stream. */
       codec = avcodec_find_decoder(input_stream->codecpar->codec_id);

       /** Allocate a new decode context */
       input_codec_context = avcodec_alloc_context3(codec);

       /** Initialize the stream parameters with demuxer information */
       avcodec_parameters_to_context(input_codec_context,
               input_stream->codecpar);    

       /** Open the decoder for the stream. */
       avcodec_open2(input_codec_context, codec, NULL);                                

       /**
        *  Create an output stream for writing encoded data
        *
        *  AM I MISSING SOMETHING ?
        *
        */
       output_stream = avformat_new_stream(output_format_context, NULL);

       /**
        * Init the output_codec_context
        */
       /** Find a encoder for the output video stream, using H264. */
       codec = avcodec_find_encoder(AV_CODEC_ID_H264);    

       /** Allocate an encode context. */
       output_codec_context = avcodec_alloc_context3(codec);  

       /**
        *  Setup encode context parameters.
        *  
        *  AM I MISSING SOMETHING ?
        *
        * */
       output_codec_context->bit_rate = input_codec_context->bit_rate;
       output_codec_context->width = input_codec_context->width;
       output_codec_context->height = input_codec_context->height;
       output_codec_context->time_base = (AVRational){1, 25};
       output_codec_context->framerate = (AVRational){25, 1};
       output_codec_context->gop_size = 10;
       output_codec_context->max_b_frames = 1;
       output_codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
       output_codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

       /** Setup output_stream codecpar. */
       avcodec_parameters_from_context(output_stream->codecpar,
               codec_context);

       /** Alloc an av frame */
       frame = av_frame_alloc();          

    }

    void convert_it()
    {
       AVPacket input_packet;
       AVPacket output_packet;

       /** Write the media file container header */
       avformat_write_header(output_format_context, NULL);

       /**
        * decode frames and encode to H264
        * */
       while (1) {
           av_init_packet(&input_packet);
           input_packet.data = NULL;
           input_packet.size = 0;

           av_init_packet(&output_packet);
           output_packet.data = NULL;
           output_packet.size = 0;

           /** Read a frame to decode */
           av_read_frame(input_format_context, &input_packet);        
           if (av_read_frame is end of file) {
               break;
           }
           ...
           ...

           /** Decoding... */
           avcodec_send_packet(input_codec_context, &input_packet);
           ...
           ...

           /** Get a decoded frame */
           avcodec_receive_frame(input_codec_context, frame);
           ...
           ...

           /** Make the frame writable, is it necessary ?? */
           av_frame_make_writable(frame);

           /** Encode to H264 */
           avcodec_send_frame(output_codec_context, frame);
           ...
           ...

           /** Get a encoded packet */
           avcodec_receive_packet(output_codec_context, &output_packet);

           /**
            * Write the packet to output.  
            * Here is the point! should I configure the parameters
            * in packet such as 'pts', 'dts', 'duration', etc, if so,
            * hwo? or I just directory write the packet to output?
            */
           av_interleaved_write_frame(output_format_context, &packet);                
       }

       /** Write the media file container trailer */
       av_write_trailer(output_format_context);

    }

    int main() {
       convert_init("./sample.avi", "./output.mp4");
       convert_it();

    }

    using VLC or QuickTime to playback the output.mp4 file, it failed, cause the file duration is zero, when dragging the time progress bar, I can see the picture frame clearly, it seems that the encoding packet buffer data is correct, but the timestamp is error, am I missing something when configure the output_stream ? The following is message from ffprobe.

    ffprobe output.mp4
    ffprobe version 3.3.3 Copyright (c) 2007-2017 the FFmpeg developers
     built with Apple LLVM version 8.1.0 (clang-802.0.42)
     configuration: --enable-shared --enable-libmp3lame
     libavutil      55. 58.100 / 55. 58.100
     libavcodec     57. 89.100 / 57. 89.100
     libavformat    57. 71.100 / 57. 71.100
     libavdevice    57.  6.100 / 57.  6.100
     libavfilter     6. 82.100 /  6. 82.100
     libswscale      4.  6.100 /  4.  6.100
     libswresample   2.  7.100 /  2.  7.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       encoder         : Lavf57.71.100
     Duration: 00:00:00.06, start: 0.000000, bitrate: 6902181 kb/s
       Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x408 [SAR 1:1 DAR 30:17], 13929056 kb/s, 90k fps, 90k tbr, 90k tbn, 50 tbc (default)
       Metadata:
         handler_name    : VideoHandler
  • MP4 Created Using FFmpeg API Can't Be Played in Media Players

    11 avril 2020, par RandyCroucher

    I've been struggling with this issue for days. There are similar issues posted here and around the web, but none of the solutions seem to work for me. They are possibly outdated ?

    



    Here is the current iteration of code I'm using to generate the MP4 file.

    



    It generates a simple 2 second .mp4 file that fails to play in any player I've tried. If I run that mp4 file back through the FFmpeg command line, it will generate a perfectly playable movie out of it. So the data is there.

    



    Also, if you modify the output file name in this code from .mp4 to .avi, this code generates a playable avi file too. So whatever it is, it is tied to the H.264 format.

    



    I'm sure I'm missing something simple, but for the life of me, I can't figure out what that is.

    



    Any help would be greatly appreciated !

    



    Here is a link to the VC++ project. MovieMaker.zip

    



    MovieMaker.h

    



    #pragma once&#xA;&#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;}&#xA;&#xA;class FMovieMaker&#xA;{&#xA;public:&#xA;    ~FMovieMaker();&#xA;&#xA;    bool Initialize(const char* FileName, int Width = 1920, int Height = 1080, int FPS = 30, int BitRate = 2000);&#xA;    bool RecordFrame(uint8_t* BGRAData);&#xA;    bool Finalize();&#xA;&#xA;    bool IsInitialized() const { return bInitialized; }&#xA;    int GetWidth() const { return CodecContext ? CodecContext->width : 0; }&#xA;    int GetHeight() const { return CodecContext ? CodecContext->height : 0; }&#xA;&#xA;private:&#xA;    bool EncodeFrame(bool bFinalize);&#xA;    void Log(const char* fmt, ...);&#xA;&#xA;    AVOutputFormat* OutputFormat = nullptr;&#xA;    AVFormatContext* FormatContext = nullptr;&#xA;    AVCodecContext* CodecContext = nullptr;&#xA;    AVFrame* Frame = nullptr;&#xA;    SwsContext* ColorConverter = nullptr;&#xA;    int64_t RecordedFrames = 0;&#xA;    bool bInitialized = false;&#xA;};&#xA;

    &#xA;&#xA;

    MovieMaker.cpp

    &#xA;&#xA;

    #include "MovieMaker.h"&#xA;&#xA;FMovieMaker::~FMovieMaker()&#xA;{&#xA;    if (IsInitialized())&#xA;        Finalize();&#xA;}&#xA;&#xA;bool FMovieMaker::Initialize(const char* FileName, int Width /*= 1920*/, int Height /*= 1080*/, int FPS /*= 30*/, int BitRate /*= 2000*/)&#xA;{&#xA;    OutputFormat = av_guess_format(nullptr, FileName, nullptr);&#xA;    if (!OutputFormat)&#xA;    {&#xA;        Log("Couldn&#x27;t guess the output format from the filename: %s", FileName);&#xA;        return false;&#xA;    }&#xA;&#xA;    AVCodecID CodecID = OutputFormat->video_codec;&#xA;    if (CodecID == AV_CODEC_ID_NONE)&#xA;    {&#xA;        Log("Could not determine a codec to use");&#xA;        return false;&#xA;    }&#xA;&#xA;    /* allocate the output media context */&#xA;    int ErrorCode = avformat_alloc_output_context2(&amp;FormatContext, OutputFormat, nullptr, FileName);&#xA;    if (ErrorCode &lt; 0)&#xA;    {&#xA;        char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;        Log("Failed to allocate format context: %s", Error);&#xA;        return false;&#xA;    }&#xA;    else if (!FormatContext)&#xA;    {&#xA;        Log("Failed to get format from filename: %s", FileName);&#xA;        return false;&#xA;    }&#xA;&#xA;    /* find the video encoder */&#xA;    const AVCodec* Codec = avcodec_find_encoder(CodecID);&#xA;    if (!Codec)&#xA;    {&#xA;        Log("Codec &#x27;%d&#x27; not found", CodecID);&#xA;        return false;&#xA;    }&#xA;&#xA;    /* create the video stream */&#xA;    AVStream* Stream = avformat_new_stream(FormatContext, Codec);&#xA;    if (!Stream)&#xA;    {&#xA;        Log("Failed to allocate stream");&#xA;        return false;&#xA;    }&#xA;&#xA;    /* create the codec context */&#xA;    CodecContext = avcodec_alloc_context3(Codec);&#xA;    if (!CodecContext)&#xA;    {&#xA;        Log("Could not allocate video codec context");&#xA;        return false;&#xA;    }&#xA;&#xA;    Stream->codecpar->codec_id = OutputFormat->video_codec;&#xA;    Stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    Stream->codecpar->width = Width;&#xA;    Stream->codecpar->height = Height;&#xA;    Stream->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;    Stream->codecpar->bit_rate = (int64_t)BitRate * 1000;&#xA;    avcodec_parameters_to_context(CodecContext, Stream->codecpar);&#xA;&#xA;    CodecContext->time_base = { 1, FPS };&#xA;    CodecContext->max_b_frames = 2;&#xA;    CodecContext->gop_size = 12;&#xA;    CodecContext->framerate = { FPS, 1 };&#xA;&#xA;    if (Stream->codecpar->codec_id == AV_CODEC_ID_H264)&#xA;        av_opt_set(CodecContext, "preset", "medium", 0);&#xA;    else if (Stream->codecpar->codec_id == AV_CODEC_ID_H265)&#xA;        av_opt_set(CodecContext, "preset", "medium", 0);&#xA;&#xA;    avcodec_parameters_from_context(Stream->codecpar, CodecContext);&#xA;&#xA;    if (FormatContext->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;        CodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;&#xA;    if ((ErrorCode = avcodec_open2(CodecContext, Codec, NULL)) &lt; 0)&#xA;    {&#xA;        char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;        Log("Failed to open codec: %s", Error);&#xA;        return false;&#xA;    }&#xA;&#xA;    if (!(OutputFormat->flags &amp; AVFMT_NOFILE))&#xA;    {&#xA;        if ((ErrorCode = avio_open(&amp;FormatContext->pb, FileName, AVIO_FLAG_WRITE)) &lt; 0)&#xA;        {&#xA;            char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;            av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;            Log("Failed to open file: %s", Error);&#xA;            return false;&#xA;        }&#xA;    }&#xA;&#xA;    Stream->time_base = CodecContext->time_base;&#xA;    if ((ErrorCode = avformat_write_header(FormatContext, NULL)) &lt; 0)&#xA;    {&#xA;        char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;        Log("Failed to write header: %s", Error);&#xA;        return false;&#xA;    }&#xA;&#xA;    CodecContext->time_base = Stream->time_base;&#xA;&#xA;    av_dump_format(FormatContext, 0, FileName, 1);&#xA;&#xA;    // create the frame&#xA;    {&#xA;        Frame = av_frame_alloc();&#xA;        if (!Frame)&#xA;        {&#xA;            Log("Could not allocate video frame");&#xA;            return false;&#xA;        }&#xA;        Frame->format = CodecContext->pix_fmt;&#xA;        Frame->width = CodecContext->width;&#xA;        Frame->height = CodecContext->height;&#xA;&#xA;        ErrorCode = av_frame_get_buffer(Frame, 32);&#xA;        if (ErrorCode &lt; 0)&#xA;        {&#xA;            char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;            av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;            Log("Could not allocate the video frame data: %s", Error);&#xA;            return false;&#xA;        }&#xA;    }&#xA;&#xA;    // create a color converter&#xA;    {&#xA;        ColorConverter = sws_getContext(CodecContext->width, CodecContext->height, AV_PIX_FMT_BGRA,&#xA;                                        CodecContext->width, CodecContext->height, AV_PIX_FMT_YUV420P, 0, 0, 0, 0);&#xA;        if (!ColorConverter)&#xA;        {&#xA;            Log("Could not allocate color converter");&#xA;            return false;&#xA;        }&#xA;    }&#xA;&#xA;    bInitialized = true;&#xA;    return true;&#xA;}&#xA;&#xA;bool FMovieMaker::RecordFrame(uint8_t* BGRAData)&#xA;{&#xA;    if (!bInitialized)&#xA;    {&#xA;        Log("Cannot record frames on an uninitialized Video Recorder");&#xA;        return false;&#xA;    }&#xA;&#xA;    /*make sure the frame data is writable */&#xA;    int ErrorCode = av_frame_make_writable(Frame);&#xA;    if (ErrorCode &lt; 0)&#xA;    {&#xA;        char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;        Log("Could not make the frame writable: %s", Error);&#xA;        return false;&#xA;    }&#xA;&#xA;    /* convert the bgra bitmap data into yuv frame data */&#xA;    int inLinesize[1] = { 4 * CodecContext->width }; // RGB stride&#xA;    sws_scale(ColorConverter, &amp;BGRAData, inLinesize, 0, CodecContext->height, Frame->data, Frame->linesize);&#xA;&#xA;    //Frame->pts = RecordedFrames&#x2B;&#x2B;;&#xA;    Frame->pts = CodecContext->time_base.den / CodecContext->time_base.num * CodecContext->framerate.den / CodecContext->framerate.num * (RecordedFrames&#x2B;&#x2B;);&#xA;    //The following assumes that codecContext->time_base = (AVRational){1, 1};&#xA;    //Frame->pts = frameduration * (RecordedFrames&#x2B;&#x2B;) * Stream->time_base.den / (Stream->time_base.num * fps);&#xA;    //Frame->pts &#x2B;= av_rescale_q(1, CodecContext->time_base, Stream->time_base);&#xA;&#xA;    return EncodeFrame(false);&#xA;}&#xA;&#xA;bool FMovieMaker::EncodeFrame(bool bFinalize)&#xA;{&#xA;    /* send the frame to the encoder */&#xA;    int ErrorCode = avcodec_send_frame(CodecContext, bFinalize ? nullptr : Frame);&#xA;    if (ErrorCode &lt; 0)&#xA;    {&#xA;        char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;        av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;        Log("Error sending a frame for encoding: %s", Error);&#xA;        return false;&#xA;    }&#xA;&#xA;    AVPacket Packet;&#xA;    av_init_packet(&amp;Packet);&#xA;    Packet.data = NULL;&#xA;    Packet.size = 0;&#xA;    Packet.flags |= AV_PKT_FLAG_KEY;&#xA;    Packet.pts = Frame->pts;&#xA;&#xA;    if (avcodec_receive_packet(CodecContext, &amp;Packet) == 0)&#xA;    {&#xA;        //std::cout &lt;&lt; "pkt key: " &lt;&lt; (Packet.flags &amp; AV_PKT_FLAG_KEY) &lt;&lt; " " &lt;&lt; Packet.size &lt;&lt; " " &lt;&lt; (counter&#x2B;&#x2B;) &lt;&lt; std::endl;&#xA;        uint8_t* size = ((uint8_t*)Packet.data);&#xA;        //std::cout &lt;&lt; "first: " &lt;&lt; (int)size[0] &lt;&lt; " " &lt;&lt; (int)size[1] &lt;&lt; " " &lt;&lt; (int)size[2] &lt;&lt; " " &lt;&lt; (int)size[3] &lt;&lt; " " &lt;&lt; (int)size[4] &lt;&lt; " " &lt;&lt; (int)size[5] &lt;&lt; " " &lt;&lt; (int)size[6] &lt;&lt; " " &lt;&lt; (int)size[7] &lt;&lt; std::endl;&#xA;&#xA;        av_interleaved_write_frame(FormatContext, &amp;Packet);&#xA;        av_packet_unref(&amp;Packet);&#xA;    }&#xA;&#xA;    return true;&#xA;}&#xA;&#xA;bool FMovieMaker::Finalize()&#xA;{&#xA;    if (!bInitialized)&#xA;    {&#xA;        Log("Cannot finalize uninitialized Video Recorder");&#xA;        return false;&#xA;    }&#xA;&#xA;    //DELAYED FRAMES&#xA;    AVPacket Packet;&#xA;    av_init_packet(&amp;Packet);&#xA;    Packet.data = NULL;&#xA;    Packet.size = 0;&#xA;&#xA;    for (;;)&#xA;    {&#xA;        avcodec_send_frame(CodecContext, NULL);&#xA;        if (avcodec_receive_packet(CodecContext, &amp;Packet) == 0)&#xA;        {&#xA;            av_interleaved_write_frame(FormatContext, &amp;Packet);&#xA;            av_packet_unref(&amp;Packet);&#xA;        }&#xA;        else&#xA;            break;&#xA;    }&#xA;&#xA;    av_write_trailer(FormatContext);&#xA;    if (!(OutputFormat->flags &amp; AVFMT_NOFILE))&#xA;    {&#xA;        int ErrorCode = avio_close(FormatContext->pb);&#xA;        if (ErrorCode &lt; 0)&#xA;        {&#xA;            char Error[AV_ERROR_MAX_STRING_SIZE];&#xA;            av_make_error_string(Error, AV_ERROR_MAX_STRING_SIZE, ErrorCode);&#xA;            Log("Failed to close file: %s", Error);&#xA;        }&#xA;    }&#xA;&#xA;    if (Frame)&#xA;    {&#xA;        av_frame_free(&amp;Frame);&#xA;        Frame = nullptr;&#xA;    }&#xA;&#xA;    if (CodecContext)&#xA;    {&#xA;        avcodec_free_context(&amp;CodecContext);&#xA;        CodecContext = nullptr;&#xA;    }&#xA;&#xA;    if (FormatContext)&#xA;    {&#xA;        avformat_free_context(FormatContext);&#xA;        FormatContext = nullptr;&#xA;    }&#xA;&#xA;    if (ColorConverter)&#xA;    {&#xA;        sws_freeContext(ColorConverter);&#xA;        ColorConverter = nullptr;&#xA;    }&#xA;&#xA;    bInitialized = false;&#xA;    return true;&#xA;}&#xA;&#xA;void FMovieMaker::Log(const char* fmt, ...)&#xA;{&#xA;    va_list args;&#xA;    fprintf(stderr, "LOG: ");&#xA;    va_start(args, fmt);&#xA;    vfprintf(stderr, fmt, args);&#xA;    va_end(args);&#xA;    fprintf(stderr, "\n");&#xA;}&#xA;

    &#xA;&#xA;

    Main.cpp

    &#xA;&#xA;

    #include "MovieMaker.h"&#xA;&#xA;uint8_t FtoB(float x)&#xA;{&#xA;    if (x &lt;= 0.0f)&#xA;        return 0;&#xA;    if (x >= 1.0f)&#xA;        return 255;&#xA;    else&#xA;        return (uint8_t)(x * 255.0f);&#xA;}&#xA;&#xA;void SetPixelColor(float X, float Y, float Width, float Height, float t, uint8_t* BGRA)&#xA;{&#xA;    t &#x2B;= 12.0f; // more interesting colors at this time&#xA;&#xA;    float P[2] = { 0.1f * X - 25.0f, 0.1f * Y - 25.0f };&#xA;    float V = sqrtf(P[0] * P[0] &#x2B; P[1] * P[1]);&#xA;    BGRA[0] = FtoB(sinf(V &#x2B; t / 0.78f));&#xA;    BGRA[1] = FtoB(sinf(V &#x2B; t / 10.0f));&#xA;    BGRA[2] = FtoB(sinf(V &#x2B; t / 36e2f));&#xA;    BGRA[3] = 255;&#xA;}&#xA;&#xA;int main()&#xA;{&#xA;    FMovieMaker MovieMaker;&#xA;&#xA;    const char* FileName = "C:\\ffmpeg\\MyMovieMakerMovie.mp4";&#xA;    int Width = 640;&#xA;    int Height = 480;&#xA;    int FPS = 30;&#xA;    int BitRateKBS = 2000;&#xA;&#xA;    if (MovieMaker.Initialize(FileName, Width, Height, FPS, BitRateKBS))&#xA;    {&#xA;        int Size = Width * 4 * Height;&#xA;        uint8_t* BGRAData = new uint8_t[Size];&#xA;        memset(BGRAData, 255, Size);&#xA;&#xA;        for (float Frame = 0; Frame &lt; 60; Frame&#x2B;&#x2B;)&#xA;        {&#xA;            // fill the image data with something interesting&#xA;            for (float Y = 0; Y &lt; Height; Y&#x2B;&#x2B;)&#xA;            {&#xA;                for (float X = 0; X &lt; Width; X&#x2B;&#x2B;)&#xA;                {&#xA;                    SetPixelColor(X, Y, (float)Width, (float)Height, Frame / (float)FPS, &amp;BGRAData[(int)(Y * Width &#x2B; X) * 4]);&#xA;                }&#xA;            }&#xA;&#xA;            if (!MovieMaker.RecordFrame(BGRAData))&#xA;                break;&#xA;        }&#xA;&#xA;        delete[] BGRAData;&#xA;&#xA;        MovieMaker.Finalize();&#xA;    }&#xA;}&#xA;

    &#xA;&#xA;

    If I have the lines that add the AV_CODEC_FLAG_GLOBAL_HEADER flag like shown above, I get all sorts of issues in the output from ffprobe MyMovieMakerMovie.mp4.

    &#xA;&#xA;

    C:\ffmpeg>ffprobe MyMovieMakerMovie.mp4&#xA;ffprobe version 4.2.2 Copyright (c) 2007-2019 the FFmpeg developers&#xA;  built with gcc 9.2.1 (GCC) 20200122&#xA;  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt&#xA;  libavutil      56. 31.100 / 56. 31.100&#xA;  libavcodec     58. 54.100 / 58. 54.100&#xA;  libavformat    58. 29.100 / 58. 29.100&#xA;  libavdevice    58.  8.100 / 58.  8.100&#xA;  libavfilter     7. 57.100 /  7. 57.100&#xA;  libswscale      5.  5.100 /  5.  5.100&#xA;  libswresample   3.  5.100 /  3.  5.100&#xA;  libpostproc    55.  5.100 / 55.  5.100&#xA;[h264 @ 000001d44b795b00] non-existing PPS 0 referenced&#xA;[h264 @ 000001d44b795b00] decode_slice_header error&#xA;[h264 @ 000001d44b795b00] no frame!&#xA;...&#xA;[h264 @ 000001d44b795b00] non-existing PPS 0 referenced&#xA;[h264 @ 000001d44b795b00] decode_slice_header error&#xA;[h264 @ 000001d44b795b00] no frame!&#xA;[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d44b783880] decoding for stream 0 failed&#xA;[mov,mp4,m4a,3gp,3g2,mj2 @ 000001d44b783880] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 640x480, 20528 kb/s): unspecified pixel format&#xA;Consider increasing the value for the &#x27;analyzeduration&#x27; and &#x27;probesize&#x27; options&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;MyMovieMakerMovie.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : isom&#xA;    minor_version   : 512&#xA;    compatible_brands: isomiso2avc1mp41&#xA;    encoder         : Lavf58.29.100&#xA;  Duration: 00:00:01.97, start: 0.000000, bitrate: 20529 kb/s&#xA;    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 640x480, 20528 kb/s, 30.51 fps, 30 tbr, 15360 tbn, 30720 tbc (default)&#xA;    Metadata:&#xA;      handler_name    : VideoHandler&#xA;

    &#xA;&#xA;

    Without adding the AV_CODEC_FLAG_GLOBAL_HEADER flag, I get a clean output from ffprobe, but the video still doesn't play. Notice it thinks the frame rate is 30.51, I'm not sure why.

    &#xA;&#xA;

    C:\ffmpeg>ffprobe MyMovieMakerMovie.mp4&#xA;ffprobe version 4.2.2 Copyright (c) 2007-2019 the FFmpeg developers&#xA;  built with gcc 9.2.1 (GCC) 20200122&#xA;  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt&#xA;  libavutil      56. 31.100 / 56. 31.100&#xA;  libavcodec     58. 54.100 / 58. 54.100&#xA;  libavformat    58. 29.100 / 58. 29.100&#xA;  libavdevice    58.  8.100 / 58.  8.100&#xA;  libavfilter     7. 57.100 /  7. 57.100&#xA;  libswscale      5.  5.100 /  5.  5.100&#xA;  libswresample   3.  5.100 /  3.  5.100&#xA;  libpostproc    55.  5.100 / 55.  5.100&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;MyMovieMakerMovie.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : isom&#xA;    minor_version   : 512&#xA;    compatible_brands: isomiso2avc1mp41&#xA;    encoder         : Lavf58.29.100&#xA;  Duration: 00:00:01.97, start: 0.000000, bitrate: 20530 kb/s&#xA;    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480, 20528 kb/s, 30.51 fps, 30 tbr, 15360 tbn, 60 tbc (default)&#xA;    Metadata:&#xA;      handler_name    : VideoHandler&#xA;

    &#xA;

  • Media-players show longest duration of multi-track MP4 files from FFmpeg [closed]

    9 juillet 2024, par EasonWaii

    I need to merge two videos into one using ffmpeg. The resulting video should have two video tracks and one audio track, with the audio track taken from the longer video.

    &#xA;

    When playing the merged video in a player like PotPlayer or VLC, it should default to playing the shorter video track. However, users should be able to switch to the other video track if they want.

    &#xA;

    The problem I am facing :&#xA;Everything is working fine, except when the player defaults to the shorter video track, it shows the timeline of the longer video track.

    &#xA;

     built with Apple clang version 15.0.0 (clang-1500.3.9.4)&#xA;  configuration: --prefix=/usr/local/Cellar/ffmpeg/7.0.1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags=&#x27;-Wl,-ld_classic&#x27; --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox&#xA;  libavutil      59.  8.100 / 59.  8.100&#xA;  libavcodec     61.  3.100 / 61.  3.100&#xA;  libavformat    61.  1.100 / 61.  1.100&#xA;  libavdevice    61.  1.100 / 61.  1.100&#xA;  libavfilter    10.  1.100 / 10.  1.100&#xA;  libswscale      8.  1.100 /  8.  1.100&#xA;  libswresample   5.  1.100 /  5.  1.100&#xA;  libpostproc    58.  1.100 / 58.  1.100&#xA;{&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;output21.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : isom&#xA;    minor_version   : 512&#xA;    compatible_brands: isomiso2avc1mp41&#xA;    encoder         : Lavf59.16.100&#xA;  Duration: 00:00:12.84, start: 0.000000, bitrate: 2207 kb/s&#xA;  Stream #0:0[0x1](und): Video: hevc (Main) (hev1 / 0x31766568), yuv420p(tv, progressive), 720x1280 [SAR 1:1 DAR 9:16], 1052 kb/s, 30 fps, 30 tbr, 15360 tbn&#xA;      Metadata:&#xA;        handler_name    : VideoHandler&#xA;        vendor_id       : [0][0][0][0]&#xA;  Stream #0:1[0x2](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 720x1280, 1293 kb/s, 29.83 fps, 29.83 tbr, 11456 tbn (default)&#xA;      Metadata:&#xA;        handler_name    : ?Mainconcept Video Media Handler&#xA;        vendor_id       : [0][0][0][0]&#xA;  Stream #0:2[0x3](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 130 kb/s (default)&#xA;      Metadata:&#xA;        handler_name    : SoundHandler&#xA;        vendor_id       : [0][0][0][0]&#xA;    "streams": [&#xA;        {&#xA;            "index": 0,&#xA;            "codec_name": "hevc",&#xA;            "codec_long_name": "H.265 / HEVC (High Efficiency Video Coding)",&#xA;            "profile": "Main",&#xA;            "codec_type": "video",&#xA;            "codec_tag_string": "hev1",&#xA;            "codec_tag": "0x31766568",&#xA;            "width": 720,&#xA;            "height": 1280,&#xA;            "coded_width": 720,&#xA;            "coded_height": 1280,&#xA;            "closed_captions": 0,&#xA;            "film_grain": 0,&#xA;            "has_b_frames": 2,&#xA;            "sample_aspect_ratio": "1:1",&#xA;            "display_aspect_ratio": "9:16",&#xA;            "pix_fmt": "yuv420p",&#xA;            "level": 93,&#xA;            "color_range": "tv",&#xA;            "chroma_location": "left",&#xA;            "field_order": "progressive",&#xA;            "refs": 1,&#xA;            "id": "0x1",&#xA;            "r_frame_rate": "30/1",&#xA;            "avg_frame_rate": "30/1",&#xA;            "time_base": "1/15360",&#xA;            "start_pts": 0,&#xA;            "start_time": "0.000000",&#xA;            "duration_ts": 196096,&#xA;            "duration": "12.766667",&#xA;            "bit_rate": "1052926",&#xA;            "nb_frames": "383",&#xA;            "extradata_size": 2480,&#xA;            "disposition": {&#xA;                "default": 0,&#xA;                "dub": 0,&#xA;                "original": 0,&#xA;                "comment": 0,&#xA;                "lyrics": 0,&#xA;                "karaoke": 0,&#xA;                "forced": 0,&#xA;                "hearing_impaired": 0,&#xA;                "visual_impaired": 0,&#xA;                "clean_effects": 0,&#xA;                "attached_pic": 0,&#xA;                "timed_thumbnails": 0,&#xA;                "non_diegetic": 0,&#xA;                "captions": 0,&#xA;                "descriptions": 0,&#xA;                "metadata": 0,&#xA;                "dependent": 0,&#xA;                "still_image": 0&#xA;            },&#xA;            "tags": {&#xA;                "language": "und",&#xA;                "handler_name": "VideoHandler",&#xA;                "vendor_id": "[0][0][0][0]"&#xA;            }&#xA;        },&#xA;        {&#xA;            "index": 1,&#xA;            "codec_name": "h264",&#xA;            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",&#xA;            "profile": "High",&#xA;            "codec_type": "video",&#xA;            "codec_tag_string": "avc1",&#xA;            "codec_tag": "0x31637661",&#xA;            "width": 720,&#xA;            "height": 1280,&#xA;            "coded_width": 720,&#xA;            "coded_height": 1280,&#xA;            "closed_captions": 0,&#xA;            "film_grain": 0,&#xA;            "has_b_frames": 2,&#xA;            "pix_fmt": "yuv420p",&#xA;            "level": 31,&#xA;            "color_range": "tv",&#xA;            "color_space": "bt709",&#xA;            "color_transfer": "bt709",&#xA;            "color_primaries": "bt709",&#xA;            "chroma_location": "left",&#xA;            "field_order": "progressive",&#xA;            "refs": 1,&#xA;            "is_avc": "true",&#xA;            "nal_length_size": "4",&#xA;            "id": "0x2",&#xA;            "r_frame_rate": "179/6",&#xA;            "avg_frame_rate": "3448256/115597",&#xA;            "time_base": "1/11456",&#xA;            "start_pts": 0,&#xA;            "start_time": "0.000000",&#xA;            "duration_ts": 115597,&#xA;            "duration": "10.090520",&#xA;            "bit_rate": "1293284",&#xA;            "bits_per_raw_sample": "8",&#xA;            "nb_frames": "301",&#xA;            "extradata_size": 46,&#xA;            "disposition": {&#xA;                "default": 1,&#xA;                "dub": 0,&#xA;                "original": 0,&#xA;                "comment": 0,&#xA;                "lyrics": 0,&#xA;                "karaoke": 0,&#xA;                "forced": 0,&#xA;                "hearing_impaired": 0,&#xA;                "visual_impaired": 0,&#xA;                "clean_effects": 0,&#xA;                "attached_pic": 0,&#xA;                "timed_thumbnails": 0,&#xA;                "non_diegetic": 0,&#xA;                "captions": 0,&#xA;                "descriptions": 0,&#xA;                "metadata": 0,&#xA;                "dependent": 0,&#xA;                "still_image": 0&#xA;            },&#xA;            "tags": {&#xA;                "language": "eng",&#xA;                "handler_name": "\u001fMainconcept Video Media Handler",&#xA;                "vendor_id": "[0][0][0][0]"&#xA;            }&#xA;        },&#xA;        {&#xA;            "index": 2,&#xA;            "codec_name": "aac",&#xA;            "codec_long_name": "AAC (Advanced Audio Coding)",&#xA;            "profile": "LC",&#xA;            "codec_type": "audio",&#xA;            "codec_tag_string": "mp4a",&#xA;            "codec_tag": "0x6134706d",&#xA;            "sample_fmt": "fltp",&#xA;            "sample_rate": "44100",&#xA;            "channels": 2,&#xA;            "channel_layout": "stereo",&#xA;            "bits_per_sample": 0,&#xA;            "initial_padding": 0,&#xA;            "id": "0x3",&#xA;            "r_frame_rate": "0/0",&#xA;            "avg_frame_rate": "0/0",&#xA;            "time_base": "1/44100",&#xA;            "start_pts": 0,&#xA;            "start_time": "0.000000",&#xA;            "duration_ts": 566244,&#xA;            "duration": "12.840000",&#xA;            "bit_rate": "130447",&#xA;            "nb_frames": "554",&#xA;            "extradata_size": 5,&#xA;            "disposition": {&#xA;                "default": 1,&#xA;                "dub": 0,&#xA;                "original": 0,&#xA;                "comment": 0,&#xA;                "lyrics": 0,&#xA;                "karaoke": 0,&#xA;                "forced": 0,&#xA;                "hearing_impaired": 0,&#xA;                "visual_impaired": 0,&#xA;                "clean_effects": 0,&#xA;                "attached_pic": 0,&#xA;                "timed_thumbnails": 0,&#xA;                "non_diegetic": 0,&#xA;                "captions": 0,&#xA;                "descriptions": 0,&#xA;                "metadata": 0,&#xA;                "dependent": 0,&#xA;                "still_image": 0&#xA;            },&#xA;            "tags": {&#xA;                "language": "und",&#xA;                "handler_name": "SoundHandler",&#xA;                "vendor_id": "[0][0][0][0]"&#xA;            }&#xA;        }&#xA;    ],&#xA;    "format": {&#xA;        "filename": "output21.mp4",&#xA;        "nb_streams": 3,&#xA;        "nb_programs": 0,&#xA;        "nb_stream_groups": 0,&#xA;        "format_name": "mov,mp4,m4a,3gp,3g2,mj2",&#xA;        "format_long_name": "QuickTime / MOV",&#xA;        "start_time": "0.000000",&#xA;        "duration": "12.840000",&#xA;        "size": "3543155",&#xA;        "bit_rate": "2207573",&#xA;        "probe_score": 100,&#xA;        "tags": {&#xA;            "major_brand": "isom",&#xA;            "minor_version": "512",&#xA;            "compatible_brands": "isomiso2avc1mp41",&#xA;            "encoder": "Lavf59.16.100"&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;

    &#xA;

    I want the timeline to display correctly according to the shorter video track when it is being played, without truncating the timeline of the longer video track and affecting its playback.

    &#xA;