
Recherche avancée
Autres articles (71)
-
Des sites réalisés avec MediaSPIP
2 mai 2011, parCette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page. -
Librairies et binaires spécifiques au traitement vidéo et sonore
31 janvier 2010, parLes logiciels et librairies suivantes sont utilisées par SPIPmotion d’une manière ou d’une autre.
Binaires obligatoires FFMpeg : encodeur principal, permet de transcoder presque tous les types de fichiers vidéo et sonores dans les formats lisibles sur Internet. CF ce tutoriel pour son installation ; Oggz-tools : outils d’inspection de fichiers ogg ; Mediainfo : récupération d’informations depuis la plupart des formats vidéos et sonores ;
Binaires complémentaires et facultatifs flvtool2 : (...) -
Possibilité de déploiement en ferme
12 avril 2011, parMediaSPIP peut être installé comme une ferme, avec un seul "noyau" hébergé sur un serveur dédié et utilisé par une multitude de sites différents.
Cela permet, par exemple : de pouvoir partager les frais de mise en œuvre entre plusieurs projets / individus ; de pouvoir déployer rapidement une multitude de sites uniques ; d’éviter d’avoir à mettre l’ensemble des créations dans un fourre-tout numérique comme c’est le cas pour les grandes plate-formes tout public disséminées sur le (...)
Sur d’autres sites (5991)
-
The Ultimate List of Alternatives to Google Products
2 août 2022, par Erin — Privacy -
ffmpeg : libavformat/libswresample to transcode and resample at same time
21 février 2024, par whatdoidoI want to transcode and down/re-sample the audio for output using ffmpeg's libav*/libswresample - I am using ffmpeg's (4.x) transcode_aac.c and resample_audio.c as reference - but the code produces audio with glitches that is clearly not what ffmpeg itself would produce (ie ffmpeg -i foo.wav -ar 22050 foo.m4a)


Based on the ffmpeg examples, to resample audio it appears that I need to set the output AVAudioContext and SwrContext sample_rate to what I desire and ensure the swr_convert() is provided with the correct number of output samples based av_rescale_rnd( swr_delay(), ...) once I have an decoded input audio. I've taken care to ensure all the relevant calculations of samples for output are taken into account in the merged code (below) :


- 

- open_output_file() - AVCodecContext.sample_rate (avctx variable) set to our target (down sampled) sample_rate
- read_decode_convert_and_store() is where the work happens : input audio is decoded to an AVFrame and this input frame is converted before being encoded.

- 

- init_converted_samples() and av_samples_alloc() uses the input frame's nb_samples
- ADDED : calc the number of output samples via av_rescale_rnd() and swr_delay()
- UPDATED : convert_samples() and swr_convert() uses the input frame's samples and our calculated output samples as parameters














However the resulting audio file is produced with audio glitches. Does the community know of any references for how transcode AND resample should be done or what is missing in this example ?


/* compile and run:
 gcc -I/usr/include/ffmpeg transcode-swr-aac.c -lavformat -lavutil -lavcodec -lswresample -lm
 ./a.out foo.wav foo.m4a
 */

/*
 * Copyright (c) 2013-2018 Andreas Unterweger
 * 
 * This file is part of FFmpeg. 
 ... ...
 * 
 * @example transcode_aac.c 
 * Convert an input audio file to AAC in an MP4 container using FFmpeg. 
 * Formats other than MP4 are supported based on the output file extension. 
 * @author Andreas Unterweger (xxxx@xxxxx.com)
 */ 
 #include 
 

 #include "libavformat/avformat.h"
 #include "libavformat/avio.h"
 
 #include "libavcodec/avcodec.h"
 
 #include "libavutil/audio_fifo.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/frame.h"
 #include "libavutil/opt.h"
 
 #include "libswresample/swresample.h"
 
 #define OUTPUT_BIT_RATE 128000
 #define OUTPUT_CHANNELS 2
 
 static int open_input_file(const char *filename,
 AVFormatContext **input_format_context,
 AVCodecContext **input_codec_context)
 {
 AVCodecContext *avctx;
 const AVCodec *input_codec;
 const AVStream *stream;
 int error;
 
 if ((error = avformat_open_input(input_format_context, filename, NULL,
 NULL)) < 0) {
 fprintf(stderr, "Could not open input file '%s' (error '%s')\n",
 filename, av_err2str(error));
 *input_format_context = NULL;
 return error;
 }
 

 if ((error = avformat_find_stream_info(*input_format_context, NULL)) < 0) {
 fprintf(stderr, "Could not open find stream info (error '%s')\n",
 av_err2str(error));
 avformat_close_input(input_format_context);
 return error;
 }
 
 if ((*input_format_context)->nb_streams != 1) {
 fprintf(stderr, "Expected one audio input stream, but found %d\n",
 (*input_format_context)->nb_streams);
 avformat_close_input(input_format_context);
 return AVERROR_EXIT;
 }
 
 stream = (*input_format_context)->streams[0];
 
 if (!(input_codec = avcodec_find_decoder(stream->codecpar->codec_id))) {
 fprintf(stderr, "Could not find input codec\n");
 avformat_close_input(input_format_context);
 return AVERROR_EXIT;
 }
 
 avctx = avcodec_alloc_context3(input_codec);
 if (!avctx) {
 fprintf(stderr, "Could not allocate a decoding context\n");
 avformat_close_input(input_format_context);
 return AVERROR(ENOMEM);
 }
 
 /* Initialize the stream parameters with demuxer information. */
 error = avcodec_parameters_to_context(avctx, stream->codecpar);
 if (error < 0) {
 avformat_close_input(input_format_context);
 avcodec_free_context(&avctx);
 return error;
 }
 
 /* Open the decoder for the audio stream to use it later. */
 if ((error = avcodec_open2(avctx, input_codec, NULL)) < 0) {
 fprintf(stderr, "Could not open input codec (error '%s')\n",
 av_err2str(error));
 avcodec_free_context(&avctx);
 avformat_close_input(input_format_context);
 return error;
 }
 
 /* Set the packet timebase for the decoder. */
 avctx->pkt_timebase = stream->time_base;
 
 /* Save the decoder context for easier access later. */
 *input_codec_context = avctx;
 
 return 0;
 }
 
 static int open_output_file(const char *filename,
 AVCodecContext *input_codec_context,
 AVFormatContext **output_format_context,
 AVCodecContext **output_codec_context)
 {
 AVCodecContext *avctx = NULL;
 AVIOContext *output_io_context = NULL;
 AVStream *stream = NULL;
 const AVCodec *output_codec = NULL;
 int error;
 

 if ((error = avio_open(&output_io_context, filename,
 AVIO_FLAG_WRITE)) < 0) {
 fprintf(stderr, "Could not open output file '%s' (error '%s')\n",
 filename, av_err2str(error));
 return error;
 }
 

 if (!(*output_format_context = avformat_alloc_context())) {
 fprintf(stderr, "Could not allocate output format context\n");
 return AVERROR(ENOMEM);
 }
 

 (*output_format_context)->pb = output_io_context;
 

 if (!((*output_format_context)->oformat = av_guess_format(NULL, filename,
 NULL))) {
 fprintf(stderr, "Could not find output file format\n");
 goto cleanup;
 }
 
 if (!((*output_format_context)->url = av_strdup(filename))) {
 fprintf(stderr, "Could not allocate url.\n");
 error = AVERROR(ENOMEM);
 goto cleanup;
 }
 

 if (!(output_codec = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
 fprintf(stderr, "Could not find an AAC encoder.\n");
 goto cleanup;
 }
 
 /* Create a new audio stream in the output file container. */
 if (!(stream = avformat_new_stream(*output_format_context, NULL))) {
 fprintf(stderr, "Could not create new stream\n");
 error = AVERROR(ENOMEM);
 goto cleanup;
 }
 
 avctx = avcodec_alloc_context3(output_codec);
 if (!avctx) {
 fprintf(stderr, "Could not allocate an encoding context\n");
 error = AVERROR(ENOMEM);
 goto cleanup;
 }
 
 /* Set the basic encoder parameters.
 * SET OUR DESIRED output sample_rate here
 */
 avctx->channels = OUTPUT_CHANNELS;
 avctx->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
 // avctx->sample_rate = input_codec_context->sample_rate;
 avctx->sample_rate = 22050;
 avctx->sample_fmt = output_codec->sample_fmts[0];
 avctx->bit_rate = OUTPUT_BIT_RATE;
 
 avctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
 
 /* Set the sample rate for the container. */
 stream->time_base.den = avctx->sample_rate;
 stream->time_base.num = 1;
 
 if ((*output_format_context)->oformat->flags & AVFMT_GLOBALHEADER)
 avctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 
 if ((error = avcodec_open2(avctx, output_codec, NULL)) < 0) {
 fprintf(stderr, "Could not open output codec (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 }
 
 error = avcodec_parameters_from_context(stream->codecpar, avctx);
 if (error < 0) {
 fprintf(stderr, "Could not initialize stream parameters\n");
 goto cleanup;
 }
 
 /* Save the encoder context for easier access later. */
 *output_codec_context = avctx;
 
 return 0;
 
 cleanup:
 avcodec_free_context(&avctx);
 avio_closep(&(*output_format_context)->pb);
 avformat_free_context(*output_format_context);
 *output_format_context = NULL;
 return error < 0 ? error : AVERROR_EXIT;
 }
 
 /**
 * Initialize one data packet for reading or writing.
 */
 static int init_packet(AVPacket **packet)
 {
 if (!(*packet = av_packet_alloc())) {
 fprintf(stderr, "Could not allocate packet\n");
 return AVERROR(ENOMEM);
 }
 return 0;
 }
 
 static int init_input_frame(AVFrame **frame)
 {
 if (!(*frame = av_frame_alloc())) {
 fprintf(stderr, "Could not allocate input frame\n");
 return AVERROR(ENOMEM);
 }
 return 0;
 }
 
 static int init_resampler(AVCodecContext *input_codec_context,
 AVCodecContext *output_codec_context,
 SwrContext **resample_context)
 {
 int error;

 /**
 * create the resample, including ref to the desired output sample rate
 */
 *resample_context = swr_alloc_set_opts(NULL,
 av_get_default_channel_layout(output_codec_context->channels),
 output_codec_context->sample_fmt,
 output_codec_context->sample_rate,
 av_get_default_channel_layout(input_codec_context->channels),
 input_codec_context->sample_fmt,
 input_codec_context->sample_rate,
 0, NULL);
 if (!*resample_context < 0) {
 fprintf(stderr, "Could not allocate resample context\n");
 return AVERROR(ENOMEM);
 }
 
 if ((error = swr_init(*resample_context)) < 0) {
 fprintf(stderr, "Could not open resample context\n");
 swr_free(resample_context);
 return error;
 }
 return 0;
 }
 
 static int init_fifo(AVAudioFifo **fifo, AVCodecContext *output_codec_context)
 {
 if (!(*fifo = av_audio_fifo_alloc(output_codec_context->sample_fmt,
 output_codec_context->channels, 1))) {
 fprintf(stderr, "Could not allocate FIFO\n");
 return AVERROR(ENOMEM);
 }
 return 0;
 }
 
 static int write_output_file_header(AVFormatContext *output_format_context)
 {
 int error;
 if ((error = avformat_write_header(output_format_context, NULL)) < 0) {
 fprintf(stderr, "Could not write output file header (error '%s')\n",
 av_err2str(error));
 return error;
 }
 return 0;
 }
 
 static int decode_audio_frame(AVFrame *frame,
 AVFormatContext *input_format_context,
 AVCodecContext *input_codec_context,
 int *data_present, int *finished)
 {
 AVPacket *input_packet;
 int error;
 
 error = init_packet(&input_packet);
 if (error < 0)
 return error;
 
 *data_present = 0;
 *finished = 0;

 if ((error = av_read_frame(input_format_context, input_packet)) < 0) {
 if (error == AVERROR_EOF)
 *finished = 1;
 else {
 fprintf(stderr, "Could not read frame (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 }
 }
 
 if ((error = avcodec_send_packet(input_codec_context, input_packet)) < 0) {
 fprintf(stderr, "Could not send packet for decoding (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 }
 
 error = avcodec_receive_frame(input_codec_context, frame);
 if (error == AVERROR(EAGAIN)) {
 error = 0;
 goto cleanup;
 } else if (error == AVERROR_EOF) {
 *finished = 1;
 error = 0;
 goto cleanup;
 } else if (error < 0) {
 fprintf(stderr, "Could not decode frame (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 } else {
 *data_present = 1;
 goto cleanup;
 }
 
 cleanup:
 av_packet_free(&input_packet);
 return error;
 }
 
 static int init_converted_samples(uint8_t ***converted_input_samples,
 AVCodecContext *output_codec_context,
 int frame_size)
 {
 int error;
 
 if (!(*converted_input_samples = calloc(output_codec_context->channels,
 sizeof(**converted_input_samples)))) {
 fprintf(stderr, "Could not allocate converted input sample pointers\n");
 return AVERROR(ENOMEM);
 }
 

 if ((error = av_samples_alloc(*converted_input_samples, NULL,
 output_codec_context->channels,
 frame_size,
 output_codec_context->sample_fmt, 0)) < 0) {
 fprintf(stderr,
 "Could not allocate converted input samples (error '%s')\n",
 av_err2str(error));
 av_freep(&(*converted_input_samples)[0]);
 free(*converted_input_samples);
 return error;
 }
 return 0;
 }
 
 static int convert_samples(const uint8_t **input_data, const int input_nb_samples,
 uint8_t **converted_data, const int output_nb_samples,
 SwrContext *resample_context)
 {
 int error;
 
 if ((error = swr_convert(resample_context,
 converted_data, output_nb_samples,
 input_data , input_nb_samples)) < 0) {
 fprintf(stderr, "Could not convert input samples (error '%s')\n",
 av_err2str(error));
 return error;
 }
 
 return 0;
 }
 
 static int add_samples_to_fifo(AVAudioFifo *fifo,
 uint8_t **converted_input_samples,
 const int frame_size)
 {
 int error;
 
 if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
 fprintf(stderr, "Could not reallocate FIFO\n");
 return error;
 }
 
 if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
 frame_size) < frame_size) {
 fprintf(stderr, "Could not write data to FIFO\n");
 return AVERROR_EXIT;
 }
 return 0;
 }
 
 static int read_decode_convert_and_store(AVAudioFifo *fifo,
 AVFormatContext *input_format_context,
 AVCodecContext *input_codec_context,
 AVCodecContext *output_codec_context,
 SwrContext *resampler_context,
 int *finished)
 {
 AVFrame *input_frame = NULL;
 uint8_t **converted_input_samples = NULL;
 int data_present;
 int ret = AVERROR_EXIT;
 

 if (init_input_frame(&input_frame))
 goto cleanup;

 if (decode_audio_frame(input_frame, input_format_context,
 input_codec_context, &data_present, finished))
 goto cleanup;

 if (*finished) {
 ret = 0;
 goto cleanup;
 }

 if (data_present) {
 /* Initialize the temporary storage for the converted input samples. */
 if (init_converted_samples(&converted_input_samples, output_codec_context,
 input_frame->nb_samples))
 goto cleanup;
 
 /* figure out how many samples are required for target sample_rate incl
 * any items left in the swr buffer
 */ 
 int output_nb_samples = av_rescale_rnd(
 swr_get_delay(resampler_context, input_codec_context->sample_rate) + input_frame->nb_samples,
 output_codec_context->sample_rate, 
 input_codec_context->sample_rate,
 AV_ROUND_UP);
 
 /* ignore, just to ensure we've got enough buffer alloc'd for conversion buffer */
 av_assert1(input_frame->nb_samples > output_nb_samples);
 
 /* Convert the input samples to the desired output sample format, via swr_convert().
 */
 if (convert_samples((const uint8_t**)input_frame->extended_data, input_frame->nb_samples,
 converted_input_samples, output_nb_samples,
 resampler_context))
 goto cleanup;
 
 /* Add the converted input samples to the FIFO buffer for later processing. */
 if (add_samples_to_fifo(fifo, converted_input_samples,
 output_nb_samples))
 goto cleanup;
 ret = 0;
 }
 ret = 0;
 
 cleanup:
 if (converted_input_samples) {
 av_freep(&converted_input_samples[0]);
 free(converted_input_samples);
 }
 av_frame_free(&input_frame);
 
 return ret;
 }
 
 static int init_output_frame(AVFrame **frame,
 AVCodecContext *output_codec_context,
 int frame_size)
 {
 int error;
 
 if (!(*frame = av_frame_alloc())) {
 fprintf(stderr, "Could not allocate output frame\n");
 return AVERROR_EXIT;
 }
 
 /* Set the frame's parameters, especially its size and format.
 * av_frame_get_buffer needs this to allocate memory for the
 * audio samples of the frame.
 * Default channel layouts based on the number of channels
 * are assumed for simplicity. */
 (*frame)->nb_samples = frame_size;
 (*frame)->channel_layout = output_codec_context->channel_layout;
 (*frame)->format = output_codec_context->sample_fmt;
 (*frame)->sample_rate = output_codec_context->sample_rate;
 
 /* Allocate the samples of the created frame. This call will make
 * sure that the audio frame can hold as many samples as specified. */
 if ((error = av_frame_get_buffer(*frame, 0)) < 0) {
 fprintf(stderr, "Could not allocate output frame samples (error '%s')\n",
 av_err2str(error));
 av_frame_free(frame);
 return error;
 }
 
 return 0;
 }
 
 /* Global timestamp for the audio frames. */
 static int64_t pts = 0;
 
 /**
 * Encode one frame worth of audio to the output file.
 */
 static int encode_audio_frame(AVFrame *frame,
 AVFormatContext *output_format_context,
 AVCodecContext *output_codec_context,
 int *data_present)
 {
 AVPacket *output_packet;
 int error;
 
 error = init_packet(&output_packet);
 if (error < 0)
 return error;
 
 /* Set a timestamp based on the sample rate for the container. */
 if (frame) {
 frame->pts = pts;
 pts += frame->nb_samples;
 }
 
 *data_present = 0;
 error = avcodec_send_frame(output_codec_context, frame);
 if (error < 0 && error != AVERROR_EOF) {
 fprintf(stderr, "Could not send packet for encoding (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 }
 

 error = avcodec_receive_packet(output_codec_context, output_packet);
 if (error == AVERROR(EAGAIN)) {
 error = 0;
 goto cleanup;
 } else if (error == AVERROR_EOF) {
 error = 0;
 goto cleanup;
 } else if (error < 0) {
 fprintf(stderr, "Could not encode frame (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 } else {
 *data_present = 1;
 }
 
 /* Write one audio frame from the temporary packet to the output file. */
 if (*data_present &&
 (error = av_write_frame(output_format_context, output_packet)) < 0) {
 fprintf(stderr, "Could not write frame (error '%s')\n",
 av_err2str(error));
 goto cleanup;
 }
 
 cleanup:
 av_packet_free(&output_packet);
 return error;
 }
 
 /**
 * Load one audio frame from the FIFO buffer, encode and write it to the
 * output file.
 */
 static int load_encode_and_write(AVAudioFifo *fifo,
 AVFormatContext *output_format_context,
 AVCodecContext *output_codec_context)
 {
 AVFrame *output_frame;
 /* Use the maximum number of possible samples per frame.
 * If there is less than the maximum possible frame size in the FIFO
 * buffer use this number. Otherwise, use the maximum possible frame size. */
 const int frame_size = FFMIN(av_audio_fifo_size(fifo),
 output_codec_context->frame_size);
 int data_written;
 
 if (init_output_frame(&output_frame, output_codec_context, frame_size))
 return AVERROR_EXIT;
 
 /* Read as many samples from the FIFO buffer as required to fill the frame.
 * The samples are stored in the frame temporarily. */
 if (av_audio_fifo_read(fifo, (void **)output_frame->data, frame_size) < frame_size) {
 fprintf(stderr, "Could not read data from FIFO\n");
 av_frame_free(&output_frame);
 return AVERROR_EXIT;
 }
 
 /* Encode one frame worth of audio samples. */
 if (encode_audio_frame(output_frame, output_format_context,
 output_codec_context, &data_written)) {
 av_frame_free(&output_frame);
 return AVERROR_EXIT;
 }
 av_frame_free(&output_frame);
 return 0;
 }
 
 /**
 * Write the trailer of the output file container.
 */
 static int write_output_file_trailer(AVFormatContext *output_format_context)
 {
 int error;
 if ((error = av_write_trailer(output_format_context)) < 0) {
 fprintf(stderr, "Could not write output file trailer (error '%s')\n",
 av_err2str(error));
 return error;
 }
 return 0;
 }
 
 int main(int argc, char **argv)
 {
 AVFormatContext *input_format_context = NULL, *output_format_context = NULL;
 AVCodecContext *input_codec_context = NULL, *output_codec_context = NULL;
 SwrContext *resample_context = NULL;
 AVAudioFifo *fifo = NULL;
 int ret = AVERROR_EXIT;
 
 if (argc != 3) {
 fprintf(stderr, "Usage: %s <input file="file" /> <output file="file">\n", argv[0]);
 exit(1);
 }
 

 if (open_input_file(argv[1], &input_format_context,
 &input_codec_context))
 goto cleanup;

 if (open_output_file(argv[2], input_codec_context,
 &output_format_context, &output_codec_context))
 goto cleanup;

 if (init_resampler(input_codec_context, output_codec_context,
 &resample_context))
 goto cleanup;

 if (init_fifo(&fifo, output_codec_context))
 goto cleanup;

 if (write_output_file_header(output_format_context))
 goto cleanup;
 
 while (1) {
 /* Use the encoder's desired frame size for processing. */
 const int output_frame_size = output_codec_context->frame_size;
 int finished = 0;
 
 while (av_audio_fifo_size(fifo) < output_frame_size) {
 /* Decode one frame worth of audio samples, convert it to the
 * output sample format and put it into the FIFO buffer. */
 if (read_decode_convert_and_store(fifo, input_format_context,
 input_codec_context,
 output_codec_context,
 resample_context, &finished))
 goto cleanup;
 
 if (finished)
 break;
 }
 
 while (av_audio_fifo_size(fifo) >= output_frame_size ||
 (finished && av_audio_fifo_size(fifo) > 0))
 if (load_encode_and_write(fifo, output_format_context,
 output_codec_context))
 goto cleanup;
 
 if (finished) {
 int data_written;
 do {
 if (encode_audio_frame(NULL, output_format_context,
 output_codec_context, &data_written))
 goto cleanup;
 } while (data_written);
 break;
 }
 }
 
 if (write_output_file_trailer(output_format_context))
 goto cleanup;
 ret = 0;
 
 cleanup:
 if (fifo)
 av_audio_fifo_free(fifo);
 swr_free(&resample_context);
 if (output_codec_context)
 avcodec_free_context(&output_codec_context);
 if (output_format_context) {
 avio_closep(&output_format_context->pb);
 avformat_free_context(output_format_context);
 }
 if (input_codec_context)
 avcodec_free_context(&input_codec_context);
 if (input_format_context)
 avformat_close_input(&input_format_context);
 
 return ret;
 }
</output>


-
FFMPEG command runs in terminal but not by subprocess
1er septembre 2022, par BasiliqueI am trying to run a bash command using the
subprocess
module from withinpython 3.10
.

The bash command is :


ffmpeg -framerate 1 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" out.mp4



In terminal the command runs fine. Here is the output :


ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
 built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
 configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
 libavutil 56. 31.100 / 56. 31.100
 libavcodec 58. 54.100 / 58. 54.100
 libavformat 58. 29.100 / 58. 29.100
 libavdevice 58. 8.100 / 58. 8.100
 libavfilter 7. 57.100 / 7. 57.100
 libavresample 4. 0. 0 / 4. 0. 0
 libswscale 5. 5.100 / 5. 5.100
 libswresample 3. 5.100 / 3. 5.100
 libpostproc 55. 5.100 / 55. 5.100
Input #0, image2, from '*.png':
 Duration: 00:16:39.00, start: 0.000000, bitrate: N/A
 Stream #0:0: Video: png, rgba(pc), 895x332 [SAR 3937:3937 DAR 895:332], 1 fps, 1 tbr, 1 tbn, 1 tbc
Stream mapping:
 Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x55726ab95d00] using SAR=1/1
[libx264 @ 0x55726ab95d00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 AVX512
[libx264 @ 0x55726ab95d00] profile High, level 2.2
[libx264 @ 0x55726ab95d00] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=10 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=1 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'out.mp4':
 Metadata:
 encoder : Lavf58.29.100
 Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 894x332 [SAR 1:1 DAR 447:166], q=-1--1, 1 fps, 16384 tbn, 1 tbc
 Metadata:
 encoder : Lavc58.54.100 libx264
 Side data:
 cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame= 173 fps=0.0 q=17.0 size= 512kB time=00:01:56.00 bitrate= 36.2kbits/frame= 351 fps=350 q=17.0 size= 1536kB time=00:04:54.00 bitrate= 42.8kbits/frame= 517 fps=343 q=17.0 size= 2560kB time=00:07:40.00 bitrate= 45.6kbits/frame= 725 fps=361 q=17.0 size= 3328kB time=00:11:08.00 bitrate= 40.8kbits/frame= 913 fps=364 q=17.0 size= 4352kB time=00:14:16.00 bitrate= 41.6kbits/frame= 999 fps=361 q=-1.0 Lsize= 4986kB time=00:16:36.00 bitrate= 41.0kbits/s speed= 360x 
video:4974kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.241361%
[libx264 @ 0x55726ab95d00] frame I:4 Avg QP: 6.12 size: 24072
[libx264 @ 0x55726ab95d00] frame P:346 Avg QP:12.94 size: 5708
[libx264 @ 0x55726ab95d00] frame B:649 Avg QP:18.19 size: 4655
[libx264 @ 0x55726ab95d00] consecutive B-frames: 5.8% 16.0% 20.1% 58.1%
[libx264 @ 0x55726ab95d00] mb I I16..4: 59.1% 10.6% 30.4%
[libx264 @ 0x55726ab95d00] mb P I16..4: 5.6% 0.6% 2.2% P16..4: 10.5% 4.3% 2.3% 0.0% 0.0% skip:74.5%
[libx264 @ 0x55726ab95d00] mb B I16..4: 2.2% 0.1% 1.7% B16..8: 16.9% 4.8% 1.6% direct: 1.1% skip:71.5% L0:50.9% L1:45.2% BI: 3.9%
[libx264 @ 0x55726ab95d00] 8x8 transform intra:5.9% inter:10.4%
[libx264 @ 0x55726ab95d00] coded y,uvDC,uvAC intra: 20.1% 18.3% 17.3% inter: 4.7% 4.7% 4.6%
[libx264 @ 0x55726ab95d00] i16 v,h,dc,p: 66% 33% 1% 0%
[libx264 @ 0x55726ab95d00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 18% 8% 73% 0% 0% 0% 0% 0% 0%
[libx264 @ 0x55726ab95d00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 31% 31% 2% 3% 2% 4% 2% 3%
[libx264 @ 0x55726ab95d00] i8c dc,h,v,p: 73% 23% 3% 0%
[libx264 @ 0x55726ab95d00] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x55726ab95d00] ref P L0: 57.2% 1.5% 24.3% 17.0%
[libx264 @ 0x55726ab95d00] ref B L0: 69.6% 24.8% 5.6%
[libx264 @ 0x55726ab95d00] ref B L1: 92.4% 7.6%
[libx264 @ 0x55726ab95d00] kb/s:40.78



In my python script I tried the following solutions :


video_cmd = """ffmpeg -framerate 1 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" out.mp4"""

subprocess.run(shlex.split(video_cmd), shell=False, cwd=path_viz, stderr=subprocess.STDOUT, check=True, text=False)

subprocess.run(video_cmd, shell=True, cwd=path_viz, stderr=subprocess.STDOUT, check=True, text=False)



as well as the solution proposed for this similar question


subprocess.Popen(video_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)



None of them worked. Apparently, the right command is run (output of the
check_out
function) :

Command 'ffmpeg -y -framerate 1 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" out.mp4' returned non-zero exit status 1.



the first part of the job (up to
Stream mapping:
) is done also correctly :

fmpeg version 4.3 Copyright (c) 2000-2020 the FFmpeg developers
 built with gcc 7.3.0 (crosstool-NG 1.23.0.449-a04d0)
 configuration: --prefix=/home/rsghazanfari/anaconda3/envs/_cuda --cc=/opt/conda/conda-bld/ffmpeg_1597178665428/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc --disable-doc --disable-openssl --enable-avresample --enable-gnutls --enable-hardcoded-tables --enable-libfreetype --enable-libopenh264 --enable-pic --enable-pthreads --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libmp3lame
 libavutil 56. 51.100 / 56. 51.100
 libavcodec 58. 91.100 / 58. 91.100
 libavformat 58. 45.100 / 58. 45.100
 libavdevice 58. 10.100 / 58. 10.100
 libavfilter 7. 85.100 / 7. 85.100
 libavresample 4. 0. 0 / 4. 0. 0
 libswscale 5. 7.100 / 5. 7.100
 libswresample 3. 7.100 / 3. 7.100
Input #0, image2, from '*.png':
 Duration: 00:16:39.00, start: 0.000000, bitrate: N/A
 Stream #0:0: Video: png, rgba(pc), 895x332 [SAR 3937:3937 DAR 895:332], 1 fps, 1 tbr, 1 tbn, 1 tbc



but it then pops up the following error :


Unknown encoder 'libx264'
Traceback (most recent call last):
 File "/home/rsgh/anaconda3/envs/_cuda/lib/python3.10/code.py", line 90, in runcode
 exec(code, self.locals)
 File "<input />", line 1, in <module>
 File "/home/rsgh/anaconda3/envs/_cuda/lib/python3.10/subprocess.py", line 524, in run
 raise CalledProcessError(retcode, process.args,

subprocess.CalledProcessError: Command 'ffmpeg -y -framerate 1 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" out.mp4' returned non-zero exit status 1.
</module>


Any ideas of why this error is produced in python while in terminal it runs fine ? Thank you in advance.


PS :
ffmpeg -version
outputs :

ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
libavutil 56. 31.100 / 56. 31.100
libavcodec 58. 54.100 / 58. 54.100
libavformat 58. 29.100 / 58. 29.100
libavdevice 58. 8.100 / 58. 8.100
libavfilter 7. 57.100 / 7. 57.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 5.100 / 5. 5.100
libswresample 3. 5.100 / 3. 5.100
libpostproc 55. 5.100 / 55. 5.100



ubuntu version :


Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal