Recherche avancée

Médias (0)

Mot : - Tags -/alertes

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

Autres articles (68)

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

  • MediaSPIP 0.1 Beta version

    25 avril 2011, par

    MediaSPIP 0.1 beta is the first version of MediaSPIP proclaimed as "usable".
    The zip file provided here only contains the sources of MediaSPIP in its standalone version.
    To get a working installation, you must manually install all-software dependencies on the server.
    If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...)

  • Amélioration de la version de base

    13 septembre 2013

    Jolie sélection multiple
    Le plugin Chosen permet d’améliorer l’ergonomie des champs de sélection multiple. Voir les deux images suivantes pour comparer.
    Il suffit pour cela d’activer le plugin Chosen (Configuration générale du site > Gestion des plugins), puis de configurer le plugin (Les squelettes > Chosen) en activant l’utilisation de Chosen dans le site public et en spécifiant les éléments de formulaires à améliorer, par exemple select[multiple] pour les listes à sélection multiple (...)

Sur d’autres sites (7578)

  • Can't decode h264 frame by frame

    17 mai 2017, par J. Doe

    Here is problematic code :

    int frame_count{0};
    int status = -1;
    while ((status = av_read_frame(ctx, pkt)) >= 0) {
       int got_frame;
       auto len =
           avcodec_decode_video2(video_ctx, frame, &got_frame, pkt);
       errcheck(len);

       if (got_frame == 0)
           errthrow("No frame could be decompressed");

       auto w = frame->width;
       auto h = frame->height;
       auto gray_convert_ctx = sws_getContext(
           w, h, input_pix_format, w, h, output_pix_format, SWS_POINT,
           nullptr, nullptr, nullptr);

       sws_scale(gray_convert_ctx, frame->data, frame->linesize, 0, h,
             frame_converted->data, frame_converted->linesize);

       f_(frame_converted->data[0], frame_converted->linesize[0], w,
          h);
       ++frame_count;
       sws_freeContext(gray_convert_ctx);

       if (pkt->data) {
           pkt->size -= len;
           pkt->data += len;
       }
    }
    if (status != AVERROR_EOF)
       errcheck(status);

    With vp8/vp9 all is okay, but when I’m trying to decode h264, I’ve got error :

    ➤ ./cv /tmp/x
    file size: 694KiB
    read /tmp/x: 694KiB
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] ISO: File Type Major Brand: mp42
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] rfps: 31.000000 0.000599
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] Before avformat_find_stream_info() pos: 3104 bytes read:32768 seeks:0
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] All info found
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x191fc00] After avformat_find_stream_info() pos: 456697 bytes read:458752 seeks:0 frames:34
    [h264 @ 0x1926700] no frame!
    error: AV: Invalid data found when processing input

    Maybe that’s because of h264 does not support AV_CODEC_CAP_TRUNCATED ? But I suppose it’s should be handled in av_read_frame.

    Then ignoring (just skipping this read if error occurs) — no frame decoded anyway until EOF. :c This video working fine with ffplay/mpv/etc. and was recorded by Android.

    #include <iostream>

    extern "C" {
    #include <libavcodec></libavcodec>avcodec.h>
    #include <libavformat></libavformat>avformat.h>
    }

    #include <functional>

    class Video
    {
         public:
       static std::string TAG;

       Video();
       Video(void *data_ptr, size_t data_size);
       ~Video();

       void set(void *data_ptr, size_t data_size);
       void process(std::function f_);

         private:
       static constexpr AVPixelFormat output_pix_format{AV_PIX_FMT_GRAY8};

       struct {
           uint8_t *ptr{nullptr};
           size_t size;
       } bd;

       bool video_ctx_opened{false};
       AVCodecContext *video_ctx{nullptr};
       AVStream *video_stream{nullptr};

       size_t width;
       size_t heigh;

       AVPixelFormat input_pix_format;

       size_t avio_ctx_buffer_size = 32 * 1024; // 32 KiB
       uint8_t *avio_ctx_buffer{nullptr};

       AVFormatContext *ctx{nullptr};
       AVIOContext *avio_ctx{nullptr};

       uint8_t *frame_converted_buffer{nullptr};
       AVFrame *frame_converted{nullptr};

       AVFrame *frame{nullptr};
       AVPacket *pkt{nullptr};

       void init_stream();
       void init_codec();
       void init_frame_converted();
    };


    extern "C" {
    #include <libswscale></libswscale>swscale.h>
    }

    #include <iostream>
    #include <stdexcept>

    namespace
    {
    using str_t = decltype(Video::TAG);

    static str_t averr(int code)
    {
       static thread_local std::array buf;
       av_make_error_string(buf.data(), buf.size(), code);
       return str_t(buf.data(), buf.size());
    }

    static str_t errstr(int err) { return Video::TAG + ": " + averr(err); }

    static str_t errstr(const char *err) { return Video::TAG + ": " + err; }

    static void errthrow(str_t err) { throw std::runtime_error{std::move(err)}; }

    static void errcheck(int val)
    {
       if (val &lt; 0)
           errthrow(errstr(val));
    }

    template <class t="t"> static void errcheck(T *ptr, const char *errmsg)
    {
       if (!ptr)
           errthrow(errstr(errmsg));
    }

    static int read_packet(void *opaque, uint8_t *buf, int buf_size)
    {
       struct _bd {
           uint8_t *ptr;
           size_t size;
       };
       _bd *bd = static_cast&lt;_bd *>(opaque);

       buf_size = FFMIN(buf_size, bd->size);

       memcpy(buf, bd->ptr, buf_size);
       bd->ptr += buf_size;
       bd->size -= buf_size;

       return buf_size;
    }
    }

    std::string Video::TAG = "AV";

    Video::Video()
    {
       av_register_all();
       avcodec_register_all();

       frame = av_frame_alloc();
       errcheck(frame, "Could not allocate frame");

       pkt = static_cast<avpacket>(av_malloc(sizeof(AVPacket)));
       errcheck(pkt, "Could not allocate packet");
       av_init_packet(pkt);
    }

    Video::Video(void *data_ptr, size_t data_size) : Video()
    {
       set(data_ptr, data_size);
    }

    Video::~Video()
    {
       avformat_close_input(&amp;ctx);
       if (avio_ctx) {
           av_freep(&amp;avio_ctx->buffer);
           av_freep(&amp;avio_ctx);
       }

       if (video_ctx) {
           avcodec_close(video_ctx);
           av_free(video_ctx);
       }
       if (frame)
           av_frame_free(&amp;frame);
       if (frame_converted_buffer)
           av_freep(&amp;frame_converted_buffer);
       if (frame_converted)
           av_frame_free(&amp;frame_converted);
       if (pkt) {
           av_free_packet(pkt);
           av_free(pkt);
       }
    }

    void Video::set(void *data_ptr, size_t data_size)
    {
       bd.ptr = static_cast(data_ptr);
       bd.size = data_size;

       init_stream();
       init_frame_converted();
       init_codec();
       pkt->data = nullptr;
       pkt->size = 0;
    }

    void Video::process(
       std::function f_)
    {
       int frame_count{0};
       int status = -1;
       while ((status = av_read_frame(ctx, pkt)) >= 0) {
           int got_frame;
           auto len =
               avcodec_decode_video2(video_ctx, frame, &amp;got_frame, pkt);
           errcheck(len);

           if (got_frame == 0)
               errthrow("No frame could be decompressed");

           auto w = frame->width;
           auto h = frame->height;
           auto gray_convert_ctx = sws_getContext(
               w, h, input_pix_format, w, h, output_pix_format, SWS_POINT,
               nullptr, nullptr, nullptr);

           sws_scale(gray_convert_ctx, frame->data, frame->linesize, 0, h,
                     frame_converted->data, frame_converted->linesize);

           f_(frame_converted->data[0], frame_converted->linesize[0], w,
              h);
           ++frame_count;
           sws_freeContext(gray_convert_ctx);

           if (pkt->data) {
               pkt->size -= len;
               pkt->data += len;
           }
       }
       if (status != AVERROR_EOF)
           errcheck(status);
    }

    void Video::init_stream()
    {
       ctx = avformat_alloc_context();
       errcheck(ctx, "Could not allocate format context");

       avio_ctx_buffer =
           static_cast(av_malloc(avio_ctx_buffer_size));
       errcheck(avio_ctx_buffer, "Could not allocate io buffer");

       avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0,
                                     &amp;bd, &amp;read_packet, nullptr, nullptr);
       errcheck(avio_ctx, "Could not allocate io context");
       ctx->pb = avio_ctx;

       auto status = avformat_open_input(&amp;ctx, nullptr, nullptr, nullptr);
       errcheck(status);

       status = avformat_find_stream_info(ctx, nullptr);
       errcheck(status);

       for (decltype(ctx->nb_streams) i = 0; i &lt; ctx->nb_streams; ++i) {
           auto stream = ctx->streams[i];
           if (!stream || !stream->codec)
               continue;
           if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
               video_stream = stream;
               break;
           }
       }
       errcheck(video_stream, "Could not find valid video stream");

       width = video_stream->codec->width;
       heigh = video_stream->codec->height;

       input_pix_format = video_stream->codec->pix_fmt;
    }

    void Video::init_codec()
    {
       auto codec = avcodec_find_decoder(video_stream->codec->codec_id);
       errcheck(codec, "Codec not found");

       video_ctx = avcodec_alloc_context3(codec);
       errcheck(video_ctx, "Could not allocate video codec context");

       if (codec->capabilities &amp; AV_CODEC_CAP_TRUNCATED)
           video_ctx->flags |=
               AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames

       auto status = avcodec_open2(video_ctx, codec, nullptr);
       errcheck(status);
    }

    void Video::init_frame_converted()
    {
       frame_converted = av_frame_alloc();
       errcheck(frame_converted, "Could not allocate frame");

       int frame_converted_buffer_size =
           avpicture_get_size(output_pix_format, width, heigh);
       errcheck(frame_converted_buffer_size);

       frame_converted_buffer =
           static_cast(av_malloc(frame_converted_buffer_size));
       errcheck(frame_converted_buffer, "Could not allocate picture buffer");

       auto status = avpicture_fill(
           reinterpret_cast<avpicture>(frame_converted),
           frame_converted_buffer, output_pix_format, width, heigh);
       errcheck(status);
    }

    #include <vector>
    #include <fstream>

    std::vector<char> read_file(const std::string &amp;fname)
    {
       std::ifstream file(fname, std::ios::binary | std::ios::ate);
       if (!file.is_open())
           throw std::runtime_error{"can't open " + fname};

       auto size = file.tellg();
       file.seekg(0, std::ios::beg);

       std::cout &lt;&lt; "file size: " &lt;&lt; std::to_string(size / 1024) &lt;&lt; "KiB\n";

       std::vector<char> buffer(size);

       if (file.read(buffer.data(), size))
           return buffer;
       return {};
    }

    int main(int argc, const char **argv)
    {
       if (argc &lt; 2)
           return EXIT_FAILURE;

       av_log_set_level(AV_LOG_DEBUG);

       try {
           auto data = read_file(argv[1]);
           std::cout &lt;&lt; "read " &lt;&lt; argv[1] &lt;&lt; ": "
                     &lt;&lt; std::to_string(data.size() / 1024) &lt;&lt; "KiB\n";

                   Video v;
               v.set(data.data(), data.size());

               v.process([](unsigned char *data, int wrap, int xsize,
                              int ysize) {
                   std::cout &lt;&lt; "w: " &lt;&lt; xsize
                             &lt;&lt; " h: " &lt;&lt; ysize &lt;&lt; '\n';
               });

       } catch (const std::runtime_error &amp;e) {
           std::cout &lt;&lt; "error: " &lt;&lt; e.what() &lt;&lt; '\n';
       }
    }
    </char></char></fstream></vector></avpicture></avpacket></class></stdexcept></iostream></functional></iostream>

    Compile&Run :
    g++ cv.cpp -std=c++14 -lavutil -lavcodec -lavformat -lswscale ; ./a.out file.name.here

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

  • ffmpeg forcing the usage of nvenc instead of libx264 c++

    3 octobre 2016, par tankyx

    The code below works, but it loads the nvenc encoder instead of the libx264 encoder, which I need for 0 latency streaming.

    this->pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
    if (this->pCodec == NULL)
       throw myExceptions("Error: Can't initialize the encoder. FfmpegEncoder.cpp l:9\n");

    this->pCodecCtx = avcodec_alloc_context3(this->pCodec);

    //Alloc output context
    if (avformat_alloc_output_context2(&amp;outFormatCtx, NULL, "rtsp", url) &lt; 0)
       throw myExceptions("Error: Can't alloc stream output. FfmpegEncoder.cpp l:17\n");

    How can I force the usage of x264 ?