Recherche avancée

Médias (91)

Autres articles (59)

  • Personnaliser les catégories

    21 juin 2013, par

    Formulaire de création d’une catégorie
    Pour ceux qui connaissent bien SPIP, une catégorie peut être assimilée à une rubrique.
    Dans le cas d’un document de type catégorie, les champs proposés par défaut sont : Texte
    On peut modifier ce formulaire dans la partie :
    Administration > Configuration des masques de formulaire.
    Dans le cas d’un document de type média, les champs non affichés par défaut sont : Descriptif rapide
    Par ailleurs, c’est dans cette partie configuration qu’on peut indiquer le (...)

  • 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

  • Keeping control of your media in your hands

    13 avril 2011, par

    The vocabulary used on this site and around MediaSPIP in general, aims to avoid reference to Web 2.0 and the companies that profit from media-sharing.
    While using MediaSPIP, you are invited to avoid using words like "Brand", "Cloud" and "Market".
    MediaSPIP is designed to facilitate the sharing of creative media online, while allowing authors to retain complete control of their work.
    MediaSPIP aims to be accessible to as many people as possible and development is based on expanding the (...)

Sur d’autres sites (12390)

  • How to compute the number of extra samples added by LAME or FFMPEG

    24 janvier 2018, par actuallyaswin

    I am attempting to build a MP3 decoder / parser in Python which supports files encoded by LAME or FFMPEG.

    My encoding shell script is shown here :

    #!/bin/bash
    for i in wav/*.wav; do
       i=${i##*/};
       lame --nores --strictly-enforce-ISO -t --cbr -b 64 -h "wav/${i}" "mpeg/lame/${i%.wav}.mp3";
       ffmpeg -i "wav/${i}" -codec:a libmp3lame -qscale:a 2 "mpeg/ffmpeg/${i%.wav}.mp3";
    done

    This scripts reads WAVE files located in ./wav/ and produces a controlled-bitrate MP3 of 64kbps in my ./mp3/lame/ directory, and a variable-bitrate MP3 of quality 2 in my ./mp3/ffmpeg/.

    I have written a Python script that iterates through both resultant MP3s, counting the number of frames and samples. Both the LAME and FFMPEG results are equivalent (in terms of frames and samples), but their binary files are different.

    The LAME/FFMPEG sample count was done by iterating through the binary MP3 files, locating and parsing the frame header, then using the MP3 spec to determine the number of samples per frame.

    • Number of MP3 data-frames : 112 (ignoring the Xing/Info first frame)
    • Number of output frames : 112*576 = 64512

    Here is a comparison of the sample count for a single 4-second input file :

    • Input WAV # of samples = 62996
    • Output LAME/FFMPEG # of samples = 64512
    • Difference = 1516

    I understand that according to the LAME FAQ file, resultant MP3 files are zero padded in the front and back to make sure the inverse MDCT is performed properly, but also because the windows overlap.

    What I can’t ascertain from the above FAQ, or from any previous StackOverflow post, is how to compute the number of artificially added samples. If I can be sure that all 1516 of these samples are zeros, and I can be sure of their position in the bytestream, I’d like to be able to confidently toss them out. Since there are 1516 "extra" samples and there are 576 samples per frame for a V2LIII encoding, then there must be more than two (but less than three) erroneous MPEG frames here.

    Is anyone here savvy enough with MPEG encoding/decoding to know how many samples are added, and in which frames those samples will be in ? In other words, will the first frame and last frame always contain blank data, or are there more frames ?

  • libavformat/libavcodec providing invalid container header

    14 décembre 2017, par seanr8

    I’m using libavcodec to encode a stream to h264 and libavformat to store it in an mp4. The resulting container has an invalid header that can be played in VLC, but not any other player.

    I’ve found that using the mp4 container and the "mpeg4" codec produces a valid mp4 file, but using libx265 (HEVC) or the libx264 codec produces invalid mp4s.

    I can use ffmpeg -i invalid.mp4 -vcodec copy valid.mp4 and I get a file of almost the exact same size, but in a valid container.

    Examples of these files are here : Broken file and
    Repaied file [use the download links in the upper right to examine]

    I used a hex editor to see the differences in the headers of the two files and the invalid one is 1 byte smaller than the valid one.

    The code I’m using to open the container and codec and to write the header is here :

    AVOutputFormat *container_format;
    AVFormatContext *container_format_context;
    AVStream *video_stream;
    int ret;

    /* allocate the output media context */
    avformat_alloc_output_context2(&container_format_context, NULL, NULL, out_file);
    if (!container_format_context) {
       log(INFO, "Unable to determine container format from filename, exiting\n");
       exit(1);
    }
    else {
       log(INFO, "Using container %s\n", container_format_context->oformat->name);
    }

    if (!container_format_context) {
       log(ERROR, "Could not build container format context. Encoding failed.");
       exit(1);
    }

    container_format = container_format_context->oformat;

    /* Pull codec based on name */
    AVCodec* codec = avcodec_find_encoder_by_name(codec_name);
    if (codec == NULL) {
       log(ERROR, "Failed to locate codec \"%s\".",
               codec_name);
       exit(1);
    }

    /* create stream */
    video_stream = NULL;
    video_stream = avformat_new_stream(container_format_context, codec);
    if (!video_stream) {
       log(ERROR, "Could not allocate encoder stream. Cannot continue.\n");
       exit(1);
    }
    video_stream->id = container_format_context->nb_streams - 1;

    video_stream->time_base = video_stream->codec->time_base = (AVRational) { 1, 25};

    av_dump_format(container_format_context, 0, out_file, 1);



    /* Retrieve encoding context */
    AVCodecContext* avcodec_context = video_stream->codec;
    if (avcodec_context == NULL) {
       log(ERROR, "Failed to allocate context for "
               "codec \"%s\".", codec_name);
       exit(1);
    }


    /* Init context with encoding parameters */
    avcodec_context->bit_rate = bitrate;
    avcodec_context->width = width;
    avcodec_context->height = height;
    avcodec_context->gop_size = 10;
    avcodec_context->max_b_frames = 1;
    avcodec_context->qmax = 31;
    avcodec_context->qmin = 2;
    avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P;

    av_dump_format(container_format_context, 0, out_file, 1);

    /* Open codec for use */
    if (avcodec_open2(avcodec_context, codec, NULL) < 0) {
       log(ERROR, "Failed to open codec \"%s\".", codec_name);
       exit(1);
    }

    /* Allocate corresponding frame */
    AVFrame* frame = av_frame_alloc();
    if (frame == NULL) {
       exit(1);
    }

    /* Copy necessary data for frame from avcodec_context */
    frame->format = avcodec_context->pix_fmt;
    frame->width = avcodec_context->width;
    frame->height = avcodec_context->height;

    /* Allocate actual backing data for frame */
    if (av_image_alloc(frame->data, frame->linesize, frame->width,
               frame->height, frame->format, 32) < 0) {
       exit(1);
    }

    /* open the output file, if the container needs it */
    if (!(container_format->flags & AVFMT_NOFILE)) {
       ret = avio_open(&container_format_context->pb, out_file, AVIO_FLAG_WRITE);
       if (ret < 0) {
           log(ERROR, "Error occurred while opening output file: %s\n",
                   av_err2str(ret));
           exit(1);
       }
    }

    /* write the stream header, if needed */
    ret = avformat_write_header(container_format_context, NULL);
    if (ret < 0) {
       log(ERROR, "Error occurred while writing output file header: %s\n",
                   av_err2str(ret));
    }

    The code to encode a frame is here :

    /* Init video packet */
    AVPacket packet;
    av_init_packet(&packet);

    /* Request that encoder allocate data for packet */
    packet.data = NULL;
    packet.size = 0;

    /* Write frame to video */
    int got_data;
    if (avcodec_encode_video2(avcontext, &packet, frame, &got_data) < 0) {
       log(WARNING, "Error encoding frame #%" PRId64,
               video_struct->next_pts);
       return -1;
    }

    /* Write corresponding data to file */
    if (got_data) {
       if (packet.pts != AV_NOPTS_VALUE) {
           packet.pts = av_rescale_q(packet.pts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
       }
       if (packet.dts != AV_NOPTS_VALUE) {
           packet.dts = av_rescale_q(packet.dts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
       }
       write_packet(video_struct, &packet, packet.size);
       av_packet_unref(&packet);
    }

    And the code to write the packet to the video stream :

    static int write_packet(video_struct* video, void* data, int size) {

    int ret;

    /* use AVStream is not null, otherwise write to output fd */
    AVPacket *pkt = (AVPacket*) data;
    pkt->stream_index = video->output_stream->index;
    ret = av_interleaved_write_frame(video->container_format_context, pkt);
    if (ret != 0) {
       return -1;
    }

    /* Data was written successfully */
    return ret;
    }
  • libavformat/libavcodec providing invalid container header

    14 décembre 2017, par seanr8

    I’m using libavcodec to encode a stream to h264 and libavformat to store it in an mp4. The resulting container has an invalid header that can be played in VLC, but not any other player.

    I’ve found that using the mp4 container and the "mpeg4" codec produces a valid mp4 file, but using libx265 (HEVC) or the libx264 codec produces invalid mp4s.

    I can use ffmpeg -i invalid.mp4 -vcodec copy valid.mp4 and I get a file of almost the exact same size, but in a valid container.

    Examples of these files are here : Broken file and
    Repaied file [use the download links in the upper right to examine]

    I used a hex editor to see the differences in the headers of the two files and the invalid one is 1 byte smaller than the valid one.

    The code I’m using to open the container and codec and to write the header is here :

    AVOutputFormat *container_format;
    AVFormatContext *container_format_context;
    AVStream *video_stream;
    int ret;

    /* allocate the output media context */
    avformat_alloc_output_context2(&container_format_context, NULL, NULL, out_file);
    if (!container_format_context) {
       log(INFO, "Unable to determine container format from filename, exiting\n");
       exit(1);
    }
    else {
       log(INFO, "Using container %s\n", container_format_context->oformat->name);
    }

    if (!container_format_context) {
       log(ERROR, "Could not build container format context. Encoding failed.");
       exit(1);
    }

    container_format = container_format_context->oformat;

    /* Pull codec based on name */
    AVCodec* codec = avcodec_find_encoder_by_name(codec_name);
    if (codec == NULL) {
       log(ERROR, "Failed to locate codec \"%s\".",
               codec_name);
       exit(1);
    }

    /* create stream */
    video_stream = NULL;
    video_stream = avformat_new_stream(container_format_context, codec);
    if (!video_stream) {
       log(ERROR, "Could not allocate encoder stream. Cannot continue.\n");
       exit(1);
    }
    video_stream->id = container_format_context->nb_streams - 1;

    video_stream->time_base = video_stream->codec->time_base = (AVRational) { 1, 25};

    av_dump_format(container_format_context, 0, out_file, 1);



    /* Retrieve encoding context */
    AVCodecContext* avcodec_context = video_stream->codec;
    if (avcodec_context == NULL) {
       log(ERROR, "Failed to allocate context for "
               "codec \"%s\".", codec_name);
       exit(1);
    }


    /* Init context with encoding parameters */
    avcodec_context->bit_rate = bitrate;
    avcodec_context->width = width;
    avcodec_context->height = height;
    avcodec_context->gop_size = 10;
    avcodec_context->max_b_frames = 1;
    avcodec_context->qmax = 31;
    avcodec_context->qmin = 2;
    avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P;

    av_dump_format(container_format_context, 0, out_file, 1);

    /* Open codec for use */
    if (avcodec_open2(avcodec_context, codec, NULL) < 0) {
       log(ERROR, "Failed to open codec \"%s\".", codec_name);
       exit(1);
    }

    /* Allocate corresponding frame */
    AVFrame* frame = av_frame_alloc();
    if (frame == NULL) {
       exit(1);
    }

    /* Copy necessary data for frame from avcodec_context */
    frame->format = avcodec_context->pix_fmt;
    frame->width = avcodec_context->width;
    frame->height = avcodec_context->height;

    /* Allocate actual backing data for frame */
    if (av_image_alloc(frame->data, frame->linesize, frame->width,
               frame->height, frame->format, 32) < 0) {
       exit(1);
    }

    /* open the output file, if the container needs it */
    if (!(container_format->flags & AVFMT_NOFILE)) {
       ret = avio_open(&container_format_context->pb, out_file, AVIO_FLAG_WRITE);
       if (ret < 0) {
           log(ERROR, "Error occurred while opening output file: %s\n",
                   av_err2str(ret));
           exit(1);
       }
    }

    /* write the stream header, if needed */
    ret = avformat_write_header(container_format_context, NULL);
    if (ret < 0) {
       log(ERROR, "Error occurred while writing output file header: %s\n",
                   av_err2str(ret));
    }

    The code to encode a frame is here :

    /* Init video packet */
    AVPacket packet;
    av_init_packet(&packet);

    /* Request that encoder allocate data for packet */
    packet.data = NULL;
    packet.size = 0;

    /* Write frame to video */
    int got_data;
    if (avcodec_encode_video2(avcontext, &packet, frame, &got_data) < 0) {
       log(WARNING, "Error encoding frame #%" PRId64,
               video_struct->next_pts);
       return -1;
    }

    /* Write corresponding data to file */
    if (got_data) {
       if (packet.pts != AV_NOPTS_VALUE) {
           packet.pts = av_rescale_q(packet.pts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
       }
       if (packet.dts != AV_NOPTS_VALUE) {
           packet.dts = av_rescale_q(packet.dts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
       }
       write_packet(video_struct, &packet, packet.size);
       av_packet_unref(&packet);
    }

    And the code to write the packet to the video stream :

    static int write_packet(video_struct* video, void* data, int size) {

    int ret;

    /* use AVStream is not null, otherwise write to output fd */
    AVPacket *pkt = (AVPacket*) data;
    pkt->stream_index = video->output_stream->index;
    ret = av_interleaved_write_frame(video->container_format_context, pkt);
    if (ret != 0) {
       return -1;
    }

    /* Data was written successfully */
    return ret;
    }