
Recherche avancée
Médias (1)
-
La conservation du net art au musée. Les stratégies à l’œuvre
26 mai 2011
Mis à jour : Juillet 2013
Langue : français
Type : Texte
Autres articles (43)
-
Publier sur MédiaSpip
13 juin 2013Puis-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 -
Déploiements possibles
31 janvier 2010, parDeux types de déploiements sont envisageable dépendant de deux aspects : La méthode d’installation envisagée (en standalone ou en ferme) ; Le nombre d’encodages journaliers et la fréquentation envisagés ;
L’encodage de vidéos est un processus lourd consommant énormément de ressources système (CPU et RAM), il est nécessaire de prendre tout cela en considération. Ce système n’est donc possible que sur un ou plusieurs serveurs dédiés.
Version mono serveur
La version mono serveur consiste à n’utiliser qu’une (...) -
Creating farms of unique websites
13 avril 2011, parMediaSPIP platforms can be installed as a farm, with a single "core" hosted on a dedicated server and used by multiple websites.
This allows (among other things) : implementation costs to be shared between several different projects / individuals rapid deployment of multiple unique sites creation of groups of like-minded sites, making it possible to browse media in a more controlled and selective environment than the major "open" (...)
Sur d’autres sites (5997)
-
How HSBC and ING are transforming banking with AI
9 novembre 2024, par Daniel Crough — Banking and Financial Services, Featured Banking Content -
Error Error sending packet to decoder -1094995529 when extracting audio from MP4 using FFmpeg C API
28 août 2024, par Saad Out03I am trying to extract audio from an MP4 file and save it as an MP3 using the FFmpeg C API. My code is able to identify the audio stream, set up the codec contexts, and start reading packets. However, I encounter an error when sending packets to the decoder.


Purpose of the Code :


- 

- Open the input video file and retrieve the stream information.
- Identify the audio stream and set up decoding with the appropriate codec.
- Set up the output format for the MP3 file and configure codec parameters.
- Read packets from the input file and send them to the decoder.










Here's the part of the code that reads packets and sends them to the decoder :


// read packets
 AVFrame *pframe = av_frame_alloc();
 if (!pframe)
 return (printf("Could not allocate packet frame\n"), 1);
 AVPacket pkt;
 while (1)
 {
 if (av_read_frame(fmt_ctx, &pkt) < 0)
 break;
 printf("packet stream index %d\n", pkt.stream_index);
 if ((pkt.stream_index) == audio_stream_index)
 {
 printf("packet size %d\n", pkt.size);
 int ret = avcodec_send_packet(cd_ctx, &pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 printf("EAGAIN or EOF\n");
 else if (ret < 0)
 return (printf("Error sending packet to decoder %d\n", ret), 1);
 }
 }




Error output :


format mov,mp4,m4a,3gp,3g2,mj2 duration 2.550000
nb of streams 2
stream 0 => id: 1 => type: video
stream 1 => id: 2 => type: audio
audio stream index 1
Output #0, mp3, to 'audio.mp3':
 Stream #0:0: Audio: mp3 (libmp3lame) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
 reading packets....
packet stream index 0
packet stream index 0
packet stream index 1
packet size 371
Error sending packet to decoder -1094995529




What could be causing this error, and how can I correct it ? Any guidance on proper handling of packets with FFmpeg’s C API would be greatly appreciated.


Full code for context :


#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include 

#define RED "\033[0;31m"
#define GREEN "\033[0;32m"
#define RESET "\033[0m"

int main(int ac, char **av)
{
 if (ac != 3)
 {
 printf("Usage: %s <input file="file" /> <output file="file">\n", av[0]);
 return 1;
 }

 AVCodec *codec = NULL;
 AVCodecContext *cd_ctx = NULL;
 AVOutputFormat *out_format = NULL;
 AVFormatContext *out_ctx = NULL;
 AVCodec *out_codec = NULL;
 AVStream *in_stream = NULL, *out_stream = NULL;

 // allocate format context
 AVFormatContext *fmt_ctx = avformat_alloc_context();
 if (!fmt_ctx)
 return (printf("Could not allocate format context\n"), 1);

 // open input file
 if (avformat_open_input(&fmt_ctx, av[1], NULL, NULL) != 0)
 return (printf("Could not open input file\n"), 1);

 printf("format %s duration %f\n",
 fmt_ctx->iformat->name,
 (double)(fmt_ctx->duration / 1000000) / (double)60);
 
 // get stream info
 if (avformat_find_stream_info(fmt_ctx, NULL) < 0)
 return (printf("Could not get stream info\n"), 1);
 
 printf("nb of streams %d\n", fmt_ctx->nb_streams);
 int audio_stream_index = -1;
 // loop through streams
 for (unsigned int i = 0; i < fmt_ctx->nb_streams; i++)
 {
 printf("stream %d => id: %d => type: ", i, fmt_ctx->streams[i]->id);
 // check stream type
 if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
 {
 printf("audio\n");

 audio_stream_index = i;
 printf("audio stream index %d\n", audio_stream_index);
 }
 else if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
 printf("video\n");
 else
 printf("other\n");
 }


 // get codec
 codec = avcodec_find_decoder(fmt_ctx->streams[audio_stream_index]->codecpar->codec_id);
 if (!codec)
 return (printf("Could not find codec\n"), 1);

 // allocate codec context
 cd_ctx = avcodec_alloc_context3(codec);
 if (!cd_ctx)
 return (printf("Could not allocate codec context\n"), 1);
 
 // open codec
 if (avcodec_open2(cd_ctx, codec, NULL) < 0)
 return (printf("Could not open codec\n"), 1);
 
 // get codec format
 out_format = av_guess_format(NULL, av[2], NULL);
 if (!out_format)
 return (printf("Could not guess out format\n"), 1);

 // allocate output context
 out_ctx = NULL;
 if (avformat_alloc_output_context2(&out_ctx, NULL, NULL, av[2]) < 0)
 return (printf("Could not allocate context for output\n"), 1);

 // find encoder for output format
 out_codec = avcodec_find_encoder(out_format->audio_codec);
 if (!out_codec)
 return (printf("Could not find encoder\n"), 1);
 
 in_stream = fmt_ctx->streams[audio_stream_index];

 // create new stream
 if ((out_stream = avformat_new_stream(out_ctx, out_codec)) == NULL)
 return (printf("Could not create new stream\n"), 1);
 
 // Fill output codec context with desired sample rate, sample fmt, channel etc.
 AVCodecParameters *in_codecpar = in_stream->codecpar;
 AVCodecParameters *out_codecpar = out_stream->codecpar;
 int ret = avcodec_parameters_copy(out_codecpar, in_codecpar);
 if (ret < 0)
 return (printf("Could not copy codec parameters\n"), 1);

 // Set codec ID to MP3
 out_codecpar->codec_id = AV_CODEC_ID_MP3;

 // Ensure codec parameters match MP3 requirements
 out_codecpar->format = AV_SAMPLE_FMT_FLTP; // Set to a format suitable for MP3
 out_codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
 out_codecpar->sample_rate = 44100; // Set the desired sample rate, e.g., 44100 Hz
 out_codecpar->channel_layout = AV_CH_LAYOUT_STEREO; // Stereo layout
 out_codecpar->channels = 2; // Number of audio channels

 av_dump_format(out_ctx, 0, av[2], 1);

 // open output file
 if (avio_open(&out_ctx->pb, av[2], AVIO_FLAG_WRITE) > 0)
 return (printf("Could not open out file\n"), 1);
 
 // write headers
 if (avformat_write_header(out_ctx, NULL) < 0)
 return (printf("Could not write headers\n"), 1);
 

 printf("\treading packets....\n");


 // read packets
 AVFrame *pframe = av_frame_alloc();
 if (!pframe)
 return (printf("Could not allocate packet frame\n"), 1);
 AVPacket pkt;
 while (1)
 {
 if (av_read_frame(fmt_ctx, &pkt) < 0)
 break;
 printf("packet stream index %d\n", pkt.stream_index);
 if ((pkt.stream_index) == audio_stream_index)
 {
 printf("packet size %d\n", pkt.size);
 int ret = avcodec_send_packet(cd_ctx, &pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
 printf("EAGAIN or EOF\n");
 else if (ret < 0)
 return (printf("Error sending packet to decoder %d\n", ret), 1);
 }
 }

 printf("done\n");
 return 0;
}

</output>


-
hwframe : Allow hwaccel frame allocators to align surface sizes
22 juin 2017, par Anton Khirnovhwframe : Allow hwaccel frame allocators to align surface sizes
Hardware accelerated decoding generally uses AVHWFramesContext for pool
allocation of hardware surfaces. These are setup to allocate surfaces
aligned to hardware and hwaccel API requirements. Due to the
architecture, av_hwframe_get_buffer() will return AVFrames with
the dimensions set to the aligned sizes.This causes some decoders (like hevc) return these aligned size as
final frame size, instead of cropping them to the video’s actual
dimensions. To make sure this doesn’t happen, crop the frame to the
size the decoder expects when ff_get_buffer() is called.Signed-off-by : Luca Barbato <lu_zero@gentoo.org>