Recherche avancée

Médias (1)

Mot : - Tags -/biographie

Autres articles (71)

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

  • How can I fix a segmentation fault in a C program ? [duplicate]

    31 mars 2023, par ipegasus
    


    Possible Duplicate :
    
Segmentation fault

    


    


    Currently I am upgrading an open source program used for HTTP streaming. It needs to support the latest FFmpeg.
The code compiles fine without any warnings, although I am getting a segmentation fault error.

    


    How can I fix the issue ? And / or, what is the best way to debug ? Please find attached a portion of the code due to size. I will try to add the project to GitHub :)

    


    Sample Usage

    


    # segmenter --i out.ts --l 10 --o stream.m3u8 --d segments --f stream


    


    Makefile

    


    FFLIBS=`pkg-config --libs libavformat libavcodec libavutil`
FFFLAGS=`pkg-config --cflags libavformat libavcodec libavutil`

all:
    gcc -Wall -g segmenter.c -o segmenter ${FFFLAGS} ${FFLIBS}


    


    segmenter.c

    


    /*&#xA; * Copyright (c) 2009 Chase Douglas&#xA; *&#xA; * This program is free software; you can redistribute it and/or&#xA; * modify it under the terms of the GNU General Public License version 2&#xA; * as published by the Free Software Foundation.&#xA; *&#xA; * This program is distributed in the hope that it will be useful,&#xA; * but WITHOUT ANY WARRANTY; without even the implied warranty of&#xA; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&#xA; * GNU General Public License for more details.&#xA; *&#xA; * You should have received a copy of the GNU General Public License&#xA; * along with this program; if not, write to the Free Software&#xA; * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.&#xA; */&#xA;#include &#xA;#include &#xA;#include &#xA;#include &#xA;#include &#xA;#include "libavformat/avformat.h"&#xA;&#xA;#include "libavformat/avio.h"&#xA;&#xA;#include <sys></sys>stat.h>&#xA;&#xA;#include "segmenter.h"&#xA;#include "libavformat/avformat.h"&#xA;&#xA;#define IMAGE_ID3_SIZE 9171&#xA;&#xA;void printUsage() {&#xA;    fprintf(stderr, "\nExample: segmenter --i infile --d baseDir --f baseFileName --o playListFile.m3u8 --l 10 \n");&#xA;    fprintf(stderr, "\nOptions: \n");&#xA;    fprintf(stderr, "--i <infile>.\n");&#xA;    fprintf(stderr, "--o <outfile>.\n");&#xA;    fprintf(stderr, "--d basedir, the base directory for files.\n");&#xA;    fprintf(stderr, "--f baseFileName, output files will be baseFileName-#.\n");&#xA;    fprintf(stderr, "--l segment length, the length of each segment.\n");&#xA;    fprintf(stderr, "--a,  audio only decode for &lt; 64k streams.\n");&#xA;    fprintf(stderr, "--v,  video only decode for &lt; 64k streams.\n");&#xA;    fprintf(stderr, "--version, print version details and exit.\n");&#xA;    fprintf(stderr, "\n\n");&#xA;}&#xA;&#xA;void ffmpeg_version() {&#xA;    // output build and version numbers&#xA;    fprintf(stderr, "  libavutil version:   %s\n", AV_STRINGIFY(LIBAVUTIL_VERSION));&#xA;    fprintf(stderr, "  libavutil build:     %d\n", LIBAVUTIL_BUILD);&#xA;    fprintf(stderr, "  libavcodec version:  %s\n", AV_STRINGIFY(LIBAVCODEC_VERSION));&#xA;    fprintf(stdout, "  libavcodec build:    %d\n", LIBAVCODEC_BUILD);&#xA;    fprintf(stderr, "  libavformat version: %s\n", AV_STRINGIFY(LIBAVFORMAT_VERSION));&#xA;    fprintf(stderr, "  libavformat build:   %d\n", LIBAVFORMAT_BUILD);&#xA;    fprintf(stderr, "  built on " __DATE__ " " __TIME__);&#xA;#ifdef __GNUC__&#xA;    fprintf(stderr, ", gcc: " __VERSION__ "\n");&#xA;#else&#xA;    fprintf(stderr, ", using a non-gcc compiler\n");&#xA;#endif&#xA;}&#xA;&#xA;&#xA;static AVStream *add_output_stream(AVFormatContext *output_format_context, AVStream *input_stream) {&#xA;    AVCodecContext *input_codec_context;&#xA;    AVCodecContext *output_codec_context;&#xA;    AVStream *output_stream;&#xA;&#xA;    output_stream = avformat_new_stream(output_format_context, 0);&#xA;    if (!output_stream) {&#xA;        fprintf(stderr, "Segmenter error: Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    input_codec_context = input_stream->codec;&#xA;    output_codec_context = output_stream->codec;&#xA;&#xA;    output_codec_context->codec_id = input_codec_context->codec_id;&#xA;    output_codec_context->codec_type = input_codec_context->codec_type;&#xA;    output_codec_context->codec_tag = input_codec_context->codec_tag;&#xA;    output_codec_context->bit_rate = input_codec_context->bit_rate;&#xA;    output_codec_context->extradata = input_codec_context->extradata;&#xA;    output_codec_context->extradata_size = input_codec_context->extradata_size;&#xA;&#xA;    if (av_q2d(input_codec_context->time_base) * input_codec_context->ticks_per_frame > av_q2d(input_stream->time_base) &amp;&amp; av_q2d(input_stream->time_base) &lt; 1.0 / 1000) {&#xA;        output_codec_context->time_base = input_codec_context->time_base;&#xA;        output_codec_context->time_base.num *= input_codec_context->ticks_per_frame;&#xA;    } else {&#xA;        output_codec_context->time_base = input_stream->time_base;&#xA;    }&#xA;&#xA;    switch (input_codec_context->codec_type) {&#xA;#ifdef USE_OLD_FFMPEG&#xA;        case CODEC_TYPE_AUDIO:&#xA;#else&#xA;        case AVMEDIA_TYPE_AUDIO:&#xA;#endif&#xA;            output_codec_context->channel_layout = input_codec_context->channel_layout;&#xA;            output_codec_context->sample_rate = input_codec_context->sample_rate;&#xA;            output_codec_context->channels = input_codec_context->channels;&#xA;            output_codec_context->frame_size = input_codec_context->frame_size;&#xA;            if ((input_codec_context->block_align == 1 &amp;&amp; input_codec_context->codec_id == CODEC_ID_MP3) || input_codec_context->codec_id == CODEC_ID_AC3) {&#xA;                output_codec_context->block_align = 0;&#xA;            } else {&#xA;                output_codec_context->block_align = input_codec_context->block_align;&#xA;            }&#xA;            break;&#xA;#ifdef USE_OLD_FFMPEG&#xA;        case CODEC_TYPE_VIDEO:&#xA;#else&#xA;        case AVMEDIA_TYPE_VIDEO:&#xA;#endif&#xA;            output_codec_context->pix_fmt = input_codec_context->pix_fmt;&#xA;            output_codec_context->width = input_codec_context->width;&#xA;            output_codec_context->height = input_codec_context->height;&#xA;            output_codec_context->has_b_frames = input_codec_context->has_b_frames;&#xA;&#xA;            if (output_format_context->oformat->flags &amp; AVFMT_GLOBALHEADER) {&#xA;                output_codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;&#xA;            }&#xA;            break;&#xA;        default:&#xA;            break;&#xA;    }&#xA;&#xA;    return output_stream;&#xA;}&#xA;&#xA;int write_index_file(const char index[], const char tmp_index[], const unsigned int planned_segment_duration, const unsigned int actual_segment_duration[],&#xA;        const char output_directory[], const char output_prefix[], const char output_file_extension[],&#xA;        const unsigned int first_segment, const unsigned int last_segment) {&#xA;    FILE *index_fp;&#xA;    char *write_buf;&#xA;    unsigned int i;&#xA;&#xA;    index_fp = fopen(tmp_index, "w");&#xA;    if (!index_fp) {&#xA;        fprintf(stderr, "Could not open temporary m3u8 index file (%s), no index file will be created\n", tmp_index);&#xA;        return -1;&#xA;    }&#xA;&#xA;    write_buf = malloc(sizeof (char) * 1024);&#xA;    if (!write_buf) {&#xA;        fprintf(stderr, "Could not allocate write buffer for index file, index file will be invalid\n");&#xA;        fclose(index_fp);&#xA;        return -1;&#xA;    }&#xA;&#xA;    unsigned int maxDuration = planned_segment_duration;&#xA;&#xA;    for (i = first_segment; i &lt;= last_segment; i&#x2B;&#x2B;)&#xA;        if (actual_segment_duration[i] > maxDuration)&#xA;            maxDuration = actual_segment_duration[i];&#xA;&#xA;&#xA;&#xA;    snprintf(write_buf, 1024, "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n", maxDuration);&#xA;&#xA;    if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {&#xA;        fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");&#xA;        free(write_buf);&#xA;        fclose(index_fp);&#xA;        return -1;&#xA;    }&#xA;&#xA;    for (i = first_segment; i &lt;= last_segment; i&#x2B;&#x2B;) {&#xA;        snprintf(write_buf, 1024, "#EXTINF:%u,\n%s-%u%s\n", actual_segment_duration[i], output_prefix, i, output_file_extension);&#xA;        if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {&#xA;            fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");&#xA;            free(write_buf);&#xA;            fclose(index_fp);&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;    snprintf(write_buf, 1024, "#EXT-X-ENDLIST\n");&#xA;    if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {&#xA;        fprintf(stderr, "Could not write last file and endlist tag to m3u8 index file\n");&#xA;        free(write_buf);&#xA;        fclose(index_fp);&#xA;        return -1;&#xA;    }&#xA;&#xA;    free(write_buf);&#xA;    fclose(index_fp);&#xA;&#xA;    return rename(tmp_index, index);&#xA;}&#xA;&#xA;int main(int argc, const char *argv[]) {&#xA;    //input parameters&#xA;    char inputFilename[MAX_FILENAME_LENGTH], playlistFilename[MAX_FILENAME_LENGTH], baseDirName[MAX_FILENAME_LENGTH], baseFileName[MAX_FILENAME_LENGTH];&#xA;    char baseFileExtension[5]; //either "ts", "aac" or "mp3"&#xA;    int segmentLength, outputStreams, verbosity, version;&#xA;&#xA;&#xA;&#xA;    char currentOutputFileName[MAX_FILENAME_LENGTH];&#xA;    char tempPlaylistName[MAX_FILENAME_LENGTH];&#xA;&#xA;&#xA;    //these are used to determine the exact length of the current segment&#xA;    double prev_segment_time = 0;&#xA;    double segment_time;&#xA;    unsigned int actual_segment_durations[2048];&#xA;    double packet_time = 0;&#xA;&#xA;    //new variables to keep track of output size&#xA;    double output_bytes = 0;&#xA;&#xA;    unsigned int output_index = 1;&#xA;    AVOutputFormat *ofmt;&#xA;    AVFormatContext *ic = NULL;&#xA;    AVFormatContext *oc;&#xA;    AVStream *video_st = NULL;&#xA;    AVStream *audio_st = NULL;&#xA;    AVCodec *codec;&#xA;    int video_index;&#xA;    int audio_index;&#xA;    unsigned int first_segment = 1;&#xA;    unsigned int last_segment = 0;&#xA;    int write_index = 1;&#xA;    int decode_done;&#xA;    int ret;&#xA;    int i;&#xA;&#xA;    unsigned char id3_tag[128];&#xA;    unsigned char * image_id3_tag;&#xA;&#xA;    size_t id3_tag_size = 73;&#xA;    int newFile = 1; //a boolean value to flag when a new file needs id3 tag info in it&#xA;&#xA;    if (parseCommandLine(inputFilename, playlistFilename, baseDirName, baseFileName, baseFileExtension, &amp;outputStreams, &amp;segmentLength, &amp;verbosity, &amp;version, argc, argv) != 0)&#xA;        return 0;&#xA;&#xA;    if (version) {&#xA;        ffmpeg_version();&#xA;        return 0;&#xA;    }&#xA;&#xA;&#xA;    fprintf(stderr, "%s %s\n", playlistFilename, tempPlaylistName);&#xA;&#xA;&#xA;    image_id3_tag = malloc(IMAGE_ID3_SIZE);&#xA;    if (outputStreams == OUTPUT_STREAM_AUDIO)&#xA;        build_image_id3_tag(image_id3_tag);&#xA;    build_id3_tag((char *) id3_tag, id3_tag_size);&#xA;&#xA;    snprintf(tempPlaylistName, strlen(playlistFilename) &#x2B; strlen(baseDirName) &#x2B; 1, "%s%s", baseDirName, playlistFilename);&#xA;    strncpy(playlistFilename, tempPlaylistName, strlen(tempPlaylistName));&#xA;    strncpy(tempPlaylistName, playlistFilename, MAX_FILENAME_LENGTH);&#xA;    strncat(tempPlaylistName, ".", 1);&#xA;&#xA;    //decide if this is an aac file or a mpegts file.&#xA;    //postpone deciding format until later&#xA;    /*    ifmt = av_find_input_format("mpegts");&#xA;    if (!ifmt)&#xA;    {&#xA;    fprintf(stderr, "Could not find MPEG-TS demuxer.\n");&#xA;    exit(1);&#xA;    } */&#xA;&#xA;    av_log_set_level(AV_LOG_DEBUG);&#xA;&#xA;    av_register_all();&#xA;    ret = avformat_open_input(&amp;ic, inputFilename, NULL, NULL);&#xA;    if (ret != 0) {&#xA;        fprintf(stderr, "Could not open input file %s. Error %d.\n", inputFilename, ret);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (avformat_find_stream_info(ic, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not read stream information.\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    oc = avformat_alloc_context();&#xA;    if (!oc) {&#xA;        fprintf(stderr, "Could not allocate output context.");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    video_index = -1;&#xA;    audio_index = -1;&#xA;&#xA;    for (i = 0; i &lt; ic->nb_streams &amp;&amp; (video_index &lt; 0 || audio_index &lt; 0); i&#x2B;&#x2B;) {&#xA;        switch (ic->streams[i]->codec->codec_type) {&#xA;&#xA;#ifdef USE_OLD_FFMPEG&#xA;            case CODEC_TYPE_VIDEO:&#xA;#else&#xA;            case AVMEDIA_TYPE_VIDEO:&#xA;#endif&#xA;                video_index = i;&#xA;                ic->streams[i]->discard = AVDISCARD_NONE;&#xA;                if (outputStreams &amp; OUTPUT_STREAM_VIDEO)&#xA;                    video_st = add_output_stream(oc, ic->streams[i]);&#xA;                break;&#xA;#ifdef USE_OLD_FFMPEG&#xA;            case CODEC_TYPE_AUDIO:&#xA;#else&#xA;            case AVMEDIA_TYPE_AUDIO:&#xA;#endif&#xA;                audio_index = i;&#xA;                ic->streams[i]->discard = AVDISCARD_NONE;&#xA;                if (outputStreams &amp; OUTPUT_STREAM_AUDIO)&#xA;                    audio_st = add_output_stream(oc, ic->streams[i]);&#xA;                break;&#xA;            default:&#xA;                ic->streams[i]->discard = AVDISCARD_ALL;&#xA;                break;&#xA;        }&#xA;    }&#xA;&#xA;    if (video_index == -1) {&#xA;        fprintf(stderr, "Stream must have video component.\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    //now that we know the audio and video output streams&#xA;    //we can decide on an output format.&#xA;    if (outputStreams == OUTPUT_STREAM_AUDIO) {&#xA;        //the audio output format should be the same as the audio input format&#xA;        switch (ic->streams[audio_index]->codec->codec_id) {&#xA;            case CODEC_ID_MP3:&#xA;                fprintf(stderr, "Setting output audio to mp3.");&#xA;                strncpy(baseFileExtension, ".mp3", strlen(".mp3"));&#xA;                ofmt = av_guess_format("mp3", NULL, NULL);&#xA;                break;&#xA;            case CODEC_ID_AAC:&#xA;                fprintf(stderr, "Setting output audio to aac.");&#xA;                ofmt = av_guess_format("adts", NULL, NULL);&#xA;                break;&#xA;            default:&#xA;                fprintf(stderr, "Codec id %d not supported.\n", ic->streams[audio_index]->id);&#xA;        }&#xA;        if (!ofmt) {&#xA;            fprintf(stderr, "Could not find audio muxer.\n");&#xA;            exit(1);&#xA;        }&#xA;    } else {&#xA;        ofmt = av_guess_format("mpegts", NULL, NULL);&#xA;        if (!ofmt) {&#xA;            fprintf(stderr, "Could not find MPEG-TS muxer.\n");&#xA;            exit(1);&#xA;        }&#xA;    }&#xA;    oc->oformat = ofmt;&#xA;&#xA;    if (outputStreams &amp; OUTPUT_STREAM_VIDEO &amp;&amp; oc->oformat->flags &amp; AVFMT_GLOBALHEADER) {&#xA;        oc->flags |= CODEC_FLAG_GLOBAL_HEADER;&#xA;    }&#xA;&#xA;&#xA;    /*  Deprecated: pass the options to avformat_write_header directly.&#xA;        if (av_set_parameters(oc, NULL) &lt; 0) {&#xA;            fprintf(stderr, "Invalid output format parameters.\n");&#xA;            exit(1);&#xA;        }&#xA;     */&#xA;&#xA;    av_dump_format(oc, 0, baseFileName, 1);&#xA;&#xA;&#xA;    //open the video codec only if there is video data&#xA;    if (video_index != -1) {&#xA;        if (outputStreams &amp; OUTPUT_STREAM_VIDEO)&#xA;            codec = avcodec_find_decoder(video_st->codec->codec_id);&#xA;        else&#xA;            codec = avcodec_find_decoder(ic->streams[video_index]->codec->codec_id);&#xA;        if (!codec) {&#xA;            fprintf(stderr, "Could not find video decoder, key frames will not be honored.\n");&#xA;        }&#xA;&#xA;        if (outputStreams &amp; OUTPUT_STREAM_VIDEO)&#xA;            ret = avcodec_open2(video_st->codec, codec, NULL);&#xA;        else&#xA;            avcodec_open2(ic->streams[video_index]->codec, codec, NULL);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Could not open video decoder, key frames will not be honored.\n");&#xA;        }&#xA;    }&#xA;&#xA;    snprintf(currentOutputFileName, strlen(baseDirName) &#x2B; strlen(baseFileName) &#x2B; strlen(baseFileExtension) &#x2B; 10, "%s%s-%u%s", baseDirName, baseFileName, output_index&#x2B;&#x2B;, baseFileExtension);&#xA;&#xA;    if (avio_open(&amp;oc->pb, currentOutputFileName, URL_WRONLY) &lt; 0) {&#xA;        fprintf(stderr, "Could not open &#x27;%s&#x27;.\n", currentOutputFileName);&#xA;        exit(1);&#xA;    }&#xA;    newFile = 1;&#xA;&#xA;    int r = avformat_write_header(oc,NULL);&#xA;    if (r) {&#xA;        fprintf(stderr, "Could not write mpegts header to first output file.\n");&#xA;        debugReturnCode(r);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    //no segment info is written here. This just creates the shell of the playlist file&#xA;    write_index = !write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);&#xA;&#xA;    do {&#xA;        AVPacket packet;&#xA;&#xA;        decode_done = av_read_frame(ic, &amp;packet);&#xA;&#xA;        if (decode_done &lt; 0) {&#xA;            break;&#xA;        }&#xA;&#xA;        if (av_dup_packet(&amp;packet) &lt; 0) {&#xA;            fprintf(stderr, "Could not duplicate packet.");&#xA;            av_free_packet(&amp;packet);&#xA;            break;&#xA;        }&#xA;&#xA;        //this time is used to check for a break in the segments&#xA;        //    if (packet.stream_index == video_index &amp;&amp; (packet.flags &amp; PKT_FLAG_KEY))&#xA;        //    {&#xA;        //    segment_time = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;&#xA;        //    }&#xA;#if USE_OLD_FFMPEG&#xA;        if (packet.stream_index == video_index &amp;&amp; (packet.flags &amp; PKT_FLAG_KEY))&#xA;#else&#xA;        if (packet.stream_index == video_index &amp;&amp; (packet.flags &amp; AV_PKT_FLAG_KEY))&#xA;#endif&#xA;        {&#xA;            segment_time = (double) packet.pts * ic->streams[video_index]->time_base.num / ic->streams[video_index]->time_base.den;&#xA;        }&#xA;        //  else if (video_index &lt; 0)&#xA;        //    {&#xA;        //        segment_time = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;&#xA;        //    }&#xA;&#xA;        //get the most recent packet time&#xA;        //this time is used when the time for the final segment is printed. It may not be on the edge of&#xA;        //of a keyframe!&#xA;        if (packet.stream_index == video_index)&#xA;            packet_time = (double) packet.pts * ic->streams[video_index]->time_base.num / ic->streams[video_index]->time_base.den; //(double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;&#xA;        else if (outputStreams &amp; OUTPUT_STREAM_AUDIO)&#xA;            packet_time = (double) audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;&#xA;        else&#xA;            continue;&#xA;        //start looking for segment splits for videos one half second before segment duration expires. This is because the&#xA;        //segments are split on key frames so we cannot expect all segments to be split exactly equally.&#xA;        if (segment_time - prev_segment_time >= segmentLength - 0.5) {&#xA;            fprintf(stderr, "looking to print index file at time %lf\n", segment_time);&#xA;            avio_flush(oc->pb);&#xA;            avio_close(oc->pb);&#xA;&#xA;            if (write_index) {&#xA;                actual_segment_durations[&#x2B;&#x2B;last_segment] = (unsigned int) rint(segment_time - prev_segment_time);&#xA;                write_index = !write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);&#xA;                fprintf(stderr, "Writing index file at time %lf\n", packet_time);&#xA;            }&#xA;&#xA;            struct stat st;&#xA;            stat(currentOutputFileName, &amp;st);&#xA;            output_bytes &#x2B;= st.st_size;&#xA;&#xA;            snprintf(currentOutputFileName, strlen(baseDirName) &#x2B; strlen(baseFileName) &#x2B; strlen(baseFileExtension) &#x2B; 10, "%s%s-%u%s", baseDirName, baseFileName, output_index&#x2B;&#x2B;, baseFileExtension);&#xA;            if (avio_open(&amp;oc->pb, currentOutputFileName, URL_WRONLY) &lt; 0) {&#xA;                fprintf(stderr, "Could not open &#x27;%s&#x27;\n", currentOutputFileName);&#xA;                break;&#xA;            }&#xA;&#xA;            newFile = 1;&#xA;            prev_segment_time = segment_time;&#xA;        }&#xA;&#xA;        if (outputStreams == OUTPUT_STREAM_AUDIO &amp;&amp; packet.stream_index == audio_index) {&#xA;            if (newFile &amp;&amp; outputStreams == OUTPUT_STREAM_AUDIO) {&#xA;                //add id3 tag info&#xA;                //fprintf(stderr, "adding id3tag to file %s\n", currentOutputFileName);&#xA;                //printf("%lf %lld %lld %lld %lld %lld %lf\n", segment_time, audio_st->pts.val, audio_st->cur_dts, audio_st->cur_pkt.pts, packet.pts, packet.dts, packet.dts * av_q2d(ic->streams[audio_index]->time_base) );&#xA;                fill_id3_tag((char*) id3_tag, id3_tag_size, packet.dts);&#xA;                avio_write(oc->pb, id3_tag, id3_tag_size);&#xA;                avio_write(oc->pb, image_id3_tag, IMAGE_ID3_SIZE);&#xA;                avio_flush(oc->pb);&#xA;                newFile = 0;&#xA;            }&#xA;&#xA;            packet.stream_index = 0; //only one stream in audio only segments&#xA;            ret = av_interleaved_write_frame(oc, &amp;packet);&#xA;        } else if (outputStreams &amp; OUTPUT_STREAM_VIDEO) {&#xA;            if (newFile) {&#xA;                //fprintf(stderr, "New File: %lld %lld %lld\n", packet.pts, video_st->pts.val, audio_st->pts.val);&#xA;                //printf("%lf %lld %lld %lld %lld %lld %lf\n", segment_time, audio_st->pts.val, audio_st->cur_dts, audio_st->cur_pkt.pts, packet.pts, packet.dts, packet.dts * av_q2d(ic->streams[audio_index]->time_base) );&#xA;                newFile = 0;&#xA;            }&#xA;            if (outputStreams == OUTPUT_STREAM_VIDEO)&#xA;                ret = av_write_frame(oc, &amp;packet);&#xA;            else&#xA;                ret = av_interleaved_write_frame(oc, &amp;packet);&#xA;        }&#xA;&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Warning: Could not write frame of stream.\n");&#xA;        } else if (ret > 0) {&#xA;            fprintf(stderr, "End of stream requested.\n");&#xA;            av_free_packet(&amp;packet);&#xA;            break;&#xA;        }&#xA;&#xA;        av_free_packet(&amp;packet);&#xA;    } while (!decode_done);&#xA;&#xA;    //make sure all packets are written and then close the last file.&#xA;    avio_flush(oc->pb);&#xA;    av_write_trailer(oc);&#xA;&#xA;    if (video_st &amp;&amp; video_st->codec)&#xA;        avcodec_close(video_st->codec);&#xA;&#xA;    if (audio_st &amp;&amp; audio_st->codec)&#xA;        avcodec_close(audio_st->codec);&#xA;&#xA;    for (i = 0; i &lt; oc->nb_streams; i&#x2B;&#x2B;) {&#xA;        av_freep(&amp;oc->streams[i]->codec);&#xA;        av_freep(&amp;oc->streams[i]);&#xA;    }&#xA;&#xA;    avio_close(oc->pb);&#xA;    av_free(oc);&#xA;&#xA;    struct stat st;&#xA;    stat(currentOutputFileName, &amp;st);&#xA;    output_bytes &#x2B;= st.st_size;&#xA;&#xA;&#xA;    if (write_index) {&#xA;        actual_segment_durations[&#x2B;&#x2B;last_segment] = (unsigned int) rint(packet_time - prev_segment_time);&#xA;&#xA;        //make sure that the last segment length is not zero&#xA;        if (actual_segment_durations[last_segment] == 0)&#xA;            actual_segment_durations[last_segment] = 1;&#xA;&#xA;        write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);&#xA;&#xA;    }&#xA;&#xA;    write_stream_size_file(baseDirName, baseFileName, output_bytes * 8 / segment_time);&#xA;&#xA;    return 0;&#xA;}&#xA;</outfile></infile>

    &#xA;

  • dyld[16458] : Library not loaded : @rpath/libavcodec.framework/libavcodec while running my flutter app on iOS

    31 janvier 2023, par Stéphane de Luca

    Compilation is successful. But When I run the code, I get this.

    &#xA;

    Here is the full trace.

    &#xA;

    Any idea ?

    &#xA;

    dyld[16458]: Library not loaded: @rpath/libavcodec.framework/libavcodec&#xA;  Referenced from: &lt;3500F5CF-B1D2-30EC-8D7F-1C29BD45D05E> /private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Runner&#xA;  Reason: tried: &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/preboot/Cryptexes/OS@rpath/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/System/Library/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache)&#xA;Library not loaded: @rpath/libavcodec.framework/libavcodec&#xA;  Referenced from: &lt;3500F5CF-B1D2-30EC-8D7F-1C29BD45D05E> /private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Runner&#xA;  Reason: tried: &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2, not in dyld cache), &#x27;/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec&#x27; (errno=2), &#x27;/usr/lib/swif&#xA;dyld config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/usr/lib/libBacktraceRecording.dylib:/usr/lib/libMainThreadChecker.dylib:/usr/lib/libRPAC.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib&#xA;(lldb) &#xA;

    &#xA;

    The pod is as follows :

    &#xA;

    # Uncomment this line to define a global platform for your project&#xA;platform :ios, &#x27;14.0&#x27;&#xA;&#xA;# CocoaPods analytics sends network stats synchronously affecting flutter build latency.&#xA;ENV[&#x27;COCOAPODS_DISABLE_STATS&#x27;] = &#x27;true&#x27;&#xA;&#xA;project &#x27;Runner&#x27;, {&#xA;  &#x27;Debug&#x27; => :debug,&#xA;  &#x27;Profile&#x27; => :release,&#xA;  &#x27;Release&#x27; => :release,&#xA;}&#xA;&#xA;def flutter_root&#xA;  generated_xcode_build_settings_path = File.expand_path(File.join(&#x27;..&#x27;, &#x27;Flutter&#x27;, &#x27;Generated.xcconfig&#x27;), __FILE__)&#xA;  unless File.exist?(generated_xcode_build_settings_path)&#xA;    raise "#{generated_xcode_build_settings_path} must exist. If you&#x27;re running pod install manually, make sure flutter pub get is executed first"&#xA;  end&#xA;&#xA;  File.foreach(generated_xcode_build_settings_path) do |line|&#xA;    matches = line.match(/FLUTTER_ROOT\=(.*)/)&#xA;    return matches[1].strip if matches&#xA;  end&#xA;  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"&#xA;end&#xA;&#xA;require File.expand_path(File.join(&#x27;packages&#x27;, &#x27;flutter_tools&#x27;, &#x27;bin&#x27;, &#x27;podhelper&#x27;), flutter_root)&#xA;&#xA;flutter_ios_podfile_setup&#xA;&#xA;target &#x27;Runner&#x27; do&#xA;  use_frameworks!&#xA;  use_modular_headers!&#xA;&#xA;  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))&#xA;end&#xA;&#xA;post_install do |installer|&#xA;  installer.pods_project.targets.each do |target|&#xA;    flutter_additional_ios_build_settings(target)&#xA;  end&#xA;end&#xA;

    &#xA;

    The yaml :

    &#xA;

    &#xA;environment:&#xA;  sdk: &#x27;>=2.18.2 &lt;3.0.0&#x27;&#xA;&#xA;# Dependencies specify other packages that your package needs in order to work.&#xA;# To automatically upgrade your package dependencies to the latest versions&#xA;# consider running `flutter pub upgrade --major-versions`. Alternatively,&#xA;# dependencies can be manually updated by changing the version numbers below to&#xA;# the latest version available on pub.dev. To see which dependencies have newer&#xA;# versions available, run `flutter pub outdated`.&#xA;dependencies:&#xA;  flutter:&#xA;    sdk: flutter&#xA;  flutter_localizations:&#xA;    sdk: flutter&#xA;  # The following adds the Cupertino Icons font to your application.&#xA;  # Use with the CupertinoIcons class for iOS style icons.&#xA;  cupertino_icons: ^1.0.2&#xA;  video_editor: ^1.5.2&#xA;  image_picker: ^0.8.6&#xA;  helpers: ^1.2.0&#xA;  intl: ^0.17.0&#xA;  i18n_extension: ^5.0.1&#xA;  optimized_cached_image: ^3.0.1&#xA;  youtube_player_flutter: ^8.1.1&#xA;  flutter_launcher_icons: ^0.11.0&#xA;  flutter_lorem: ^2.0.0&#xA;  advance_image_picker: ^0.1.7&#x2B;1&#xA;  wechat_assets_picker: ^8.1.4&#xA;  lecle_flutter_absolute_path: ^0.0.2&#x2B;1&#xA;  #ffmpeg_kit_flutter: 5.1.0-LTS&#xA;  path_provider: ^2.0.11&#xA;  video_thumbnail: ^0.5.3&#xA;  flutter_document_picker: ^5.1.0&#xA;  flutter_login: ^4.1.1&#xA;  #flutter_absolute_path: ^1.0.6&#xA;  # flutter_absolute_path:&#xA;  #     git:&#xA;  #       url: https://github.com/ValeriusGC/flutter_absolute_path.git&#xA;  uuid: ^3.0.6&#xA;  flutter_form_builder: ^7.7.0&#xA;  form_builder_validators: ^8.4.0&#xA;  state_persistence: ^0.1.0&#xA;  shared_preferences: ^2.0.15&#xA;  firebase_core: ^2.4.0&#xA;  firebase_storage: ^11.0.8&#xA;  video_compress: ^3.1.2&#xA;  connectivity_plus: ^3.0.2&#xA;  internet_connection_checker: ^1.0.0&#x2B;1&#xA;  cached_video_player: ^2.0.3&#xA;  visibility_detector: ^0.3.3&#xA;  firebase_database: ^10.0.7&#xA;  firebase_auth: ^4.2.1&#xA;  firebase_dynamic_links: ^5.0.9&#xA;  cloud_firestore: ^4.2.0&#xA;  cloud_functions: ^4.0.6&#xA;  cached_network_image: ^3.2.3&#xA;  ffmpeg_kit_flutter_min_gpl: ^5.1.0&#xA;  video_player: ^2.4.10&#xA;  provider: ^6.0.5&#xA;  camera: ^0.9.8&#x2B;1&#xA;  share_plus: ^6.3.0&#xA;  package_info_plus: ^3.0.2&#xA;&#xA;dependency_overrides:&#xA;  ffmpeg_kit_flutter_min_gpl: ^5.1.0-LTS&#xA;&#xA;dev_dependencies:&#xA;  flutter_test:&#xA;    sdk: flutter&#xA;&#xA;&#xA;  # The "flutter_lints" package below contains a set of recommended lints to&#xA;  # encourage good coding practices. The lint set provided by the package is&#xA;  # activated in the `analysis_options.yaml` file located at the root of your&#xA;  # package. See that file for information about deactivating specific lint&#xA;  # rules and activating additional ones.&#xA;  flutter_lints: ^2.0.0&#xA;&#xA;``&#xA;

    &#xA;

  • Node.js readable maximize throughput/performance for compute intense readable - Writable doesn't pull data fast enough

    31 décembre 2022, par flohall

    General setup

    &#xA;

    I developed an application using AWS Lambda node.js 14.&#xA;I use a custom Readable implementation FrameCreationStream that uses node-canvas to draw images, svgs and more on a canvas. This result is then extracted as a raw image buffer in BGRA. A single image buffer contains 1920 * 1080 * 4 Bytes = 8294400 Bytes 8 MB.&#xA;This is then piped to stdin of a child_process running ffmpeg.&#xA;The highWaterMark of my Readable in objectMode:true is set to 25 so that the internal buffer can use up to 8 MB * 25 = 200 MB.

    &#xA;

    All this works fine and also doesn't contain too much RAM. But I noticed after some time, that the performance is not ideally.

    &#xA;

    Performance not optimal

    &#xA;

    I have an example input that generates a video of 315 frames. If I set highWaterMark to a value above 25 the performance increases to the point, when I set to a value of 315 or above.

    &#xA;

    For some reason ffmpeg doesn't start to pull any data until highWaterMark is reached. Obviously thats not what I want. ffmpeg should always consume data if minimum 1 frame is cached in the Readable and if it has finished processing the frame before. And the Readable should produce more frames as long highWaterMark isn't reached or the last frame has been reached. So ideally the Readable and the Writeable are busy all the time.

    &#xA;

    I found another way to improve the speed. If I add a timeout in the _read() method of the Readable after let's say every tenth frame for 100 ms. Then the ffmpeg-Writable will use this timeout to write some frames to ffmpeg.

    &#xA;

    It seems like frames aren't passed to ffmpeg during frame creation because some node.js main thread is busy ?

    &#xA;

    The fastest result I have if I increase highWaterMark above the amount of frames - which doesn't work for longer videos as this would make the AWS Lambda RAM explode. And this makes the whole streaming idea useless. Using timeouts always gives me stomach pain. Also depending on the execution on different environments a good fitting timeout might differ. Any ideas ?

    &#xA;

    FrameCreationStream

    &#xA;

    import canvas from &#x27;canvas&#x27;;&#xA;import {Readable} from &#x27;stream&#x27;;&#xA;import {IMAGE_STREAM_BUFFER_SIZE, PerformanceUtil, RenderingLibraryError, VideoRendererInput} from &#x27;vm-rendering-backend-commons&#x27;;&#xA;import {AnimationAssets, BufferType, DrawingService, FullAnimationData} from &#x27;vm-rendering-library&#x27;;&#xA;&#xA;/**&#xA; * This is a proper back pressure compatible implementation of readable for a having a stream to read single frames from.&#xA; * Whenever read() is called a new frame is created and added to the stream.&#xA; * read() will be called internally until options.highWaterMark has been reached.&#xA; * then calling read will be paused until one frame is read from the stream.&#xA; */&#xA;export class FrameCreationStream extends Readable {&#xA;&#xA;    drawingService: DrawingService;&#xA;    endFrameIndex: number;&#xA;    currentFrameIndex: number = 0;&#xA;    startFrameIndex: number;&#xA;    frameTimer: [number, number];&#xA;    readTimer: [number, number];&#xA;    fullAnimationData: FullAnimationData;&#xA;&#xA;    constructor(animationAssets: AnimationAssets, fullAnimationData: FullAnimationData, videoRenderingInput: VideoRendererInput, frameTimer: [number, number]) {&#xA;        super({highWaterMark: IMAGE_STREAM_BUFFER_SIZE, objectMode: true});&#xA;&#xA;        this.frameTimer = frameTimer;&#xA;        this.readTimer = PerformanceUtil.startTimer();&#xA;&#xA;        this.fullAnimationData = fullAnimationData;&#xA;&#xA;        this.startFrameIndex = Math.floor(videoRenderingInput.startFrameId);&#xA;        this.currentFrameIndex = this.startFrameIndex;&#xA;        this.endFrameIndex = Math.floor(videoRenderingInput.endFrameId);&#xA;&#xA;        this.drawingService = new DrawingService(animationAssets, fullAnimationData, videoRenderingInput, canvas);&#xA;        console.time("read");&#xA;    }&#xA;&#xA;    /**&#xA;     * this method is only overwritten for debugging&#xA;     * @param size&#xA;     */&#xA;    read(size?: number): string | Buffer {&#xA;&#xA;        console.log("read("&#x2B;size&#x2B;")");&#xA;        const buffer = super.read(size);&#xA;        console.log(buffer);&#xA;        console.log(buffer?.length);&#xA;        if(buffer) {&#xA;            console.timeLog("read");&#xA;        }&#xA;        return buffer;&#xA;    }&#xA;&#xA;    // _read() will be called when the stream wants to pull more data in.&#xA;    // _read() will be called again after each call to this.push(dataChunk) once the stream is ready to accept more data. https://nodejs.org/api/stream.html#readable_readsize&#xA;    // this way it is ensured, that even though this.createImageBuffer() is async, only one frame is created at a time and the order is kept&#xA;    _read(): void {&#xA;        // as frame numbers are consecutive and unique, we have to draw each frame number (also the first and the last one)&#xA;        if (this.currentFrameIndex &lt;= this.endFrameIndex) {&#xA;            PerformanceUtil.logTimer(this.readTimer, &#x27;WAIT   -> READ\t&#x27;);&#xA;            this.createImageBuffer()&#xA;                 .then(buffer => this.optionalTimeout(buffer))&#xA;                // push means adding a buffered raw frame to the stream&#xA;                .then((buffer: Buffer) => {&#xA;                    this.readTimer = PerformanceUtil.startTimer();&#xA;                    // the following two frame numbers start with 1 as first value&#xA;                    const processedFrameNumberOfScene = 1 &#x2B; this.currentFrameIndex - this.startFrameIndex;&#xA;                    const totalFrameNumberOfScene = 1 &#x2B; this.endFrameIndex - this.startFrameIndex;&#xA;                    // the overall frameId or frameIndex starts with frameId 0&#xA;                    const processedFrameIndex = this.currentFrameIndex;&#xA;                    this.currentFrameIndex&#x2B;&#x2B;;&#xA;                    this.push(buffer); // nothing besides logging should happen after calling this.push(buffer)&#xA;                    console.log(processedFrameNumberOfScene &#x2B; &#x27; of &#x27; &#x2B; totalFrameNumberOfScene &#x2B; &#x27; processed - full video frameId: &#x27; &#x2B; processedFrameIndex &#x2B; &#x27; - buffered frames: &#x27; &#x2B; this.readableLength);&#xA;                })&#xA;                .catch(err => {&#xA;                    // errors will be finally handled, when subscribing to frameCreation stream in ffmpeg service&#xA;                    // this log is just generated for tracing errors and if for some reason the handling in ffmpeg service doesn&#x27;t work&#xA;                    console.log("createImageBuffer: ", err);&#xA;                    this.emit("error", err);&#xA;                });&#xA;        } else {&#xA;            // push(null) makes clear that this stream has ended&#xA;            this.push(null);&#xA;            PerformanceUtil.logTimer(this.frameTimer, &#x27;FRAME_STREAM&#x27;);&#xA;        }&#xA;    }&#xA;&#xA;    private optionalTimeout(buffer: Buffer): Promise<buffer> {&#xA;        if(this.currentFrameIndex % 10 === 0) {&#xA;            return new Promise(resolve => setTimeout(() => resolve(buffer), 140));&#xA;        }&#xA;        return Promise.resolve(buffer);&#xA;    }&#xA;&#xA;    // prevent memory leaks - without this lambda memory will increase with every call&#xA;    _destroy(): void {&#xA;        this.drawingService.destroyStage();&#xA;    }&#xA;&#xA;    /**&#xA;     * This creates a raw pixel buffer that contains a single frame of the video drawn by the rendering library&#xA;     *&#xA;     */&#xA;    public async createImageBuffer(): Promise<buffer> {&#xA;&#xA;        const drawTimer = PerformanceUtil.startTimer();&#xA;        try {&#xA;            await this.drawingService.drawForFrame(this.currentFrameIndex);&#xA;        } catch (err: any) {&#xA;            throw new RenderingLibraryError(err);&#xA;        }&#xA;&#xA;        PerformanceUtil.logTimer(drawTimer, &#x27;DRAW   -> FRAME\t&#x27;);&#xA;&#xA;        const bufferTimer = PerformanceUtil.startTimer();&#xA;        // Creates a raw pixel buffer, containing simple binary data&#xA;        // the exact same information (BGRA/screen ratio) has to be provided to ffmpeg, because ffmpeg cannot detect format for raw input&#xA;        const buffer = await this.drawingService.toBuffer(BufferType.RAW);&#xA;        PerformanceUtil.logTimer(bufferTimer, &#x27;CANVAS -> BUFFER&#x27;);&#xA;&#xA;        return buffer;&#xA;    }&#xA;}&#xA;</buffer></buffer>

    &#xA;

    FfmpegService

    &#xA;

    import {ChildProcess, execFile} from &#x27;child_process&#x27;;&#xA;import {Readable} from &#x27;stream&#x27;;&#xA;import {FPS, StageSize} from &#x27;vm-rendering-library&#x27;;&#xA;import {&#xA;    FfmpegError,&#xA;    LOCAL_MERGE_VIDEOS_TEXT_FILE, LOCAL_SOUND_FILE_PATH,&#xA;    LOCAL_VIDEO_FILE_PATH,&#xA;    LOCAL_VIDEO_SOUNDLESS_MERGE_FILE_PATH&#xA;} from "vm-rendering-backend-commons";&#xA;&#xA;/**&#xA; * This class bundles all ffmpeg usages for rendering one scene.&#xA; * FFmpeg is a console program which can transcode nearly all types of sounds, images and videos from one to another.&#xA; */&#xA;export class FfmpegService {&#xA;&#xA;    ffmpegPath: string = null;&#xA;&#xA;&#xA;    constructor(ffmpegPath: string) {&#xA;        this.ffmpegPath = ffmpegPath;&#xA;    }&#xA;&#xA;    /**&#xA;     * Convert a stream of raw images into an .mp4 video using the command line program ffmpeg.&#xA;     *&#xA;     * @param inputStream an input stream containing images in raw format BGRA&#xA;     * @param stageSize the size of a single frame in pixels (minimum is 2*2)&#xA;     * @param outputPath the filepath to write the resulting video to&#xA;     */&#xA;    public imageToVideo(inputStream: Readable, stageSize: StageSize, outputPath: string): Promise<void> {&#xA;        const args: string[] = [&#xA;            &#x27;-f&#x27;,&#xA;            &#x27;rawvideo&#x27;,&#xA;            &#x27;-r&#x27;,&#xA;            `${FPS}`,&#xA;            &#x27;-pix_fmt&#x27;,&#xA;            &#x27;bgra&#x27;,&#xA;            &#x27;-s&#x27;,&#xA;            `${stageSize.width}x${stageSize.height}`,&#xA;            &#x27;-i&#x27;,&#xA;            // input "-" means input will be passed via pipe (streamed)&#xA;            &#x27;-&#x27;,&#xA;            // codec that also QuickTime player can understand&#xA;            &#x27;-vcodec&#x27;,&#xA;            &#x27;libx264&#x27;,&#xA;            &#x27;-pix_fmt&#x27;,&#xA;            &#x27;yuv420p&#x27;,&#xA;            /*&#xA;                * "-movflags faststart":&#xA;                * metadata at beginning of file&#xA;                * needs more RAM&#xA;                * file will be broken, if not finished properly&#xA;                * higher application compatibility&#xA;                * better for browser streaming&#xA;            */&#xA;            &#x27;-movflags&#x27;,&#xA;            &#x27;faststart&#x27;,&#xA;            // "-preset ultrafast", //use this to speed up compression, but quality/compression ratio gets worse&#xA;            // don&#x27;t overwrite an existing file here,&#xA;            // but delete file in the beginning of execution index.ts&#xA;            // (this is better for local testing believe me)&#xA;            outputPath&#xA;        ];&#xA;&#xA;        return this.execFfmpegPromise(args, inputStream);&#xA;    }&#xA;&#xA;    private execFfmpegPromise(args: string[], inputStream?: Readable): Promise<void> {&#xA;        const ffmpegServiceSelf = this;&#xA;        return new Promise(function (resolve, reject) {&#xA;            const executionProcess: ChildProcess = execFile(ffmpegServiceSelf.ffmpegPath, args, (err) => {&#xA;                if (err) {&#xA;                    reject(new FfmpegError(err));&#xA;                } else {&#xA;                    console.log(&#x27;ffmpeg finished&#x27;);&#xA;                    resolve();&#xA;                }&#xA;            });&#xA;            if (inputStream) {&#xA;                // it&#x27;s important to listen on errors of input stream before piping it into the write stream&#xA;                // if we don&#x27;t do this here, we get an unhandled promise exception for every issue in the input stream&#xA;                inputStream.on("error", err => {&#xA;                    reject(err);&#xA;                });&#xA;                // don&#x27;t reject promise here as the error will also be thrown inside execFile and will contain more debugging info&#xA;                // this log is just generated for tracing errors and if for some reason the handling in execFile doesn&#x27;t work&#xA;                inputStream.pipe(executionProcess.stdin).on("error", err => console.log("pipe stream: " , err));&#xA;            }&#xA;        });&#xA;    }&#xA;}&#xA;</void></void>

    &#xA;