Recherche avancée

Médias (1)

Mot : - Tags -/ticket

Autres articles (52)

  • 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 ;

  • 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 ) (...)

  • Qu’est ce qu’un éditorial

    21 juin 2013, par

    Ecrivez votre de point de vue dans un article. Celui-ci sera rangé dans une rubrique prévue à cet effet.
    Un éditorial est un article de type texte uniquement. Il a pour objectif de ranger les points de vue dans une rubrique dédiée. Un seul éditorial est placé à la une en page d’accueil. Pour consulter les précédents, consultez la rubrique dédiée.
    Vous pouvez personnaliser le formulaire de création d’un éditorial.
    Formulaire de création d’un éditorial Dans le cas d’un document de type éditorial, les (...)

Sur d’autres sites (4028)

  • Adding A New System To The Game Music Website

    1er août 2012, par Multimedia Mike — General

    At first, I was planning to just make a little website where users could install a Chrome browser extension and play music from old 8-bit NES games. But, like many software projects, the goal sort of ballooned. I created a website where users can easily play old video game music. It doesn’t cover too many systems yet, but I have had individual requests to add just about every system you can think of.

    The craziest part is that I know it’s possible to represent most of the systems. Eventually, it would be great to reach Chipamp parity (a combination plugin for Winamp that packages together plugins for many of these chiptunes). But there is a process to all of this. I have taken to defining a number of phases that are required to get a new system covered.

    Phase 0 informally involves marveling at the obscurity of some of the console systems for which chiptune collections have evolved. WonderSwan ? Sharp X68000 ? PC-88 ? I may be viewing this through a terribly Ameri-centric lens. I’ve at least heard of the ZX Spectrum and the Amstrad CPC even if I’ve never seen either.

    No matter. The goal is to get all their chiptunes cataloged and playable.

    Phase 1 : Finding A Player
    The first step is to find a bit of open source code that can play a particular format. If it’s a library that can handle many formats, like Game Music Emu or Audio Overload SDK, even better (probably). The specific open source license isn’t a big concern for me. I’m almost certain that some of the libraries that SaltyGME currently mixes are somehow incompatible, license-wise. I’ll worry about it when I encounter someone who A) cares, and B) is in a position to do something about it. Historical preservation comes first, and these software libraries aren’t getting any younger (I’m finding some that haven’t been touched in a decade).

    Phase 2 : Test Program
    The next phase is to create a basic test bench program that sends a music file into the library, generates a buffer of audio, and shoves it out to the speakers via PulseAudio’s simple API (people like to rip on PulseAudio, but its simple API really lives up to its name and requires pages less boilerplate code to play a few samples than ALSA).

    Phase 3 : Plug Into Web Player
    After successfully creating the test bench and understanding exactly which source files need to be built, the next phase is to hook it up to the main SaltyGME program via the ad-hoc plugin API I developed. This API requires that a player backend can, at the very least, initialize itself based on a buffer of bytes and generate audio samples into an array of 16-bit numbers. The API also provides functions for managing files with multiple tracks and toggling individual voices/channels if the library supports such a feature. Having the test bench application written beforehand usually smooths out this step.

    But really, I’m just getting started.

    Phase 4 : Collecting A Song Corpus
    Then there is the matter of staging a collection of songs for a given system. It seems like it would just be a matter of finding a large collection of songs for a given format, downloading them in bulk, and mirroring them. Honestly, that’s the easy part. People who are interested in this stuff have been lovingly curating massive collections of these songs for years (see SNESmusic.org for one of the best examples, and they also host a torrent of all their music for really quick and easy hoarding).

    In my drive to make this game music website more useful for normal people, the goal is to extract as much metadata as possible to make searching better, and to package the data so that it’s as convenient as possible for users. Whenever I seek to add a new format to the collection, this is the phase where I invariably find that I have to fundamentally modify some of the assumptions I originally made in the player.

    First, there were the NES Sound Format (NSF) files, the original format I wanted to play. These are files that have any number of songs packed into a single file. Playback libraries expose APIs to jump to individual tracks. So the player was designed around that. Game Boy GBS files also fall into this category but present a different challenge vis-à-vis metadata, addressed in the next phase.

    Then, there were the SPC files. Each SPC file is its own song and multiple SPC files are commonly bundled as RAR files. Not wanting to deal with RAR, or any format where I interacted with a general compression API to pull a few files out, I created a custom resource format (inspired by so many I have studied and documented) and compressed it with a simpler compression API. I also had to modify some of the player’s assumptions to deal with this archive format. Genesis VGMs, bundled either in .zip or .7z, followed the same model as SPC in RAR.

    Then it was suggested that I attempt to bring SaltyGME closer to feature parity with Chipamp, rather than just being a Chrome browser frontend for Game Music Emu. When I studied the Portable Sound Format (PSF), I realized it didn’t fit into the player model I already had. PSF uses a sort of shared library model for code execution and I developed another resource archive format to cope with it. So that covers quite a few formats.

    One more architecture challenge arose when I started to study one of the prevailing metadata formats, explained in the next phase.

    Phase 5 : Metadata
    Finally, for the collections to really be useful, I need to harvest that juicy metadata for search and presentation.

    I have created a series of programs and scripts to scrape metadata out of these music files and store it all in a database that drives the website and search engine. I recognize that it’s no good to have a large corpus of songs with minimal metadata and while importing bulk quantities of music, the scripts harshly reject songs that have too little metadata.

    Again, challenges abound. One of the biggest challenges I’m facing is the peculiar quasi-freeform metadata format that emerged as .m3u that takes a form similar to :

    #################################################################
    #
    # GRADIUS2
    # (c) KONAMI  by Furukawa Motoaki, IKACHAN
    #
    #################################################################
    

    nemesis2.kss::KSS,62,[Nemesis2] (Opening),2:23,,0
    nemesis2.kss::KSS,61,[Nemesis2] (Start),7,,0
    nemesis2.kss::KSS,43,[Nemesis2] (Air Battle),34,0-
    nemesis2.kss::KSS,44,[Nemesis2] (1st. BGM),51,0-
    [...]

    A lot of file formats (including Game Boy GBS mentioned earlier) store their metadata separately using this format. I have some ideas about tools I can use to help me process this data but I’m pretty sure each one will require some manual intervention.

    As alluded to in phase 4, .m3u presents another architectural challenge : Notice the second field in the CSV .m3u data. That’s a track number. A player can’t expect every track in a bundled chiptune file to be valid, nor to be in any particular order. Thus, I needed to alter the architecture once more to take this into account. However, instead of modifying the SaltyGME player, I simply extended the metadata database to include a playback order which, by default, is the same as the track order but can also accommodate this new issue. This also has the bonus of providing a facility to exclude playback of certain tracks. This comes in handy for many PSF archives which tend to include files that only provide support for other files and aren’t meant to be played on their own.

    Bright Side
    The reward for all of this effort is that the data lands in a proper database in the end. None of it goes back into the chiptune files themselves. This makes further modification easier as all of the data that is indexed and presented on the site comes from the database. Somewhere down the road, I should probably create an API for accessing this metadata.

  • Downscaling a video from 1080p to 480p using swscale and encoding to x265 gives a glitched output

    5 mai 2023, par lokit khemka

    I am basically first scaling a frame and then sending the frame to the encoder as below :

    


    scaled_frame->pts = input_frame->pts;
scaled_frame->pkt_dts = input_frame->pkt_dts;
scaled_frame->pict_type = input_frame->pict_type;
sws_scale_frame(encoder->sws_ctx, scaled_frame, input_frame);
if (encode_video(decoder, encoder, scaled_frame))
     return -1;


    


    The scaling context is configured as :

    


    scaled_frame->width = 854;
scaled_frame->height=480; 
encoder->sws_ctx = sws_getContext(1920, 1080,
                            decoder->video_avcc->pix_fmt, 
                           scaled_frame->width, scaled_frame->height, decoder->video_avcc->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL );
    if (!encoder->sws_ctx){logging("Cannot Create Scaling Context."); return -1;}


    


    The encoder is configured as :

    


        encoder_sc->video_avcc->height = decoder_ctx->height; //1080
    encoder_sc->video_avcc->width = decoder_ctx->width; //1920
    encoder_sc->video_avcc->bit_rate = 2 * 1000 * 1000;
    encoder_sc->video_avcc->rc_buffer_size = 4 * 1000 * 1000;
    encoder_sc->video_avcc->rc_max_rate = 2 * 1000 * 1000;
    encoder_sc->video_avcc->rc_min_rate = 2.5 * 1000 * 1000;

    encoder_sc->video_avcc->time_base = av_inv_q(input_framerate);
    encoder_sc->video_avs->time_base = encoder_sc->video_avcc->time_base;


    


    When I get the output, the output video is 1080p and I have glitches like : enter image description here

    


    I changed the encoder avcc resolution to 480p (854 x 480). However, that is causing the video to get sliced to the top quarter of the original frame.
I am new to FFMPEG and video processing in general.

    


    EDIT : I am adding the minimal reproducible code sample. However, it is really long because I need to include code for decoding, scaling and then encoding because the possible error is either in scaling or encoding :

    


    #include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>timestamp.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;&#xA;#include &#xA;#include &#xA;&#xA;typedef struct StreamingContext{&#xA;    AVFormatContext* avfc;&#xA;    AVCodec *video_avc;&#xA;    AVCodec *audio_avc;&#xA;    AVStream *video_avs;&#xA;    AVStream *audio_avs;&#xA;    AVCodecContext *video_avcc;&#xA;    AVCodecContext *audio_avcc;&#xA;    int video_index;&#xA;    int audio_index;&#xA;    char* filename;&#xA;    struct SwsContext *sws_ctx;&#xA;}StreamingContext;&#xA;&#xA;&#xA;typedef struct StreamingParams{&#xA;    char copy_video;&#xA;    char copy_audio;&#xA;    char *output_extension;&#xA;    char *muxer_opt_key;&#xA;    char *muxer_opt_value;&#xA;    char *video_codec;&#xA;    char *audio_codec;&#xA;    char *codec_priv_key;&#xA;    char *codec_priv_value;&#xA;}StreamingParams;&#xA;&#xA;void logging(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;int fill_stream_info(AVStream *avs, AVCodec **avc, AVCodecContext **avcc)&#xA;{&#xA;    *avc = avcodec_find_decoder(avs->codecpar->codec_id);&#xA;    if (!*avc)&#xA;    {&#xA;        logging("Failed to find the codec.\n");&#xA;        return -1;&#xA;    }&#xA;&#xA;    *avcc = avcodec_alloc_context3(*avc);&#xA;    if (!*avcc)&#xA;    {&#xA;        logging("Failed to alloc memory for codec context.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    if (avcodec_parameters_to_context(*avcc, avs->codecpar) &lt; 0)&#xA;    {&#xA;        logging("Failed to fill Codec Context.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    if (avcodec_open2(*avcc, *avc, NULL) &lt; 0)&#xA;    {&#xA;        logging("Failed to open Codec.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;int open_media(const char *in_filename, AVFormatContext **avfc)&#xA;{&#xA;    *avfc = avformat_alloc_context();&#xA;&#xA;    if (!*avfc)&#xA;    {&#xA;        logging("Failed to Allocate Memory for Format Context");&#xA;        return -1;&#xA;    }&#xA;&#xA;    if (avformat_open_input(avfc, in_filename, NULL, NULL) != 0)&#xA;    {&#xA;        logging("Failed to open input file %s", in_filename);&#xA;        return -1;&#xA;    }&#xA;&#xA;    if (avformat_find_stream_info(*avfc, NULL) &lt; 0)&#xA;    {&#xA;        logging("Failed to get Stream Info.");&#xA;        return -1;&#xA;    }&#xA;}&#xA;&#xA;int prepare_decoder(StreamingContext *sc)&#xA;{&#xA;    for (int i = 0; i &lt; sc->avfc->nb_streams; i&#x2B;&#x2B;)&#xA;    {&#xA;        if (sc->avfc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)&#xA;        {&#xA;            sc->video_avs = sc->avfc->streams[i];&#xA;            sc->video_index = i;&#xA;&#xA;            if (fill_stream_info(sc->video_avs, &amp;sc->video_avc, &amp;sc->video_avcc))&#xA;            {&#xA;                return -1;&#xA;            }&#xA;        }&#xA;        else&#xA;        {&#xA;            logging("Skipping Streams other than Video.");&#xA;        }&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;int prepare_video_encoder(StreamingContext *encoder_sc, AVCodecContext *decoder_ctx, AVRational input_framerate,&#xA;                          StreamingParams sp)&#xA;{&#xA;    encoder_sc->video_avs = avformat_new_stream(encoder_sc->avfc, NULL);&#xA;    encoder_sc->video_avc = avcodec_find_encoder_by_name(sp.video_codec);&#xA;    if (!encoder_sc->video_avc)&#xA;    {&#xA;        logging("Cannot find the Codec.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    encoder_sc->video_avcc = avcodec_alloc_context3(encoder_sc->video_avc);&#xA;    if (!encoder_sc->video_avcc)&#xA;    {&#xA;        logging("Could not allocate memory for Codec Context.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    av_opt_set(encoder_sc->video_avcc->priv_data, "preset", "fast", 0);&#xA;    if (sp.codec_priv_key &amp;&amp; sp.codec_priv_value)&#xA;        av_opt_set(encoder_sc->video_avcc->priv_data, sp.codec_priv_key, sp.codec_priv_value, 0);&#xA;&#xA;    encoder_sc->video_avcc->height = decoder_ctx->height;&#xA;    encoder_sc->video_avcc->width = decoder_ctx->width;&#xA;    encoder_sc->video_avcc->sample_aspect_ratio = decoder_ctx->sample_aspect_ratio;&#xA;&#xA;    if (encoder_sc->video_avc->pix_fmts)&#xA;        encoder_sc->video_avcc->pix_fmt = encoder_sc->video_avc->pix_fmts[0];&#xA;    else&#xA;        encoder_sc->video_avcc->pix_fmt = decoder_ctx->pix_fmt;&#xA;&#xA;    encoder_sc->video_avcc->bit_rate = 2 * 1000 * 1000;&#xA;    encoder_sc->video_avcc->rc_buffer_size = 4 * 1000 * 1000;&#xA;    encoder_sc->video_avcc->rc_max_rate = 2 * 1000 * 1000;&#xA;    encoder_sc->video_avcc->rc_min_rate = 2.5 * 1000 * 1000;&#xA;&#xA;    encoder_sc->video_avcc->time_base = av_inv_q(input_framerate);&#xA;    encoder_sc->video_avs->time_base = encoder_sc->video_avcc->time_base;&#xA;&#xA;    &#xA;&#xA;    if (avcodec_open2(encoder_sc->video_avcc, encoder_sc->video_avc, NULL) &lt; 0)&#xA;    {&#xA;        logging("Could not open the Codec.");&#xA;        return -1;&#xA;    }&#xA;    avcodec_parameters_from_context(encoder_sc->video_avs->codecpar, encoder_sc->video_avcc);&#xA;    return 0;&#xA;}&#xA;&#xA;int encode_video(StreamingContext *decoder, StreamingContext *encoder, AVFrame *input_frame)&#xA;{&#xA;    if (input_frame)&#xA;        input_frame->pict_type = AV_PICTURE_TYPE_NONE;&#xA;&#xA;    AVPacket *output_packet = av_packet_alloc();&#xA;    if (!output_packet)&#xA;    {&#xA;        logging("Could not allocate memory for Output Packet.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    int response = avcodec_send_frame(encoder->video_avcc, input_frame);&#xA;&#xA;    while (response >= 0)&#xA;    {&#xA;        response = avcodec_receive_packet(encoder->video_avcc, output_packet);&#xA;        if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)&#xA;        {&#xA;            break;&#xA;        }&#xA;        else if (response &lt; 0)&#xA;        {&#xA;            logging("Error while receiving packet from encoder: %s", av_err2str(response));&#xA;            return -1;&#xA;        }&#xA;&#xA;        output_packet->stream_index = decoder->video_index;&#xA;        output_packet->duration = encoder->video_avs->time_base.den / encoder->video_avs->time_base.num / decoder->video_avs->avg_frame_rate.num * decoder->video_avs->avg_frame_rate.den;&#xA;&#xA;        av_packet_rescale_ts(output_packet, decoder->video_avs->time_base, encoder->video_avs->time_base);&#xA;        response = av_interleaved_write_frame(encoder->avfc, output_packet);&#xA;        if (response != 0)&#xA;        {&#xA;            logging("Error %d while receiving packet from decoder: %s", response, av_err2str(response));&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;    av_packet_unref(output_packet);&#xA;    av_packet_free(&amp;output_packet);&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;int transcode_video(StreamingContext *decoder, StreamingContext *encoder, AVPacket *input_packet, AVFrame *input_frame, AVFrame *scaled_frame)&#xA;{&#xA;    int response = avcodec_send_packet(decoder->video_avcc, input_packet);&#xA;    if (response &lt; 0)&#xA;    {&#xA;        logging("Error while sending the Packet to Decoder: %s", av_err2str(response));&#xA;        return response;&#xA;    }&#xA;&#xA;    while (response >= 0)&#xA;    {&#xA;        response = avcodec_receive_frame(decoder->video_avcc, input_frame);&#xA;        &#xA;        if (response == AVERROR(EAGAIN) || response == AVERROR_EOF)&#xA;        {&#xA;            break;&#xA;        }&#xA;        else if (response &lt; 0)&#xA;        {&#xA;            logging("Error while receiving frame from Decoder: %s", av_err2str(response));&#xA;            return response;&#xA;        }&#xA;        if (response >= 0)&#xA;        {&#xA;            scaled_frame->pts = input_frame->pts;&#xA;            scaled_frame->pkt_dts = input_frame->pkt_dts;&#xA;            scaled_frame->pict_type = input_frame->pict_type;&#xA;            sws_scale_frame(encoder->sws_ctx, scaled_frame, input_frame);&#xA;            if (encode_video(decoder, encoder, scaled_frame))&#xA;                return -1;&#xA;        }&#xA;&#xA;        av_frame_unref(input_frame);&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;int main(int argc, char *argv[])&#xA;{&#xA;    StreamingParams sp = {0};&#xA;    sp.copy_audio = 1;&#xA;    sp.copy_video = 0;&#xA;    sp.video_codec = "libx265";&#xA;&#xA;&#xA;    StreamingContext *decoder = (StreamingContext *)calloc(1, sizeof(StreamingContext));&#xA;    decoder->filename = argv[1];&#xA;&#xA;    StreamingContext *encoder = (StreamingContext *)calloc(1, sizeof(StreamingContext));&#xA;    encoder->filename = argv[2];&#xA;&#xA;    if (sp.output_extension)&#xA;    {&#xA;        strcat(encoder->filename, sp.output_extension);&#xA;    }&#xA;&#xA;    if (open_media(decoder->filename, &amp;decoder->avfc))&#xA;        return -1;&#xA;    if (prepare_decoder(decoder))&#xA;        return -1;&#xA;&#xA;    avformat_alloc_output_context2(&amp;encoder->avfc, NULL, NULL, encoder->filename);&#xA;    if (!encoder->avfc)&#xA;    {&#xA;        logging("Could not allocate memory for output Format Context.");&#xA;        return -1;&#xA;    }&#xA;&#xA;        AVRational input_framerate = av_guess_frame_rate(decoder->avfc, decoder->video_avs, NULL);&#xA;        prepare_video_encoder(encoder, decoder->video_avcc, input_framerate, sp);&#xA;&#xA;&#xA;    if (encoder->avfc->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;        encoder->avfc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;&#xA;    if (!(encoder->avfc->oformat->flags &amp; AVFMT_NOFILE))&#xA;    {&#xA;        if (avio_open(&amp;encoder->avfc->pb, encoder->filename, AVIO_FLAG_WRITE) &lt; 0)&#xA;        {&#xA;            logging("could not open the output file");&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;    AVDictionary *muxer_opts = NULL;&#xA;&#xA;    if (sp.muxer_opt_key &amp;&amp; sp.muxer_opt_value)&#xA;    {&#xA;        av_dict_set(&amp;muxer_opts, sp.muxer_opt_key, sp.muxer_opt_value, 0);&#xA;    }&#xA;&#xA;    if (avformat_write_header(encoder->avfc, &amp;muxer_opts) &lt; 0)&#xA;    {&#xA;        logging("an error occurred when opening output file");&#xA;        return -1;&#xA;    }&#xA;&#xA;    AVFrame *input_frame = av_frame_alloc();&#xA;    AVFrame *scaled_frame = av_frame_alloc();&#xA;    if (!input_frame || !scaled_frame)&#xA;    {&#xA;        logging("Failed to allocate memory for AVFrame");&#xA;        return -1;&#xA;    }&#xA;&#xA;    // scaled_frame->format = AV_PIX_FMT_YUV420P;&#xA;    scaled_frame->width = 854;&#xA;    scaled_frame->height=480;    &#xA;&#xA;    //Creating Scaling Context&#xA;    encoder->sws_ctx = sws_getContext(1920, 1080,&#xA;                            decoder->video_avcc->pix_fmt, &#xA;                           scaled_frame->width, scaled_frame->height, decoder->video_avcc->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL );&#xA;    if (!encoder->sws_ctx){logging("Cannot Create Scaling Context."); return -1;}&#xA;&#xA;&#xA;    AVPacket *input_packet = av_packet_alloc();&#xA;    if (!input_packet)&#xA;    {&#xA;        logging("Failed to allocate memory for AVPacket.");&#xA;        return -1;&#xA;    }&#xA;&#xA;    while (av_read_frame(decoder->avfc, input_packet) >= 0)&#xA;    {&#xA;        if (decoder->avfc->streams[input_packet->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)&#xA;        {&#xA;                if (transcode_video(decoder, encoder, input_packet, input_frame, scaled_frame))&#xA;                    return -1;&#xA;                av_packet_unref(input_packet);&#xA;        }&#xA;        else&#xA;        {&#xA;            logging("Ignoring all nonvideo  packets.");&#xA;        }&#xA;    }&#xA;&#xA;    if (encode_video(decoder, encoder, NULL))&#xA;        return -1;&#xA;&#xA;    av_write_trailer(encoder->avfc);&#xA;&#xA;    if (muxer_opts != NULL)&#xA;    {&#xA;        av_dict_free(&amp;muxer_opts);&#xA;        muxer_opts = NULL;&#xA;    }&#xA;&#xA;    if (input_frame != NULL)&#xA;    {&#xA;        av_frame_free(&amp;input_frame);&#xA;        input_frame = NULL;&#xA;    }&#xA;&#xA;    if (input_packet != NULL)&#xA;    {&#xA;        av_packet_free(&amp;input_packet);&#xA;        input_packet = NULL;&#xA;    }&#xA;&#xA;    avformat_close_input(&amp;decoder->avfc);&#xA;&#xA;    avformat_free_context(decoder->avfc);&#xA;    decoder->avfc = NULL;&#xA;    avformat_free_context(encoder->avfc);&#xA;    encoder->avfc = NULL;&#xA;&#xA;    avcodec_free_context(&amp;decoder->video_avcc);&#xA;    decoder->video_avcc = NULL;&#xA;    avcodec_free_context(&amp;decoder->audio_avcc);&#xA;    decoder->audio_avcc = NULL;&#xA;&#xA;    free(decoder);&#xA;    decoder = NULL;&#xA;    free(encoder);&#xA;    encoder = NULL;&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;

    The video I am using for testing is available at the repo : https://github.com/leandromoreira/ffmpeg-libav-tutorial

    &#xA;

    The file name is small_bunny_1080p_60fps.mp4

    &#xA;

  • FFmpeg fails to draw text

    6 avril 2024, par Edoardo Balducci

    I've rarely used ffmpeg before, so, sorry If the question is too dumb.&#xA;I have a problem adding a text layer to a video frame using ffmpeg.

    &#xA;

    This is my current code :

    &#xA;

    import subprocess&#xA;from PyQt5.QtGui import QPixmap, QImage&#xA;from PyQt5.QtWidgets import QLabel&#xA;&#xA;class VideoThumbnailLabel(QLabel):&#xA;    def __init__(self, file_path, *args, **kwargs):&#xA;        super().__init__(*args, **kwargs)&#xA;        self.video = video&#xA;        video_duration = self.get_video_duration(file_path)&#xA;        thumbnail_path = self.get_thumbnail(file_path, video_duration)&#xA;        if thumbnail_path:&#xA;            self.setPixmap(QPixmap(thumbnail_path).scaled(160, 90, Qt.KeepAspectRatio))&#xA;        self.setToolTip(f"{video.title}\n{video.description}")&#xA;&#xA;    def get_video_duration(self, video_path):&#xA;        """Returns the duration of the video in seconds."""&#xA;        command = [&#xA;            &#x27;ffprobe&#x27;, &#x27;-v&#x27;, &#x27;error&#x27;, &#x27;-show_entries&#x27;,&#xA;            &#x27;format=duration&#x27;, &#x27;-of&#x27;,&#xA;            &#x27;default=noprint_wrappers=1:nokey=1&#x27;, video_path&#xA;        ]&#xA;        try:&#xA;            result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)&#xA;            if result.returncode != 0:&#xA;                print(f"ffprobe error: {result.stderr}")&#xA;                return 0&#xA;            duration = float(result.stdout)&#xA;            return int(duration)  # Returning duration as an integer for simplicity&#xA;        except Exception as e:&#xA;            print(f"Error getting video duration: {e}")&#xA;            return 0&#xA;&#xA;    def get_thumbnail(self, video_path, duration):&#xA;        """Generates a thumbnail with the video duration overlaid."""&#xA;        output_path = "thumbnail.jpg"  # Temporary thumbnail file&#xA;        duration_str = f"{duration // 3600:02d}:{(duration % 3600) // 60:02d}:{duration % 60:02d}"&#xA;        command = [&#xA;            &#x27;ffmpeg&#x27;, &#x27;-i&#x27;, video_path,&#xA;            &#x27;-ss&#x27;, &#x27;00:00:01&#x27;,  # Time to take the screenshot&#xA;            &#x27;-frames:v&#x27;, &#x27;1&#x27;,  # Number of frames to capture&#xA;            &#x27;-vf&#x27;, f"drawtext=text=&#x27;Duration: {duration_str}&#x27;:x=10:y=10:fontsize=24:fontcolor=white",&#xA;            &#x27;-q:v&#x27;, &#x27;2&#x27;,  # Output quality&#xA;            &#x27;-y&#x27;,  # Overwrite output files without asking&#xA;            output_path&#xA;        ]&#xA;        try:&#xA;            result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)&#xA;            if result.returncode != 0:&#xA;                print(f"ffmpeg error: {result.stderr}")&#xA;                return None&#xA;            return output_path&#xA;        except Exception as e:&#xA;            print(f"Error generating thumbnail with duration: {e}")&#xA;            return None&#xA;

    &#xA;

    and it is used like this :

    &#xA;

    for i, video in enumerate(self.videos):&#xA;    video_widget = VideoThumbnailLabel(video.file)&#xA;    video_widget.mousePressEvent = lambda event, v=video: self.onThumbnailClick(&#xA;        v&#xA;    )&#xA;    self.layout.addWidget(video_widget, i // 3, i % 3)&#xA;

    &#xA;

    I'm facing a problem where I am not able to get the thumbnail if I try to add the duration (I've tested it without the draw filter and worked fine)

    &#xA;

    I get this error (from the result.returncode) that I'm not able to comprehend :

    &#xA;

    ffmpeg error: b"ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers\n  built with Apple clang version 15.0.0 (clang-1500.1.0.2.5)\n  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.1.1_4 --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-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon\n  libavutil      58. 29.100 / 58. 29.100\n  libavcodec     60. 31.102 / 60. 31.102\n  libavformat    60. 16.100 / 60. 16.100\n  libavdevice    60.  3.100 / 60.  3.100\n  libavfilter     9. 12.100 /  9. 12.100\n  libswscale      7.  5.100 /  7.  5.100\n  libswresample   4. 12.100 /  4. 12.100\n  libpostproc    57.  3.100 / 57.  3.100\nInput #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;/Users/edoardo/Projects/work/test/BigBuckBunny.mp4&#x27;:\n  Metadata:\n    major_brand     : mp42\n    minor_version   : 0\n    compatible_brands: isomavc1mp42\n    creation_time   : 2010-01-10T08:29:06.000000Z\n  Duration: 00:09:56.47, start: 0.000000, bitrate: 2119 kb/s\n  Stream #0:0[0x1](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)\n    Metadata:\n      creation_time   : 2010-01-10T08:29:06.000000Z\n      handler_name    : (C) 2007 Google Inc. v08.13.2007.\n      vendor_id       : [0][0][0][0]\n  Stream #0:1[0x2](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1991 kb/s, 24 fps, 24 tbr, 24k tbn (default)\n    Metadata:\n      creation_time   : 2010-01-10T08:29:06.000000Z\n      handler_name    : (C) 2007 Google Inc. v08.13.2007.\n      vendor_id       : [0][0][0][0]\n[Parsed_drawtext_0 @ 0x60000331cd10] Both text and text file provided. Please provide only one\n[AVFilterGraph @ 0x600002018000] Error initializing filters\n[vost#0:0/mjpeg @ 0x13ce0c7e0] Error initializing a simple filtergraph\nError opening output file thumbnail.jpg.\nError opening output files: Invalid argument\n"&#xA;

    &#xA;

    I've installed both ffmpeg and ffmprobe in my machine :

    &#xA;

    ┌(edoardomacbook-air)-[~/Projects/work/tests-scripts]                                                                                                                                   &#xA;└─ $ ffmpeg -version &amp;&amp; ffprobe -version                                                                                                                                                              2 ⚙ &#xA;ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers&#xA;built with Apple clang version 15.0.0 (clang-1500.1.0.2.5)&#xA;configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.1.1_4 --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-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon&#xA;libavutil      58. 29.100 / 58. 29.100&#xA;libavcodec     60. 31.102 / 60. 31.102&#xA;libavformat    60. 16.100 / 60. 16.100&#xA;libavdevice    60.  3.100 / 60.  3.100&#xA;libavfilter     9. 12.100 /  9. 12.100&#xA;libswscale      7.  5.100 /  7.  5.100&#xA;libswresample   4. 12.100 /  4. 12.100&#xA;libpostproc    57.  3.100 / 57.  3.100&#xA;ffprobe version 6.1.1 Copyright (c) 2007-2023 the FFmpeg developers&#xA;built with Apple clang version 15.0.0 (clang-1500.1.0.2.5)&#xA;configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.1.1_4 --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-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon&#xA;libavutil      58. 29.100 / 58. 29.100&#xA;libavcodec     60. 31.102 / 60. 31.102&#xA;libavformat    60. 16.100 / 60. 16.100&#xA;libavdevice    60.  3.100 / 60.  3.100&#xA;libavfilter     9. 12.100 /  9. 12.100&#xA;libswscale      7.  5.100 /  7.  5.100&#xA;libswresample   4. 12.100 /  4. 12.100&#xA;libpostproc    57.  3.100 / 57.  3.100&#xA;

    &#xA;

    Does anyone see the problem ?

    &#xA;


    &#xA;

    P.S. : I know that I havent provided a minimal reproducible example, but since I don't know where the problem lies I didn't want to exclude anything

    &#xA;