
Recherche avancée
Médias (1)
-
Géodiversité
9 septembre 2011, par ,
Mis à jour : Août 2018
Langue : français
Type : Texte
Autres articles (37)
-
HTML5 audio and video support
13 avril 2011, parMediaSPIP uses HTML5 video and audio tags to play multimedia files, taking advantage of the latest W3C innovations supported by modern browsers.
The MediaSPIP player used has been created specifically for MediaSPIP and can be easily adapted to fit in with a specific theme.
For older browsers the Flowplayer flash fallback is used.
MediaSPIP allows for media playback on major mobile platforms with the above (...) -
De l’upload à la vidéo finale [version standalone]
31 janvier 2010, parLe chemin d’un document audio ou vidéo dans SPIPMotion est divisé en trois étapes distinctes.
Upload et récupération d’informations de la vidéo source
Dans un premier temps, il est nécessaire de créer un article SPIP et de lui joindre le document vidéo "source".
Au moment où ce document est joint à l’article, deux actions supplémentaires au comportement normal sont exécutées : La récupération des informations techniques des flux audio et video du fichier ; La génération d’une vignette : extraction d’une (...) -
Support audio et vidéo HTML5
10 avril 2011MediaSPIP utilise les balises HTML5 video et audio pour la lecture de documents multimedia en profitant des dernières innovations du W3C supportées par les navigateurs modernes.
Pour les navigateurs plus anciens, le lecteur flash Flowplayer est utilisé.
Le lecteur HTML5 utilisé a été spécifiquement créé pour MediaSPIP : il est complètement modifiable graphiquement pour correspondre à un thème choisi.
Ces technologies permettent de distribuer vidéo et son à la fois sur des ordinateurs conventionnels (...)
Sur d’autres sites (3673)
-
[ffmpeg]How to use libfdk_aac to encode pcm to constant bitrate
27 décembre 2024, par qytI have tried both libfdk_aac and aac, but the encoded PCM audio always has a variable bit rate. Why is this happening ? How can I make it encode with a constant bit rate
The code is as follows :


#include 
#include 
#include 
extern "C"
{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavdevice></libavdevice>avdevice.h>
#include <libavfilter></libavfilter>avfilter.h>
#include <libavfilter></libavfilter>buffersrc.h>
#include <libavfilter></libavfilter>buffersink.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>imgutils.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>channel_layout.h> 
#include <libswresample></libswresample>swresample.h>
#include <libswscale></libswscale>swscale.h>
}

int main(int argc, char** argv) {
 AVCodecContext* codec_context = NULL;
 const AVCodec* codec = NULL;
 AVFrame* frame = NULL;
 AVPacket* pkt = NULL;
 FILE* input_file = NULL;
 FILE* output_file = NULL;
 int ret;

 // Open input file
 const char* input_filename = "D:\\audio\\b.pcm";
 const char* output_filename = "D:\\audio\\input.aac";
 input_file = fopen(input_filename, "rb");
 output_file = fopen(output_filename, "wb");
 if (!input_file || !output_file) {
 fprintf(stderr, "Could not open input or output file\n");
 exit(1);
 }

 // Find the AAC encoder
 codec = avcodec_find_encoder_by_name("libfdk_aac");
 if (!codec) {
 fprintf(stderr, "Codec not found\n");
 exit(1);
 }

 codec_context = avcodec_alloc_context3(codec);
 if (!codec_context) {
 fprintf(stderr, "Could not allocate audio codec context\n");
 exit(1);
 }

 // Set codec parameters
 codec_context->sample_fmt = AV_SAMPLE_FMT_S16;
 codec_context->sample_rate = 44100;
 codec_context->bit_rate = 256000;
 codec_context->rc_buffer_size = codec_context->bit_rate; 
 codec_context->rc_min_rate = codec_context->bit_rate; 
 codec_context->rc_max_rate = codec_context->bit_rate; 
 av_channel_layout_default(&codec_context->ch_layout, 2);
 // Open codec
 if (avcodec_open2(codec_context, codec, &opts) < 0) {
 fprintf(stderr, "Could not open codec\n");
 exit(1);
 }

 // Initialize packet
 pkt = av_packet_alloc();
 if (!pkt) {
 fprintf(stderr, "Could not allocate AVPacket\n");
 exit(1);
 }

 // Initialize frame
 frame = av_frame_alloc();
 frame->nb_samples = codec_context->frame_size;
 frame->format = codec_context->sample_fmt;
 frame->ch_layout.nb_channels = 2;

 // Allocate the data buffers
 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not allocate audio data buffers\n");
 exit(1);
 }

 // Main loop: read from the input file, encode, write to the output file
 while (fread(frame->data[0], 1, frame->linesize[0], input_file) == frame->linesize[0]) {
 // Send the frame to the encoder
 if (avcodec_send_frame(codec_context, frame) < 0) {
 fprintf(stderr, "Error sending frame to codec\n");
 exit(1);
 }

 // Get the encoded packet
 while (avcodec_receive_packet(codec_context, pkt) == 0) {
 fwrite(pkt->data, 1, pkt->size, output_file);
 av_packet_unref(pkt);
 }
 }

 // Flush the encoder
 avcodec_send_frame(codec_context, NULL);
 while (avcodec_receive_packet(codec_context, pkt) == 0) {
 fwrite(pkt->data, 1, pkt->size, output_file);
 av_packet_unref(pkt);
 }

 // Clean up
 fclose(input_file);
 fclose(output_file);
 av_frame_free(&frame);
 av_packet_free(&pkt);
 avcodec_free_context(&codec_context);

 return 0;
}




I encoded the AAC and then used FFmpeg to write it to an MP4 file : ./ffmpeg -i input.aac -c copy output.mp4, and I checked it using MediaInfo


The PCM file is S16, 2 channels, 44100 Hz


I try to write mp4 directly and check the aac audio through mediainfo which is also a variable bitrate


#include 
#include 
#include 
extern "C"
{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavdevice></libavdevice>avdevice.h>
#include <libavfilter></libavfilter>avfilter.h>
#include <libavfilter></libavfilter>buffersrc.h>
#include <libavfilter></libavfilter>buffersink.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>avutil.h>
#include <libavutil></libavutil>imgutils.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>channel_layout.h> 
#include <libswresample></libswresample>swresample.h>
#include <libswscale></libswscale>swscale.h>
}

int main(int argc, char** argv) {
 AVCodecContext* codec_context = NULL;
 const AVCodec* codec = NULL;
 AVFormatContext* format_context = NULL;
 AVStream* audio_stream = NULL;
 AVFrame* frame = NULL;
 AVPacket* pkt = NULL;
 FILE* input_file = NULL;
 int ret;
 int64_t next_pts = 0;

 // Open input file
 const char* input_filename = "D:\\audio\\b.pcm";
 const char* output_filename = "D:\\audio\\input.mp4";
 input_file = fopen(input_filename, "rb");
 if (!input_file) {
 fprintf(stderr, "Could not open input file\n");
 exit(1);
 }

 // Find the AAC encoder
 codec = avcodec_find_encoder_by_name("libfdk_aac");
 if (!codec) {
 fprintf(stderr, "Codec not found\n");
 exit(1);
 }

 // Allocate AVFormatContext for MP4 output
 avformat_alloc_output_context2(&format_context, NULL, NULL, output_filename);
 if (!format_context) {
 fprintf(stderr, "Could not allocate output context\n");
 exit(1);
 }

 // Add audio stream to the output file
 audio_stream = avformat_new_stream(format_context, codec);
 if (!audio_stream) {
 fprintf(stderr, "Could not allocate stream\n");
 exit(1);
 }

 codec_context = avcodec_alloc_context3(codec);
 if (!codec_context) {
 fprintf(stderr, "Could not allocate audio codec context\n");
 exit(1);
 }
 audio_stream->codecpar->frame_size = codec_context->frame_size = 1024;

 // Set codec parameters
 codec_context->sample_fmt = AV_SAMPLE_FMT_S16;
 codec_context->sample_rate = 44100;
 codec_context->bit_rate = 256000;
 codec_context->rc_buffer_size = codec_context->bit_rate;
 codec_context->rc_min_rate = codec_context->bit_rate;
 codec_context->rc_max_rate = codec_context->bit_rate;
 av_channel_layout_default(&codec_context->ch_layout, 2);

 // Copy settings to stream
 ret = avcodec_parameters_from_context(audio_stream->codecpar, codec_context);
 if (ret < 0) {
 fprintf(stderr, "Failed to copy codec parameters to stream\n");
 exit(1);
 }

 // Open codec
 if (avcodec_open2(codec_context, codec, NULL) < 0) {
 fprintf(stderr, "Could not open codec\n");
 exit(1);
 }

 // Open output file
 if (!(format_context->oformat->flags & AVFMT_NOFILE)) {
 ret = avio_open(&format_context->pb, output_filename, AVIO_FLAG_WRITE);
 if (ret < 0) {
 fprintf(stderr, "Could not open output file '%s'\n", output_filename);
 exit(1);
 }
 }

 // Write file header
 ret = avformat_write_header(format_context, NULL);
 if (ret < 0) {
 fprintf(stderr, "Error occurred when opening output file\n");
 exit(1);
 }

 // Initialize packet and frame
 pkt = av_packet_alloc();
 if (!pkt) {
 fprintf(stderr, "Could not allocate AVPacket\n");
 exit(1);
 }

 frame = av_frame_alloc();
 frame->nb_samples = codec_context->frame_size;
 frame->format = codec_context->sample_fmt;
 frame->ch_layout = codec_context->ch_layout;

 // Allocate the data buffers
 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 fprintf(stderr, "Could not allocate audio data buffers\n");
 exit(1);
 }

 // Main loop: read from the input file, encode, write to the output file
 while (fread(frame->data[0], 1, frame->linesize[0], input_file) == frame->linesize[0]) {
 frame->pts = next_pts; // Set PTS for the frame
 next_pts += frame->nb_samples; // Increment the next PTS
 // Send the frame to the encoder
 if (avcodec_send_frame(codec_context, frame) < 0) {
 fprintf(stderr, "Error sending frame to codec\n");
 exit(1);
 }

 // Get the encoded packet
 while (avcodec_receive_packet(codec_context, pkt) == 0) {
 pkt->pts = pkt->dts = frame->pts;
 av_packet_rescale_ts(pkt, codec_context->time_base, audio_stream->time_base);
 pkt->stream_index = audio_stream->index;
 av_interleaved_write_frame(format_context, pkt);
 av_packet_unref(pkt);
 }
 }

 // Flush the encoder
 avcodec_send_frame(codec_context, NULL);
 while (avcodec_receive_packet(codec_context, pkt) == 0) {
 pkt->pts = pkt->dts = next_pts;
 av_packet_rescale_ts(pkt, codec_context->time_base, audio_stream->time_base);
 pkt->stream_index = audio_stream->index;
 av_interleaved_write_frame(format_context, pkt);
 av_packet_unref(pkt);
 next_pts += pkt->duration;
 }

 // Write file trailer
 av_write_trailer(format_context);

 // Clean up
 fclose(input_file);
 av_frame_free(&frame);
 av_packet_free(&pkt);
 avcodec_free_context(&codec_context);
 avio_closep(&format_context->pb);
 avformat_free_context(format_context);

 return 0;
}



-
Six Best Amplitude Alternatives
10 décembre 2024, par Daniel Crough -
ffmpeg piped output producing incorrect metadata frame count
8 décembre 2024, par XorgonThe short version : Using piped output from ffmpeg produces a file with incorrect metadata.


ffmpeg -y -i .\test_mp4.mp4 -f avi -c:v libx264 - > output.avi
to make an AVI file using the pipe output.

ffprobe -v error -count_frames -show_entries stream=duration,nb_read_frames,r_frame_rate .\output.avi


The output will show that the metadata does not match the actual frames contained in the video.


Details below.



Using Python, I am attempting to use ffmpeg to compress videos and put them in a PowerPoint. This works great, however, the video files themselves have incorrect frame counts which can cause issues when I read from those videos in other code.


Edit for clarification : by "frame count" I mean the metadata frame count. The actual number of frames contained in the video is correct, but querying the metadata gives an incorrect frame count.


Having eliminated the PowerPoint aspect of the code, I've narrowed this down to the following minimal reproducing example of saving an output from an ffmpeg pipe :


from subprocess import Popen, PIPE

video_path = 'test_mp4.mp4'

ffmpeg_pipe = Popen(['ffmpeg',
 '-y', # Overwrite files
 '-i', f'{video_path}', # Input from file
 '-f', 'avi', # Output format
 '-c:v', 'libx264', # Codec
 '-'], # Output to pipe
 stdout=PIPE)

new_path = "piped_video.avi"
vid_file = open(new_path, "wb")
vid_file.write(ffmpeg_pipe.stdout.read())
vid_file.close()



I've tested several different videos. One small example video that I've tested can be found here.


I've tried a few different codecs with
avi
format and triedlibvpx
withwebm
format. For theavi
outputs, the frame count usually reads as1073741824
(2^30). Weirdly, for thewebm
format, the frame count read as-276701161105643264
.

Edit : This issue can also be reproduced with just ffmpeg in command prompt using the following command :

ffmpeg -y -i .\test_mp4.mp4 -f avi -c:v libx264 - > output.avi


This is a snippet I used to read the frame count, but one could also see the error by opening the video details in Windows Explorer and seeing the total time as something like 9942 hours, 3 minutes, and 14 seconds.


import cv2

video_path = 'test_mp4.mp4'
new_path = "piped_video.webm"

cap = cv2.VideoCapture(video_path)
print(f"Original video frame count: = {int(cap.get(cv2.CAP_PROP_FRAME_COUNT)):d}")
cap.release()

cap = cv2.VideoCapture(new_path)
print(f"Piped video frame count: = {int(cap.get(cv2.CAP_PROP_FRAME_COUNT)):d}")
cap.release()



The error can also be observed using
ffprobe
with the following command :ffprobe -v error -count_frames -show_entries stream=duration,nb_read_frames,r_frame_rate .\output.avi
. Note that the frame rate and number of frames counted by ffprobe do not match with the duration from the metadata.

For completeness, here is the ffmpeg output :


ffmpeg version 2023-06-11-git-09621fd7d9-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
 built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
 configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
 libavutil 58. 13.100 / 58. 13.100
 libavcodec 60. 17.100 / 60. 17.100
 libavformat 60. 6.100 / 60. 6.100
 libavdevice 60. 2.100 / 60. 2.100
 libavfilter 9. 8.101 / 9. 8.101
 libswscale 7. 3.100 / 7. 3.100
 libswresample 4. 11.100 / 4. 11.100
 libpostproc 57. 2.100 / 57. 2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test_mp4.mp4':
 Metadata:
 major_brand : mp42
 minor_version : 0
 compatible_brands: isommp42
 creation_time : 2022-08-10T12:54:09.000000Z
 Duration: 00:00:06.67, start: 0.000000, bitrate: 567 kb/s
 Stream #0:0[0x1](eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 384x264 [SAR 1:1 DAR 16:11], 563 kb/s, 30 fps, 30 tbr, 30k tbn (default)
 Metadata:
 creation_time : 2022-08-10T12:54:09.000000Z
 handler_name : Mainconcept MP4 Video Media Handler
 vendor_id : [0][0][0][0]
 encoder : AVC Coding
Stream mapping:
 Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0000018c68c8b9c0] using SAR=1/1
[libx264 @ 0000018c68c8b9c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0000018c68c8b9c0] profile High, level 2.1, 4:2:0, 8-bit
Output #0, avi, to 'pipe:':
 Metadata:
 major_brand : mp42
 minor_version : 0
 compatible_brands: isommp42
 ISFT : Lavf60.6.100
 Stream #0:0(eng): Video: h264 (H264 / 0x34363248), yuv420p(progressive), 384x264 [SAR 1:1 DAR 16:11], q=2-31, 30 fps, 30 tbn (default)
 Metadata:
 creation_time : 2022-08-10T12:54:09.000000Z
 handler_name : Mainconcept MP4 Video Media Handler
 vendor_id : [0][0][0][0]
 encoder : Lavc60.17.100 libx264
 Side data:
 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[out#0/avi @ 0000018c687f47c0] video:82kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.631060%
frame= 200 fps=0.0 q=-1.0 Lsize= 85kB time=00:00:06.56 bitrate= 106.5kbits/s speed=76.2x 
[libx264 @ 0000018c68c8b9c0] frame I:1 Avg QP:16.12 size: 3659
[libx264 @ 0000018c68c8b9c0] frame P:80 Avg QP:21.31 size: 647
[libx264 @ 0000018c68c8b9c0] frame B:119 Avg QP:26.74 size: 243
[libx264 @ 0000018c68c8b9c0] consecutive B-frames: 3.0% 53.0% 0.0% 44.0%
[libx264 @ 0000018c68c8b9c0] mb I I16..4: 17.6% 70.6% 11.8%
[libx264 @ 0000018c68c8b9c0] mb P I16..4: 0.8% 1.7% 0.6% P16..4: 17.6% 4.6% 3.3% 0.0% 0.0% skip:71.4%
[libx264 @ 0000018c68c8b9c0] mb B I16..4: 0.1% 0.3% 0.2% B16..8: 11.7% 1.4% 0.4% direct: 0.6% skip:85.4% L0:32.0% L1:59.7% BI: 8.3%
[libx264 @ 0000018c68c8b9c0] 8x8 transform intra:59.6% inter:62.4%
[libx264 @ 0000018c68c8b9c0] coded y,uvDC,uvAC intra: 48.5% 0.0% 0.0% inter: 3.5% 0.0% 0.0%
[libx264 @ 0000018c68c8b9c0] i16 v,h,dc,p: 19% 39% 25% 17%
[libx264 @ 0000018c68c8b9c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 25% 30% 3% 3% 4% 4% 4% 5%
[libx264 @ 0000018c68c8b9c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 22% 20% 16% 6% 8% 8% 8% 5% 6%
[libx264 @ 0000018c68c8b9c0] i8c dc,h,v,p: 100% 0% 0% 0%
[libx264 @ 0000018c68c8b9c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0000018c68c8b9c0] ref P L0: 76.2% 7.9% 11.2% 4.7%
[libx264 @ 0000018c68c8b9c0] ref B L0: 85.6% 12.9% 1.5%
[libx264 @ 0000018c68c8b9c0] ref B L1: 97.7% 2.3%
[libx264 @ 0000018c68c8b9c0] kb/s:101.19



So the question is : why does this happen, and how can one avoid it ?