Recherche avancée

Médias (0)

Mot : - Tags -/serveur

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

Autres articles (79)

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

  • Publier sur MédiaSpip

    13 juin 2013

    Puis-je poster des contenus à partir d’une tablette Ipad ?
    Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir

Sur d’autres sites (7089)

  • Decoding and resampling audio with FFmpeg for output with libao

    13 mai 2020, par DoctorSelar

    I'm trying to write a program to read and play an audio file using FFmpeg and libao. I've been following the procedure outlined in the FFmpeg documentation for decoding audio using the new avcodec_send_packet and avcodec_receive_frame functions, but the examples I've been able to find are few and far between (the ones in the FFmpeg documentation either don't use libavformat or use the deprecated avcodec_decode_audio4). I've based a lot of my program off of the transcode_aac.c example (up to init_resampler) in the FFmpeg documentation, but that also uses the deprecated decoding function.

    



    I believe I have the decoding part of the program working, but I need to resample the audio in order to convert it into an interleaved format to send to libao, for which I'm attempting to use libswresample. Whenever the program is run in its current state, it outputs (many times) "Error resampling : Output changed". The test file I've been using is just a YouTube rip that I had on hand. ffprobe reports the only stream as :

    



    Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)


    



    This is my first program with FFmpeg (and I'm still relatively new to C), so any advice on how to improve/fix other parts of the program would be welcome.

    



    #include&#xA;#include<libavcodec></libavcodec>avcodec.h>&#xA;#include<libavformat></libavformat>avformat.h>&#xA;#include<libavutil></libavutil>avutil.h>&#xA;#include<libswresample></libswresample>swresample.h>&#xA;#include<ao></ao>ao.h>&#xA;&#xA;#define OUTPUT_CHANNELS 2&#xA;#define OUTPUT_RATE 44100&#xA;#define BUFFER_SIZE 192000&#xA;#define OUTPUT_BITS 16&#xA;#define OUTPUT_FMT AV_SAMPLE_FMT_S16&#xA;&#xA;static char *errtext (int err) {&#xA;    static char errbuff[256];&#xA;    av_strerror(err,errbuff,sizeof(errbuff));&#xA;    return errbuff;&#xA;}&#xA;&#xA;static int open_audio_file (const char *filename, AVFormatContext **context, AVCodecContext **codec_context) {&#xA;    AVCodecContext *avctx;&#xA;    AVCodec *codec;&#xA;    int ret;&#xA;    int stream_id;&#xA;    int i;&#xA;&#xA;    // Open input file&#xA;    if ((ret = avformat_open_input(context,filename,NULL,NULL)) &lt; 0) {&#xA;        fprintf(stderr,"Error opening input file &#x27;%s&#x27;: %s\n",filename,errtext(ret));&#xA;        *context = NULL;&#xA;        return ret;&#xA;    }&#xA;&#xA;    // Get stream info&#xA;    if ((ret = avformat_find_stream_info(*context,NULL)) &lt; 0) {&#xA;        fprintf(stderr,"Unable to find stream info: %s\n",errtext(ret));&#xA;        avformat_close_input(context);&#xA;        return ret;&#xA;    }&#xA;&#xA;    // Find the best stream&#xA;    if ((stream_id = av_find_best_stream(*context,AVMEDIA_TYPE_AUDIO,-1,-1,&amp;codec,0)) &lt; 0) {&#xA;        fprintf(stderr,"Unable to find valid audio stream: %s\n",errtext(stream_id));&#xA;        avformat_close_input(context);&#xA;        return stream_id;&#xA;    }&#xA;&#xA;    // Allocate a decoding context&#xA;    if (!(avctx = avcodec_alloc_context3(codec))) {&#xA;        fprintf(stderr,"Unable to allocate decoder context\n");&#xA;        avformat_close_input(context);&#xA;        return AVERROR(ENOMEM);&#xA;    }&#xA;&#xA;    // Initialize stream parameters&#xA;    if ((ret = avcodec_parameters_to_context(avctx,(*context)->streams[stream_id]->codecpar)) &lt; 0) {&#xA;        fprintf(stderr,"Unable to get stream parameters: %s\n",errtext(ret));&#xA;        avformat_close_input(context);&#xA;        avcodec_free_context(&amp;avctx);&#xA;        return ret;&#xA;    }&#xA;&#xA;    // Open the decoder&#xA;    if ((ret = avcodec_open2(avctx,codec,NULL)) &lt; 0) {&#xA;        fprintf(stderr,"Could not open codec: %s\n",errtext(ret));&#xA;        avformat_close_input(context);&#xA;        avcodec_free_context(&amp;avctx);&#xA;        return ret;&#xA;    }&#xA;&#xA;    *codec_context = avctx;&#xA;    return 0;&#xA;}&#xA;&#xA;static void init_packet (AVPacket *packet) {&#xA;    av_init_packet(packet);&#xA;    packet->data = NULL;&#xA;    packet->size = 0;&#xA;}&#xA;&#xA;static int init_resampler (AVCodecContext *codec_context, SwrContext **resample_context) {&#xA;    int ret;&#xA;&#xA;    // Set resampler options&#xA;    *resample_context = swr_alloc_set_opts(NULL,&#xA;                                           av_get_default_channel_layout(OUTPUT_CHANNELS),&#xA;                                           OUTPUT_FMT,&#xA;                                           codec_context->sample_rate,&#xA;                                           av_get_default_channel_layout(codec_context->channels),&#xA;                                           codec_context->sample_fmt,&#xA;                                           codec_context->sample_rate,&#xA;                                           0,NULL);&#xA;    if (!(*resample_context)) {&#xA;        fprintf(stderr,"Unable to allocate resampler context\n");&#xA;        return AVERROR(ENOMEM);&#xA;    }&#xA;&#xA;    // Open the resampler&#xA;    if ((ret = swr_init(*resample_context)) &lt; 0) {&#xA;        fprintf(stderr,"Unable to open resampler context: %s\n",errtext(ret));&#xA;        swr_free(resample_context);&#xA;        return ret;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;static int init_frame (AVFrame **frame) {&#xA;    if (!(*frame = av_frame_alloc())) {&#xA;        fprintf(stderr,"Could not allocate frame\n");&#xA;        return AVERROR(ENOMEM);&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;int main (int argc, char *argv[]) {&#xA;    AVFormatContext *context = 0;&#xA;    AVCodecContext *codec_context;&#xA;    SwrContext *resample_context = NULL;&#xA;    AVPacket packet;&#xA;    AVFrame *frame = 0;&#xA;    AVFrame *resampled = 0;&#xA;    int16_t *buffer;&#xA;    int ret, packet_ret, finished;&#xA;&#xA;    ao_device *device;&#xA;    ao_sample_format format;&#xA;    int default_driver;&#xA;&#xA;    if (argc != 2) {&#xA;        fprintf(stderr,"Usage: %s <filename>\n",argv[0]);&#xA;        return 1;&#xA;    }&#xA;&#xA;    av_register_all();&#xA;    printf("Opening file...\n");&#xA;    if (open_audio_file(argv[1],&amp;context,&amp;codec_context) &lt; 0)&#xA;        return 1;&#xA;&#xA;    printf("Initializing resampler...\n");&#xA;    if (init_resampler(codec_context,&amp;resample_context) &lt; 0) {&#xA;        avformat_close_input(&amp;context);&#xA;        avcodec_free_context(&amp;codec_context);&#xA;        return 1;&#xA;    }&#xA;&#xA;    // Setup libao&#xA;    printf("Starting audio device...\n");&#xA;    ao_initialize();&#xA;    default_driver = ao_default_driver_id();&#xA;    format.bits = OUTPUT_BITS;&#xA;    format.channels = OUTPUT_CHANNELS;&#xA;    format.rate = codec_context->sample_rate;&#xA;    format.byte_format = AO_FMT_NATIVE;&#xA;    format.matrix = 0;&#xA;    if ((device = ao_open_live(default_driver,&amp;format,NULL)) == NULL) {&#xA;        fprintf(stderr,"Error opening audio device\n");&#xA;        avformat_close_input(&amp;context);&#xA;        avcodec_free_context(&amp;codec_context);&#xA;        swr_free(&amp;resample_context);&#xA;        return 1;&#xA;    }&#xA;&#xA;    // Mainloop&#xA;    printf("Beginning mainloop...\n");&#xA;    init_packet(&amp;packet);&#xA;    // Read packets until done&#xA;    while (1) {&#xA;        packet_ret = av_read_frame(context,&amp;packet);&#xA;        // Send a packet&#xA;        if ((ret = avcodec_send_packet(codec_context,&amp;packet)) &lt; 0)&#xA;            fprintf(stderr,"Error sending packet to decoder: %s\n",errtext(ret));&#xA;&#xA;        av_packet_unref(&amp;packet);&#xA;&#xA;        while (1) {&#xA;            if (!frame)&#xA;                frame = av_frame_alloc();&#xA;&#xA;            ret = avcodec_receive_frame(codec_context,frame);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) // Need more input&#xA;                break;&#xA;            else if (ret &lt; 0) {&#xA;                fprintf(stderr,"Error receiving frame: %s\n",errtext(ret));&#xA;                break;&#xA;            }&#xA;            // We have a valid frame, need to resample it&#xA;            if (!resampled)&#xA;                resampled = av_frame_alloc();&#xA;&#xA;            resampled->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);&#xA;            resampled->sample_rate = codec_context->sample_rate;&#xA;            resampled->format = OUTPUT_FMT;&#xA;&#xA;            if ((ret = swr_convert_frame(resample_context,resampled,frame)) &lt; 0) {&#xA;                fprintf(stderr,"Error resampling: %s\n",errtext(ret));&#xA;            } else {&#xA;                ao_play(device,(char*)resampled->extended_data[0],resampled->linesize[0]);&#xA;            }&#xA;            av_frame_unref(resampled);&#xA;            av_frame_unref(frame);&#xA;        }&#xA;&#xA;        if (packet_ret == AVERROR_EOF)&#xA;            break;&#xA;    }&#xA;&#xA;    printf("Closing file and freeing contexts...\n");&#xA;    avformat_close_input(&amp;context);&#xA;    avcodec_free_context(&amp;codec_context);&#xA;    swr_free(&amp;resample_context);&#xA;&#xA;    printf("Closing audio device...\n");&#xA;    ao_close(device);&#xA;    ao_shutdown();&#xA;&#xA;    return 0;&#xA;}&#xA;</filename>

    &#xA;&#xA;

    UPDATE : I've got it playing sound now, but it sounds like samples are missing (and MP3 files warn that "Could not update timestamps for skipped samples"). The issue was that the resampled frame needed to have certain attributes set before being passed to swr_convert_frame. I've also added av_packet_unref and av_frame_unref, but I'm still unsure as to where to best locate them.

    &#xA;

  • FFMPEG RTSP stream to MPEG4/H264 file using libx264

    16 octobre 2020, par Phi

    Heyo folks,

    &#xA;&#xA;

    I'm attempting to transcode/remux an RTSP stream in H264 format into a MPEG4 container, containing just the H264 video stream. Basically, webcam output into a MP4 container.

    &#xA;&#xA;

    I can get a poorly coded MP4 produced, using this code :

    &#xA;&#xA;

    // Variables here for demo&#xA;AVFormatContext * video_file_output_format = nullptr;&#xA;AVFormatContext * rtsp_format_context = nullptr;&#xA;AVCodecContext * video_file_codec_context = nullptr;&#xA;AVCodecContext * rtsp_vidstream_codec_context = nullptr;&#xA;AVPacket packet = {0};&#xA;AVStream * video_file_stream = nullptr;&#xA;AVCodec * rtsp_decoder_codec = nullptr;&#xA;int errorNum = 0, video_stream_index = 0;&#xA;std::string outputMP4file = "D:\\somemp4file.mp4";&#xA;&#xA;// begin&#xA;AVDictionary * opts = nullptr;&#xA;av_dict_set(&amp;opts, "rtsp_transport", "tcp", 0);&#xA;&#xA;if ((errorNum = avformat_open_input(&amp;rtsp_format_context, uriANSI.c_str(), NULL, &amp;opts)) &lt; 0) {&#xA;    errOut &lt;&lt; "Connection failed: avformat_open_input failed with error " &lt;&lt; errorNum &lt;&lt; ":\r\n" &lt;&lt; ErrorRead(errorNum);&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;&#xA;rtsp_format_context->max_analyze_duration = 50000;&#xA;if ((errorNum = avformat_find_stream_info(rtsp_format_context, NULL)) &lt; 0) {&#xA;    errOut &lt;&lt; "Connection failed: avformat_find_stream_info failed with error " &lt;&lt; errorNum &lt;&lt; ":\r\n" &lt;&lt; ErrorRead(errorNum);&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;&#xA;video_stream_index = errorNum = av_find_best_stream(rtsp_format_context, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);&#xA;&#xA;if (video_stream_index &lt; 0) {&#xA;    errOut &lt;&lt; "Connection in unexpected state; made a connection, but there was no video stream.\r\n"&#xA;        "Attempts to find a video stream resulted in error " &lt;&lt; errorNum &lt;&lt; ": " &lt;&lt; ErrorRead(errorNum);&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;&#xA;rtsp_vidstream_codec_context = rtsp_format_context->streams[video_stream_index]->codec;&#xA;&#xA;av_init_packet(&amp;packet);&#xA;&#xA;if (!(video_file_output_format = av_guess_format(NULL, outputMP4file.c_str(),  NULL))) {&#xA;    TacticalAbort();&#xA;    throw std::exception("av_guess_format");&#xA;}&#xA;&#xA;if (!(rtsp_decoder_codec = avcodec_find_decoder(rtsp_vidstream_codec_context->codec_id))) {&#xA;    errOut &lt;&lt; "Connection failed: connected, but avcodec_find_decoder returned null.\r\n"&#xA;        "Couldn&#x27;t find codec with an AV_CODEC_ID value of " &lt;&lt; rtsp_vidstream_codec_context->codec_id &lt;&lt; ".";&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;&#xA;video_file_format_context = avformat_alloc_context();&#xA;video_file_format_context->oformat = video_file_output_format;&#xA;&#xA;if (strcpy_s(video_file_format_context->filename, sizeof(video_file_format_context->filename), outputMP4file.c_str())) {&#xA;    errOut &lt;&lt; "Couldn&#x27;t open video file: strcpy_s failed with error " &lt;&lt; errno &lt;&lt; ".";&#xA;    std::string log = errOut.str();&#xA;    TacticalAbort();&#xA;    throw std::exception("strcpy_s");&#xA;}&#xA;&#xA;if (!(video_file_encoder_codec = avcodec_find_encoder(video_file_output_format->video_codec))) {&#xA;    TacticalAbort();&#xA;    throw std::exception("avcodec_find_encoder");&#xA;}&#xA;&#xA;// MARKER ONE&#xA;&#xA;if (!outputMP4file.empty() &amp;&amp;&#xA;    !(video_file_output_format->flags &amp; AVFMT_NOFILE) &amp;&amp;&#xA;    (errorNum = avio_open2(&amp;video_file_format_context->pb, outputMP4file.c_str(), AVIO_FLAG_WRITE, nullptr, &amp;opts)) &lt; 0) {&#xA;    errOut &lt;&lt; "Couldn&#x27;t open video file \"" &lt;&lt; outputMP4file &lt;&lt; "\" for writing : avio_open2 failed with error " &lt;&lt; errorNum &lt;&lt; ": " &lt;&lt; ErrorRead(errorNum);&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;&#xA;// Create stream in MP4 file&#xA;if (!(video_file_stream = avformat_new_stream(video_file_format_context, video_file_encoder_codec))) {&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;&#xA;AVCodecContext * video_file_codec_context = video_file_stream->codec;&#xA;&#xA;// MARKER TWO&#xA;&#xA;// error -22/-21 in avio_open2 if this is skipped&#xA;if ((errorNum = avcodec_copy_context(video_file_codec_context, rtsp_vidstream_codec_context)) != 0) {&#xA;    TacticalAbort();&#xA;    throw std::exception("avcodec_copy_context");&#xA;}&#xA;&#xA;//video_file_codec_context->codec_tag = 0;&#xA;&#xA;/*&#xA;// MARKER 3 - is this not needed? Examples suggest not.&#xA;if ((errorNum = avcodec_open2(video_file_codec_context, video_file_encoder_codec, &amp;opts)) &lt; 0)&#xA;{&#xA;    errOut &lt;&lt; "Couldn&#x27;t open video file codec context: avcodec_open2 failed with error " &lt;&lt; errorNum &lt;&lt; ": " &lt;&lt; ErrorRead(errorNum);&#xA;    std::string log = errOut.str();&#xA;    TacticalAbort();&#xA;    throw std::exception("avcodec_open2, video file");&#xA;}*/&#xA;&#xA;//video_file_format_context->flags |= AVFMT_FLAG_GENPTS;&#xA;if (video_file_format_context->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;{&#xA;    video_file_codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;&#xA;}&#xA;&#xA;if ((errorNum = avformat_write_header(video_file_format_context, &amp;opts)) &lt; 0) {&#xA;    errOut &lt;&lt; "Couldn&#x27;t open video file: avformat_write_header failed with error " &lt;&lt; errorNum &lt;&lt; ":\r\n" &lt;&lt; ErrorRead(errorNum);&#xA;    std::string log = errOut.str();&#xA;    TacticalAbort();&#xA;    return;&#xA;}&#xA;

    &#xA;&#xA;

    However, there are several issues :

    &#xA;&#xA;

      &#xA;
    1. I can't pass any x264 options to the output file. The output H264 matches the input H264's profile/level - switching cameras to a different model switches H264 level.
    2. &#xA;

    3. The timing of the output file is off, noticeably.
    4. &#xA;

    5. The duration of the output file is off, massively. A few seconds of footage becomes hours, although playtime doesn't match. (FWIW, I'm using VLC to play them.)
    6. &#xA;

    &#xA;&#xA;

    Passing x264 options

    &#xA;&#xA;

    If I manually increment PTS per packet, and set DTS equal to PTS, it plays too fast, 2-3 seconds' worth of footage in one second playtime, and duration is hours long. The footage also blurs past several seconds, about 10 seconds' footage in a second.

    &#xA;&#xA;

    If I let FFMPEG decide (with or without GENPTS flag), the file has a variable frame rate (probably as expected), but it plays the whole file in an instant and has a long duration too (over forty hours for a few seconds). The duration isn't "real", as the file plays in an instant.

    &#xA;&#xA;

    At Marker One, I try to set the profile by passing options to avio_open2. The options are simply ignored by libx264. I've tried :

    &#xA;&#xA;

    av_dict_set(&amp;opts, "vprofile", "main", 0);&#xA;av_dict_set(&amp;opts, "profile", "main", 0); // error, missing &#x27;(&#x27;&#xA;// FF_PROFILE_H264_MAIN equals 77, so I also tried&#xA;av_dict_set(&amp;opts, "vprofile", "77", 0); &#xA;av_dict_set(&amp;opts, "profile", "77", 0);&#xA;

    &#xA;&#xA;

    It does seem to read the profile setting, but it doesn't use them. At Marker Two, I tried to set it after the avio_open2, before avformat_write_header .

    &#xA;&#xA;

    // I tried all 4 av_dict_set from earlier, passing it to avformat_write_header.&#xA;// None had any effect, they weren&#x27;t consumed.&#xA;av_opt_set(video_file_codec_context, "profile", "77", 0);&#xA;av_opt_set(video_file_codec_context, "profile", "main", 0);&#xA;video_file_codec_context->profile = FF_PROFILE_H264_MAIN;&#xA;av_opt_set(video_file_codec_context->priv_data, "profile", "77", 0);&#xA;av_opt_set(video_file_codec_context->priv_data, "profile", "main", 0);&#xA;

    &#xA;&#xA;

    Messing with privdata made the program unstable, but I was trying anything at that point.&#xA;I'd like to solve issue 1 with passing settings, since I imagine it'd bottleneck any attempt to solve issues 2 or 3.

    &#xA;&#xA;

    I've been fiddling with this for the better part of a month now. I've been through dozens of documentation, Q&As, examples. It doesn't help that quite a few are outdated.

    &#xA;&#xA;

    Any help would be appreciated.

    &#xA;&#xA;

    Cheers

    &#xA;

  • dca_parser : Extend DTS core sync word and fix existing check

    29 avril 2015, par foo86
    dca_parser : Extend DTS core sync word and fix existing check
    

    The previous version checked for 14-bit streams and did not properly
    work across buffer boundaries.

    Use the 64-bit parser state to make extended sync word detection work
    across buffer boundary and check the extended sync word for 16-bit LE
    and BE core streams to reduce probability of alias sync detection.

    Signed-off-by : Michael Niedermayer <michaelni@gmx.at>
    Signed-off-by : Luca Barbato <lu_zero@gentoo.org>

    • [DBH] libavcodec/dca_parser.c