Recherche avancée

Médias (0)

Mot : - Tags -/formulaire

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (89)

  • MediaSPIP v0.2

    21 juin 2013, par

    MediaSPIP 0.2 est la première version de MediaSPIP stable.
    Sa date de sortie officielle est le 21 juin 2013 et est annoncée ici.
    Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
    Comme pour la version précédente, 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 (...)

  • Le profil des utilisateurs

    12 avril 2011, par

    Chaque utilisateur dispose d’une page de profil lui permettant de modifier ses informations personnelle. Dans le menu de haut de page par défaut, un élément de menu est automatiquement créé à l’initialisation de MediaSPIP, visible uniquement si le visiteur est identifié sur le site.
    L’utilisateur a accès à la modification de profil depuis sa page auteur, un lien dans la navigation "Modifier votre profil" est (...)

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

Sur d’autres sites (7912)

  • FFMpeg CUDA yuvj420p frame conversion to cv::Mat layers shifted

    26 février 2023, par AcidTonic

    I am trying to retrieve hardware decoded H264 frames from the cuda backend of ffmpeg and display them as a cv::Mat. I got decently far and was able to get color images but it seems the conversion is not quite right as the image I get has a green bar at the top and if you look closely the blue parts of the image are offset down and to the right a little bit making everything look a little wonky.

    


    Correct Image as shown by ffplay using the same driver
enter image description here
Image I am getting
enter image description here

    


    Here is the full source code in the hopes someone can help me to get the correct image here...

    


    #include &#xA;&#xA;#include &#xA;&#xA;#include &#xA;&#xA;#include &#xA;&#xA;#include &#xA;&#xA;#include &#xA;&#xA;#include <iostream>&#xA;&#xA;#include <fstream>&#xA;&#xA;#include <cstdlib>&#xA;&#xA;#include <chrono>&#xA;&#xA;#include <cstring>&#xA;&#xA;extern "C" {&#xA;&#xA;  //Linker errors if not inside extern. FFMPEG headers are not C&#x2B;&#x2B; aware&#xA;  #include <libavcodec></libavcodec>avcodec.h>&#xA;&#xA;  #include <libavformat></libavformat>avformat.h>&#xA;&#xA;  #include <libavutil></libavutil>pixdesc.h>&#xA;&#xA;  #include <libavutil></libavutil>hwcontext.h>&#xA;&#xA;  #include <libavutil></libavutil>opt.h>&#xA;&#xA;  #include <libavutil></libavutil>avassert.h>&#xA;&#xA;  #include <libavutil></libavutil>imgutils.h>&#xA;&#xA;}&#xA;&#xA;#include <iomanip>&#xA;&#xA;#include <string>&#xA;&#xA;#include <sstream>&#xA;&#xA;#include <opencv2></opencv2>opencv.hpp>&#xA;&#xA;#ifdef __cplusplus&#xA;extern "C" {&#xA;  #endif // __cplusplus&#xA;  #include <libavdevice></libavdevice>avdevice.h>&#xA;&#xA;  #include <libavfilter></libavfilter>avfilter.h>&#xA;&#xA;  #include <libavformat></libavformat>avio.h>&#xA;&#xA;  #include <libavutil></libavutil>avutil.h>&#xA;&#xA;  #include <libpostproc></libpostproc>postprocess.h>&#xA;&#xA;  #include <libswresample></libswresample>swresample.h>&#xA;&#xA;  #include <libswscale></libswscale>swscale.h>&#xA;&#xA;  #ifdef __cplusplus&#xA;} // end extern "C".&#xA;#endif // __cplusplus&#xA;&#xA;static AVBufferRef * hw_device_ctx = NULL;&#xA;static enum AVPixelFormat hw_pix_fmt;&#xA;static FILE * output_file_fd = NULL;&#xA;cv::Mat output_mat;&#xA;int bgr_size;&#xA;&#xA;static int hw_decoder_init(AVCodecContext * ctx,&#xA;  const enum AVHWDeviceType type) {&#xA;  int err = 0;&#xA;&#xA;  if ((err = av_hwdevice_ctx_create( &amp; hw_device_ctx, type,&#xA;      NULL, NULL, 0)) &lt; 0) {&#xA;    fprintf(stderr, "Failed to create specified HW device.\n");&#xA;    return err;&#xA;  }&#xA;  ctx -> hw_device_ctx = av_buffer_ref(hw_device_ctx);&#xA;&#xA;  return err;&#xA;}&#xA;&#xA;static enum AVPixelFormat get_hw_format(AVCodecContext * ctx,&#xA;  const enum AVPixelFormat * pix_fmts) {&#xA;  const enum AVPixelFormat * p;&#xA;&#xA;  for (p = pix_fmts;* p != -1; p&#x2B;&#x2B;) {&#xA;    if ( * p == hw_pix_fmt)&#xA;      return * p;&#xA;  }&#xA;&#xA;  fprintf(stderr, "Failed to get HW surface format.\n");&#xA;  return AV_PIX_FMT_NONE;&#xA;}&#xA;&#xA;static int decode_write(AVCodecContext * avctx, AVPacket * packet) {&#xA;  AVFrame * frame = NULL, * sw_frame = NULL;&#xA;  AVFrame * tmp_frame = NULL;&#xA;  uint8_t * buffer = NULL;&#xA;  int size;&#xA;  int ret = 0;&#xA;&#xA;  ret = avcodec_send_packet(avctx, packet);&#xA;  if (ret &lt; 0) {&#xA;    fprintf(stderr, "Error during decoding\n");&#xA;    return ret;&#xA;  }&#xA;&#xA;  while (1) {&#xA;    if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {&#xA;      fprintf(stderr, "Can not alloc frame\n");&#xA;      ret = AVERROR(ENOMEM);&#xA;      av_frame_free( &amp; frame);&#xA;      av_frame_free( &amp; sw_frame);&#xA;      av_freep( &amp; buffer);&#xA;      if (ret &lt; 0) {&#xA;        return ret;&#xA;      }&#xA;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_frame(avctx, frame);&#xA;    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;      av_frame_free( &amp; frame);&#xA;      av_frame_free( &amp; sw_frame);&#xA;      return 0;&#xA;    } else if (ret &lt; 0) {&#xA;      fprintf(stderr, "Error while decoding\n");&#xA;      av_frame_free( &amp; frame);&#xA;      av_frame_free( &amp; sw_frame);&#xA;      av_freep( &amp; buffer);&#xA;      if (ret &lt; 0) {&#xA;        return ret;&#xA;      }&#xA;&#xA;    }&#xA;&#xA;    if (frame -> format == hw_pix_fmt) {&#xA;      /* retrieve data from GPU to CPU */&#xA;      if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) &lt; 0) {&#xA;        fprintf(stderr, "Error transferring the data to system memory\n");&#xA;        av_frame_free( &amp; frame);&#xA;        av_frame_free( &amp; sw_frame);&#xA;        av_freep( &amp; buffer);&#xA;        if (ret &lt; 0) {&#xA;          return ret;&#xA;        }&#xA;&#xA;      }&#xA;      tmp_frame = sw_frame;&#xA;    } else {&#xA;      tmp_frame = frame;&#xA;    }&#xA;&#xA;    AVPixelFormat format_to_use = AV_PIX_FMT_YUVJ420P;&#xA;    cv::Mat mat_src = cv::Mat(sw_frame -> height &#x2B; (sw_frame -> height / 2), sw_frame -> width, CV_8UC1, sw_frame -> data[0]);&#xA;    cv::Mat out_mat;&#xA;    cv::cvtColor(mat_src, out_mat, cv::COLOR_YUV2RGB_NV21);&#xA;&#xA;    output_mat = out_mat;&#xA;&#xA;    if (output_mat.empty() == false) {&#xA;      cv::imshow("image", output_mat);&#xA;      cv::waitKey(1);&#xA;    }&#xA;&#xA;    av_frame_free( &amp; frame);&#xA;    av_frame_free( &amp; sw_frame);&#xA;    av_freep( &amp; buffer);&#xA;    return ret;&#xA;  }&#xA;}&#xA;&#xA;TEST_CASE("CUDAH264", "Tests hardware h264 decoding") {&#xA;&#xA;  AVFormatContext * input_ctx = NULL;&#xA;  int video_stream, ret;&#xA;  AVStream * video = NULL;&#xA;  AVCodecContext * decoder_ctx = NULL;&#xA;  AVCodec * decoder = NULL;&#xA;  AVPacket * packet = NULL;&#xA;  enum AVHWDeviceType type;&#xA;  int i;&#xA;&#xA;  std::string device_type = "cuda";&#xA;  std::string input_file = "rtsp://10.100.2.152"; //My H264 network stream here...&#xA;&#xA;  /* The stream data is below...&#xA;  Input #0, rtsp, from &#x27;rtsp://10.100.2.152&#x27;:&#xA;    Metadata:&#xA;      title           : VCP IPC Realtime stream&#xA;    Duration: N/A, start: 0.000000, bitrate: N/A&#xA;    Stream #0:0: Video: h264 (High), yuvj420p(pc, bt709, progressive), 1920x1080, 10 fps, 10 tbr, 90k tbn, 20 tbc&#xA;  */&#xA;&#xA;  type = av_hwdevice_find_type_by_name(device_type.c_str());&#xA;  if (type == AV_HWDEVICE_TYPE_NONE) {&#xA;    fprintf(stderr, "Device type %s is not supported.\n", device_type.c_str());&#xA;    fprintf(stderr, "Available device types:");&#xA;    while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)&#xA;      fprintf(stderr, " %s", av_hwdevice_get_type_name(type));&#xA;    fprintf(stderr, "\n");&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  packet = av_packet_alloc();&#xA;  if (!packet) {&#xA;    fprintf(stderr, "Failed to allocate AVPacket\n");&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  /* open the input file */&#xA;  if (avformat_open_input( &amp; input_ctx, input_file.c_str(), NULL, NULL) != 0) {&#xA;    fprintf(stderr, "Cannot open input file &#x27;%s&#x27;\n", input_file.c_str());&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  if (avformat_find_stream_info(input_ctx, NULL) &lt; 0) {&#xA;    fprintf(stderr, "Cannot find input stream information.\n");&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  av_dump_format(input_ctx, 0, input_file.c_str(), 0);&#xA;&#xA;  for (int i = 0; i &lt; input_ctx -> nb_streams; i&#x2B;&#x2B;) {&#xA;    auto pCodec = avcodec_find_decoder(input_ctx -> streams[i] -> codecpar -> codec_id);&#xA;    auto pCodecCtx = avcodec_alloc_context3(pCodec);&#xA;    avcodec_parameters_to_context(pCodecCtx, input_ctx -> streams[i] -> codecpar);&#xA;&#xA;    printf("Found Video stream with ID: %d\n", input_ctx -> streams[i] -> id);&#xA;    printf("\t Stream Index: %d\n", input_ctx -> streams[i] -> index);&#xA;&#xA;    AVCodecParameters * codecpar = input_ctx -> streams[i] -> codecpar;&#xA;    printf("\t Codec Type: %s\n", av_get_media_type_string(codecpar -> codec_type));&#xA;    printf("\t Side data count: %d\n", input_ctx -> streams[i] -> nb_side_data);&#xA;    printf("\t Pixel format: %i\n", input_ctx -> streams[i] -> codecpar -> format);&#xA;    printf("\t Pixel Format Name: %s\n", av_get_pix_fmt_name((AVPixelFormat) input_ctx -> streams[i] -> codecpar -> format));&#xA;    printf("\t Metadata count: %d\n", av_dict_count(input_ctx -> streams[i] -> metadata));&#xA;  }&#xA;&#xA;  /* find the video stream information */&#xA;  ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &amp; decoder, 0);&#xA;  if (ret &lt; 0) {&#xA;    fprintf(stderr, "Cannot find a video stream in the input file\n");&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  video_stream = ret;&#xA;&#xA;  for (i = 0;; i&#x2B;&#x2B;) {&#xA;    const AVCodecHWConfig * config = avcodec_get_hw_config(decoder, i);&#xA;    if (!config) {&#xA;      fprintf(stderr, "Decoder %s does not support device type %s.\n",&#xA;        decoder -> name, av_hwdevice_get_type_name(type));&#xA;      throw std::runtime_error("Error");&#xA;    }&#xA;    if (config -> methods &amp; AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &amp;&amp;&#xA;      config -> device_type == type) {&#xA;      hw_pix_fmt = config -> pix_fmt;&#xA;      break;&#xA;    }&#xA;  }&#xA;&#xA;  if (!(decoder_ctx = avcodec_alloc_context3(decoder))) {&#xA;    throw std::runtime_error("NO MEMORY");&#xA;  }&#xA;&#xA;  video = input_ctx -> streams[video_stream];&#xA;  if (avcodec_parameters_to_context(decoder_ctx, video -> codecpar) &lt; 0) {&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  decoder_ctx -> get_format = get_hw_format;&#xA;&#xA;  if (hw_decoder_init(decoder_ctx, type) &lt; 0) {&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) &lt; 0) {&#xA;    fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);&#xA;    throw std::runtime_error("Error");&#xA;  }&#xA;&#xA;  /* actual decoding and dump the raw data */&#xA;  while (ret >= 0) {&#xA;    if ((ret = av_read_frame(input_ctx, packet)) &lt; 0)&#xA;      break;&#xA;&#xA;    if (video_stream == packet -> stream_index)&#xA;      ret = decode_write(decoder_ctx, packet);&#xA;&#xA;    av_packet_unref(packet);&#xA;  }&#xA;&#xA;  /* flush the decoder */&#xA;  ret = decode_write(decoder_ctx, NULL);&#xA;&#xA;  if (output_file_fd) {&#xA;    fclose(output_file_fd);&#xA;  }&#xA;  av_packet_free( &amp; packet);&#xA;  avcodec_free_context( &amp; decoder_ctx);&#xA;  avformat_close_input( &amp; input_ctx);&#xA;  av_buffer_unref( &amp; hw_device_ctx);&#xA;&#xA;}&#xA;</sstream></string></iomanip></cstring></chrono></cstdlib></fstream></iostream>

    &#xA;

  • What permission ffmpeg-static need in AWS Lambda ?

    17 février 2023, par János

    I have this code. It download a image, made a video from it and upload it to S3. It runs on Lambda. Added packages, intalled, zipped, uploaded.

    &#xA;

    npm install --production&#xA;zip -r my-lambda-function.zip ./&#xA;

    &#xA;

    But get an error code 126

    &#xA;

    2023-02-17T09:27:55.236Z    5c845bb6-02c1-41b0-8759-4459591b57b0    INFO    Error: ffmpeg exited with code 126&#xA;    at ChildProcess.<anonymous> (/var/task/node_modules/fluent-ffmpeg/lib/processor.js:182:22)&#xA;    at ChildProcess.emit (node:events:513:28)&#xA;    at ChildProcess._handle.onexit (node:internal/child_process:291:12)&#xA;2023-02-17T09:27:55.236Z 5c845bb6-02c1-41b0-8759-4459591b57b0 INFO Error: ffmpeg exited with code 126 at ChildProcess.<anonymous> (/var/task/node_modules/fluent-ffmpeg/lib/processor.js:182:22) at ChildProcess.emit (node:events:513:28) at ChildProcess._handle.onexit (node:internal/child_process:291:12)&#xA;</anonymous></anonymous>

    &#xA;

    Do I need to set a specific premission for ffmpeg ?

    &#xA;

    import { PutObjectCommand, S3Client } from &#x27;@aws-sdk/client-s3&#x27;&#xA;import { fromNodeProviderChain } from &#x27;@aws-sdk/credential-providers&#x27;&#xA;import axios from &#x27;axios&#x27;&#xA;import pathToFfmpeg from &#x27;ffmpeg-static&#x27;&#xA;import ffmpeg from &#x27;fluent-ffmpeg&#x27;&#xA;import fs from &#x27;fs&#x27;&#xA;ffmpeg.setFfmpegPath(pathToFfmpeg)&#xA;const credentials = fromNodeProviderChain({&#xA;    clientConfig: {&#xA;        region: &#x27;eu-central-1&#x27;,&#xA;    },&#xA;})&#xA;const client = new S3Client({ credentials })&#xA;&#xA;export const handler = async (event, context) => {&#xA;    try {&#xA;        let body&#xA;        let statusCode = 200&#xA;        const query = event?.queryStringParameters&#xA;        if (!query?.imgId &amp;&amp; !query?.video1Id &amp;&amp; !query?.video2Id) {&#xA;            return&#xA;        }&#xA;&#xA;        const imgId = query?.imgId&#xA;        const video1Id = query?.video1Id&#xA;        const video2Id = query?.video2Id&#xA;        console.log(&#xA;            `Parameters received, imgId: ${imgId}, video1Id: ${video1Id}, video2Id: ${video2Id}`&#xA;        )&#xA;        const imgURL = getFileURL(imgId)&#xA;        const video1URL = getFileURL(`${video1Id}.mp4`)&#xA;        const video2URL = getFileURL(`${video2Id}.mp4`)&#xA;        const imagePath = `/tmp/${imgId}`&#xA;        const video1Path = `/tmp/${video1Id}.mp4`&#xA;        const video2Path = `/tmp/${video2Id}.mp4`&#xA;        const outputPath = `/tmp/${imgId}.mp4`&#xA;        await Promise.all([&#xA;            downloadFile(imgURL, imagePath),&#xA;            downloadFile(video1URL, video1Path),&#xA;            downloadFile(video2URL, video2Path),&#xA;        ])&#xA;        await new Promise((resolve, reject) => {&#xA;            console.log(&#x27;Input files downloaded&#x27;)&#xA;            ffmpeg()&#xA;                .input(imagePath)&#xA;                .inputFormat(&#x27;image2&#x27;)&#xA;                .inputFPS(30)&#xA;                .loop(1)&#xA;                .size(&#x27;1080x1080&#x27;)&#xA;                .videoCodec(&#x27;libx264&#x27;)&#xA;                .format(&#x27;mp4&#x27;)&#xA;                .outputOptions([&#xA;                    &#x27;-tune animation&#x27;,&#xA;                    &#x27;-pix_fmt yuv420p&#x27;,&#xA;                    &#x27;-profile:v baseline&#x27;,&#xA;                    &#x27;-level 3.0&#x27;,&#xA;                    &#x27;-preset medium&#x27;,&#xA;                    &#x27;-crf 23&#x27;,&#xA;                    &#x27;-movflags &#x2B;faststart&#x27;,&#xA;                    &#x27;-y&#x27;,&#xA;                ])&#xA;                .output(outputPath)&#xA;                .on(&#x27;end&#x27;, () => {&#xA;                    console.log(&#x27;Output file generated&#x27;)&#xA;                    resolve()&#xA;                })&#xA;                .on(&#x27;error&#x27;, (e) => {&#xA;                    console.log(e)&#xA;                    reject()&#xA;                })&#xA;                .run()&#xA;            &#xA;        })&#xA;        await uploadFile(outputPath, imgId &#x2B; &#x27;.mp4&#x27;)&#xA;            .then((url) => {&#xA;                body = JSON.stringify({&#xA;                    url,&#xA;                })&#xA;            })&#xA;            .catch((error) => {&#xA;                console.error(error)&#xA;                statusCode = 400&#xA;                body = error?.message ?? error&#xA;            })&#xA;        console.log(`File uploaded to S3`)&#xA;        const headers = {&#xA;            &#x27;Content-Type&#x27;: &#x27;application/json&#x27;,&#xA;            &#x27;Access-Control-Allow-Headers&#x27;: &#x27;Content-Type&#x27;,&#xA;            &#x27;Access-Control-Allow-Origin&#x27;: &#x27;https://tikex.com, https://borespiac.hu&#x27;,&#xA;            &#x27;Access-Control-Allow-Methods&#x27;: &#x27;GET&#x27;,&#xA;        }&#xA;        return {&#xA;            statusCode,&#xA;            body,&#xA;            headers,&#xA;        }&#xA;    } catch (error) {&#xA;        console.error(error)&#xA;        return {&#xA;            statusCode: 500,&#xA;            body: JSON.stringify(&#x27;Error fetching data&#x27;),&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;const downloadFile = async (url, path) => {&#xA;    try {&#xA;        console.log(`Download will start: ${url}`)&#xA;        const response = await axios(url, {&#xA;            responseType: &#x27;stream&#x27;,&#xA;        })&#xA;        if (response.status !== 200) {&#xA;            throw new Error(&#xA;                `Failed to download file, status code: ${response.status}`&#xA;            )&#xA;        }&#xA;        response.data&#xA;            .pipe(fs.createWriteStream(path))&#xA;            .on(&#x27;finish&#x27;, () => console.log(`File downloaded to ${path}`))&#xA;            .on(&#x27;error&#x27;, (e) => {&#xA;                throw new Error(`Failed to save file: ${e}`)&#xA;            })&#xA;    } catch (e) {&#xA;        console.error(`Error downloading file: ${e}`)&#xA;    }&#xA;}&#xA;const uploadFile = async (path, id) => {&#xA;    const buffer = fs.readFileSync(path)&#xA;    const params = {&#xA;        Bucket: &#x27;t44-post-cover&#x27;,&#xA;        ACL: &#x27;public-read&#x27;,&#xA;        Key: id,&#xA;        ContentType: &#x27;video/mp4&#x27;,&#xA;        Body: buffer,&#xA;    }&#xA;    await client.send(new PutObjectCommand(params))&#xA;    return getFileURL(id)&#xA;}&#xA;const getFileURL = (id) => {&#xA;    const bucket = &#x27;t44-post-cover&#x27;&#xA;    const url = `https://${bucket}.s3.eu-central-1.amazonaws.com/${id}`&#xA;    return url&#xA;}&#xA;

    &#xA;

    Added AWSLambdaBasicExecutionRole-16e770c8-05fa-4c42-9819-12c468cb5b49 permission, with policy :

    &#xA;

    {&#xA;    "Version": "2012-10-17",&#xA;    "Statement": [&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": "logs:CreateLogGroup",&#xA;            "Resource": "arn:aws:logs:eu-central-1:634617701827:*"&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "logs:CreateLogStream",&#xA;                "logs:PutLogEvents"&#xA;            ],&#xA;            "Resource": [&#xA;                "arn:aws:logs:eu-central-1:634617701827:log-group:/aws/lambda/promo-video-composer-2:*"&#xA;            ]&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "s3:GetObject",&#xA;                "s3:PutObject",&#xA;                "s3:ListBucket"&#xA;            ],&#xA;            "Resource": [&#xA;                "arn:aws:s3:::example-bucket",&#xA;                "arn:aws:s3:::example-bucket/*"&#xA;            ]&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "logs:CreateLogGroup",&#xA;                "logs:CreateLogStream",&#xA;                "logs:PutLogEvents"&#xA;            ],&#xA;            "Resource": [&#xA;                "arn:aws:logs:*:*:*"&#xA;            ]&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "ec2:DescribeNetworkInterfaces"&#xA;            ],&#xA;            "Resource": [&#xA;                "*"&#xA;            ]&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "sns:*"&#xA;            ],&#xA;            "Resource": [&#xA;                "*"&#xA;            ]&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "cloudwatch:*"&#xA;            ],&#xA;            "Resource": [&#xA;                "*"&#xA;            ]&#xA;        },&#xA;        {&#xA;            "Effect": "Allow",&#xA;            "Action": [&#xA;                "kms:Decrypt"&#xA;            ],&#xA;            "Resource": [&#xA;                "*"&#xA;            ]&#xA;        }&#xA;    ]&#xA;}&#xA;

    &#xA;

    What do I miss ?

    &#xA;

    janoskukoda@Janoss-MacBook-Pro promo-video-composer-2 % ls -l $(which ffmpeg)&#xA;lrwxr-xr-x  1 janoskukoda  admin  35 Feb 10 12:50 /opt/homebrew/bin/ffmpeg -> ../Cellar/ffmpeg/5.1.2_4/bin/ffmpeg&#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;