Recherche avancée

Médias (0)

Mot : - Tags -/signalement

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

Autres articles (63)

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

  • Configurer la prise en compte des langues

    15 novembre 2010, par

    Accéder à la configuration et ajouter des langues prises en compte
    Afin de configurer la prise en compte de nouvelles langues, il est nécessaire de se rendre dans la partie "Administrer" du site.
    De là, dans le menu de navigation, vous pouvez accéder à une partie "Gestion des langues" permettant d’activer la prise en compte de nouvelles langues.
    Chaque nouvelle langue ajoutée reste désactivable tant qu’aucun objet n’est créé dans cette langue. Dans ce cas, elle devient grisée dans la configuration et (...)

  • XMP PHP

    13 mai 2011, par

    Dixit Wikipedia, XMP signifie :
    Extensible Metadata Platform ou XMP est un format de métadonnées basé sur XML utilisé dans les applications PDF, de photographie et de graphisme. Il a été lancé par Adobe Systems en avril 2001 en étant intégré à la version 5.0 d’Adobe Acrobat.
    Étant basé sur XML, il gère un ensemble de tags dynamiques pour l’utilisation dans le cadre du Web sémantique.
    XMP permet d’enregistrer sous forme d’un document XML des informations relatives à un fichier : titre, auteur, historique (...)

Sur d’autres sites (7745)

  • C++ FFmpeg encode stream to mp4 wrong fps and kbs

    22 mars 2024, par elmomoJacky

    i'm new to FFmpeg, i'm trying to learn how to encode images to an mp4 using a stream.
But even tough i can generate a playable file, my file is "broken", it does does past 1sec, and if i check it's data with ffmpeg i can see that it's duration is accordly wrong with the kb/s fps value

    


    


    Duration : 00:00:00.01 .... 2635232 kb/s, 15463.09 fps.

    


    


    ffmpeg -i 25_5.mp4 -f ffmetadata 25_5.txt

    


    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '25_5.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    title           : Your Title Here
    encoder         : Lavf60.16.100
    comment         : Your Comment Here
    copyright       : Copyright Information
  Duration: 00:00:00.01, start: 0.000000, bitrate: 2557680 kb/s
  Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2635232 kb/s, 15463.09 fps, 15360 tbr, 15360 tbn, 30 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
File '25_5.txt' already exists. Overwrite? [y/N] y
Output #0, ffmetadata, to '25_5.txt':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    title           : Your Title Here
    copyright       : Copyright Information
    comment         : Your Comment Here
    encoder         : Lavf58.76.100


    


    Here is my cpp and header code (using visual studio and vcpkg install ffmpeg)
file ffmpeg_example.cpp

    


    &#xA;#include "ffmpeg_example.h"&#xA;&#xA;#include &#xA;#include &#xA;#include &#xA;#include <iostream>&#xA;#include <map>&#xA;&#xA;extern "C" {&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;&#xA;}&#xA;&#xA;#define COMMON_AV_FRAME_FORMAT AV_PIX_FMT_YUV420P &#xA;&#xA;ffmpeg_example::ffmpeg_example()&#xA;{&#xA;&#xA;}&#xA;&#xA;ffmpeg_example::~ffmpeg_example()&#xA;{&#xA;&#xA;}&#xA;&#xA;void ffmpeg_example::set_codec_params(AVFormatContext*&amp; fctx, AVCodecContext*&amp; codec_ctx, double width, double height, int fps, int bitrate) {&#xA;    std::cout &lt;&lt; "encoding video for width:" &lt;&lt; width &lt;&lt; ", height:" &lt;&lt; height &lt;&lt; ", fps:" &lt;&lt; fps &lt;&lt; ", bitrate:" &lt;&lt; bitrate &lt;&lt; std::endl;&#xA;    codec_ctx->codec_id = fctx->oformat->video_codec;&#xA;    codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    codec_ctx->width = width;&#xA;    codec_ctx->height = height;&#xA;    codec_ctx->gop_size = 12;&#xA;    codec_ctx->pix_fmt = COMMON_AV_FRAME_FORMAT;&#xA;    codec_ctx->framerate = AVRational{ fps, 1 };&#xA;    codec_ctx->time_base = AVRational{ 1, fps };&#xA;    if (bitrate) codec_ctx->bit_rate = bitrate;&#xA;    if (fctx->oformat->flags &amp; AVFMT_GLOBALHEADER) {&#xA;        codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;    }&#xA;}&#xA;&#xA;AVStream* ffmpeg_example::create_stream(AVFormatContext*&amp; fctx, AVCodecContext*&amp; codec_ctx) {&#xA;    AVStream* stream = avformat_new_stream(fctx, nullptr);&#xA;    avcodec_parameters_from_context(stream->codecpar, codec_ctx);&#xA;    stream->time_base = codec_ctx->time_base;&#xA;    //stream->r_frame_rate = codec_ctx->framerate;&#xA;    //stream->avg_frame_rate = stream->r_frame_rate;&#xA;    return stream;&#xA;}&#xA;&#xA;AVFrame* ffmpeg_example::create_frame(int width, int height) {&#xA;    AVFrame* frame = av_frame_alloc();&#xA;    frame->format = COMMON_AV_FRAME_FORMAT;&#xA;    frame->width = width;&#xA;    frame->height = height;&#xA;    av_frame_get_buffer(frame, 0);&#xA;    return frame;&#xA;}&#xA;&#xA;void ffmpeg_example::fill_fake_yuv_image(AVFrame* frame, int frame_index, int width, int height) {&#xA;    /* Ensure the data buffers are writable */&#xA;    av_frame_make_writable(frame);&#xA;&#xA;    // This is where you could load actual image data into the frame.&#xA;    // For demonstration, we fill the frame with a color.&#xA;    // Y&#xA;    for (int y = 0; y &lt; height; y&#x2B;&#x2B;) {&#xA;        for (int x = 0; x &lt; width; x&#x2B;&#x2B;) {&#xA;            frame->data[0][y * frame->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; frame_index * 3;&#xA;        }&#xA;    }&#xA;&#xA;    // Cb and Cr&#xA;    for (int y = 0; y &lt; height / 2; y&#x2B;&#x2B;) {&#xA;        for (int x = 0; x &lt; width / 2; x&#x2B;&#x2B;) {&#xA;            frame->data[1][y * frame->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; frame_index * 2;&#xA;            frame->data[2][y * frame->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; frame_index * 5;&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;int ffmpeg_example::initFfmpegcontext()&#xA;{&#xA;    int ret;&#xA;&#xA;&#xA;    // Initialize the AVFormatContext&#xA;    if (avformat_alloc_output_context2(&amp;output_format_context, nullptr, _ouput_format, nullptr) &lt; 0 || !output_format_context) {&#xA;        std::cout &lt;&lt; "Could not allocat output context." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;&#xA;    // Find the encoder&#xA;    encoder = avcodec_find_encoder(output_format_context->oformat->video_codec);&#xA;    if (!encoder) {&#xA;        std::cout &lt;&lt; "Could not find encoder." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Create a new codec context&#xA;    codec_context = avcodec_alloc_context3(encoder);&#xA;    if (!codec_context) {&#xA;        std::cout &lt;&lt; "Could not allocate codec context." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Set codec parameters&#xA;    set_codec_params(output_format_context, codec_context, 1920, 1080, video_fps, 0);&#xA;&#xA;    // Open the codec&#xA;    ret = avcodec_open2(codec_context, encoder, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        std::cout &lt;&lt; "Could not open encoder with context." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Create a new video stream&#xA;    video_stream = create_stream(output_format_context, codec_context);&#xA;    if (!video_stream) {&#xA;        std::cout &lt;&lt; "Could not create stream." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Initialize the IO context&#xA;    if (!(output_format_context->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        std::cout &lt;&lt; "Init IO, AVFMT_NOFILE not detected." &lt;&lt; std::endl;&#xA;        if (avio_open(&amp;output_format_context->pb, output, AVIO_FLAG_WRITE) &lt; 0) {&#xA;            std::cout &lt;&lt; "Could not open output file." &lt;&lt; std::endl;&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;    // Write the file header&#xA;    ret = avformat_write_header(output_format_context, nullptr);&#xA;    if (ret &lt; 0) {&#xA;        std::cout &lt;&lt; "Could not write file header." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Create a new frame&#xA;    frame = create_frame(codec_context->width, codec_context->height);&#xA;    if (!frame) {&#xA;        std::cout &lt;&lt; "Could not create frame." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    // Create a new packet&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        std::cout &lt;&lt; "Could not allocate packet." &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;&#xA;void ffmpeg_example::set_metadata() {&#xA;    // Ensure the format context exists&#xA;    if (!output_format_context) return;&#xA;    std::cout &lt;&lt; "Setting metadata." &lt;&lt; std::endl;&#xA;&#xA;    // Create or get the existing metadata dictionary&#xA;    AVDictionary** metadata = &amp;output_format_context->metadata;&#xA;&#xA;    // Set various metadata fields&#xA;    av_dict_set(metadata, "title", "Your Title Here", 0);&#xA;    av_dict_set(metadata, "author", "Author Name", 0);&#xA;    av_dict_set(metadata, "copyright", "Copyright Information", 0);&#xA;    av_dict_set(metadata, "comment", "Your Comment Here", 0);&#xA;    av_dict_set(metadata, "rating", "5", 0); // Assuming rating is a simple numeric value&#xA;&#xA;    // Note: The last parameter of av_dict_set is a flag; 0 means the entry will be added to the dictionary&#xA;    // if it doesn&#x27;t exist, or the existing entry will be updated if it does exist.&#xA;}&#xA;&#xA;int ffmpeg_example::start()&#xA;{&#xA;    int ret;&#xA;    if (initFfmpegcontext() &lt; 0) {&#xA;        std::cout &lt;&lt; "quitting" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    set_metadata();&#xA;    std::cout &lt;&lt; "filling stream with frames data." &lt;&lt; std::endl;&#xA;    // Write frames&#xA;    for (int i = 0; i &lt; video_fps * 4; i&#x2B;&#x2B;) { // 5 seconds at video_fps fps&#xA;        frame->pts = i;&#xA;        fill_fake_yuv_image(frame, i, codec_context->width, codec_context->height);&#xA;&#xA;        // Encode the image&#xA;        ret = avcodec_send_frame(codec_context, frame);&#xA;        while (ret >= 0) {&#xA;            ret = avcodec_receive_packet(codec_context, pkt);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;                break;&#xA;            }&#xA;            pkt->stream_index = video_stream->index;&#xA;            av_interleaved_write_frame(output_format_context, pkt);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;    }&#xA;&#xA;    std::cout &lt;&lt; "closing." &lt;&lt; std::endl;&#xA;    // Send a null frame to the encoder to flush it&#xA;    avcodec_send_frame(codec_context, NULL);&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(codec_context, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;            break;&#xA;        }&#xA;        pkt->stream_index = video_stream->index;&#xA;        av_interleaved_write_frame(output_format_context, pkt);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Write the file trailer&#xA;    av_write_trailer(output_format_context);&#xA;&#xA;    cleanup();&#xA;    std::cout &lt;&lt; "done." &lt;&lt; std::endl;&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;void ffmpeg_example::cleanup() {&#xA;    // Clean up&#xA;    avcodec_free_context(&amp;codec_context);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avio_closep(&amp;output_format_context->pb);&#xA;    avformat_free_context(output_format_context);&#xA;}&#xA;</map></iostream>

    &#xA;

    Header file :

    &#xA;

    #pragma once&#xA;&#xA;class AVFormatContext;&#xA;class AVCodecContext;&#xA;class AVStream;&#xA;class AVCodec;&#xA;class AVFrame;&#xA;class AVPacket;&#xA;class AVOutputFormat;&#xA;&#xA;class ffmpeg_example {&#xA;private:&#xA;    const AVCodec* encoder = nullptr;&#xA;    AVFormatContext* output_format_context = nullptr;&#xA;    AVCodecContext* codec_context = nullptr;&#xA;    AVStream* video_stream = nullptr;&#xA;    AVFrame* frame = nullptr;&#xA;    AVPacket* pkt = nullptr;&#xA;&#xA;    const char* output = "output.mp4";&#xA;    const char* _ouput_format = "mp4";&#xA;&#xA;    int video_fps = 30;&#xA;public:&#xA;&#xA;    ffmpeg_example();&#xA;    virtual ~ffmpeg_example();&#xA;&#xA;    int start();&#xA;private:&#xA;    int initFfmpegcontext();&#xA;    AVFrame* create_frame(int width, int height);&#xA;    void fill_fake_yuv_image(AVFrame* frame, int frame_index, int width, int height);&#xA;    void set_codec_params(AVFormatContext*&amp; fctx, AVCodecContext*&amp; codec_ctx, double width, double height, int fps, int bitrate);&#xA;    AVStream* create_stream(AVFormatContext*&amp; fctx, AVCodecContext*&amp; codec_ctx);&#xA;&#xA;    void cleanup();&#xA;    void set_metadata();&#xA;};&#xA;

    &#xA;

    You just have to call the start() function to make it work.

    &#xA;

    I understand that the problem come surely from a flag disabling my fps setting, or it's an incorrect param while generating the header, but i can't find which one.&#xA;I searched other similar examples (ffmpeg example like encode_video.c or stackoverflow post : video-too-fast, speed-encoding-problem, ffmpeg-create-mp4 )&#xA;But whatever change i make i cannot make it work (and chatgpt is as lost as me).

    &#xA;

    — edit

    &#xA;

    The only way i find to impact the fps of the ouputed video is to inverscodec_ctx->framerate = AVRational{ 1, fps }; but even with that, my video is marked 30sec (should be 5) and doesn't past 1sec.&#xA;I'M LOST, i can count the number of packet -> 150

    &#xA;

    &#xA;

    ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 output.mp4&#xA;150

    &#xA;

    &#xA;

    Each frame has the correct pts so that it know in which order to play, my stream has the same avg_frame_rate as my codec framerate.

    &#xA;

    I don't know what i do wrong

    &#xA;

  • How to use hardware acceleration for ffmpeg [closed]

    11 mars 2024, par Kevin Gilbert

    I am try to get hardware acceleration going on my Dell Inspiron AMD laptop. It appears that vaapi is installed but how to I use it in ffmpeg ?

    &#xA;

    For testing, all I want is to accelerate

    &#xA;

    ffmpeg -i input.ts output.mp4&#xA;

    &#xA;

    Currently, the unaccelerated command I am currently using is

    &#xA;

    ffmpeg -y -i input.ts -c:v libx264 -preset slower -tune film -filter_complex scale=1080:608 -sws_flags lanczos output.mp4&#xA;

    &#xA;

    BTW : Environment is fully up-to-date Fedora 39 with stock ffmpeg.

    &#xA;

  • Ffmpeg encoding too slow

    8 mars 2024, par Marc Cuadras

    I'm using a Python script to encode mp4 videos in various qualities, in my case 360 +720 +1080.&#xA;Ffmpeg encoding videos very slow, 1 video taking 2 hours or more, I'm using good dedicated server from hetzner (ryzen 5 3600 + 64gm ram), any suggestion to improve speed will be really appreciated

    &#xA;

    import os&#xA;import glob&#xA;from datetime import datetime&#xA;import subprocess&#xA;from rich import print&#xA;import time&#xA;import sys&#xA;import pymysql as sql&#xA;import move&#xA;&#xA;MYSQL_HOST = "127.0.0.1"&#xA;MYSQL_USER = ""&#xA;MYSQL_PASSWORD = ""&#xA;MYSQL_DB = ""&#xA;&#xA;BASE_PATH = &#x27;&#x27;&#xA;UPLOAD_PATH = os.path.join(BASE_PATH, &#x27;upload&#x27;)&#xA;UPLOAD_PATH2 = os.path.join(BASE_PATH, &#x27;upload2&#x27;)&#xA;VIDEO_PATH = os.path.join(BASE_PATH, &#x27;video&#x27;)&#xA;LOGO_PATH = os.path.join(BASE_PATH, &#x27;logo.png&#x27;)&#xA;ERROR_PATH = os.path.join(BASE_PATH, &#x27;error&#x27;)&#xA;RCLONE_PATH = os.path.join(BASE_PATH, &#x27;rclone&#x27;)&#xA;WAIT = 60&#xA;VIDEO_LENGTH = 10&#xA;MOVE_CMD = "screen -dmS move python3 move.py --folder {}"&#xA;&#xA;global current_video_id&#xA;&#xA;db = sql.connect(&#xA;        host=MYSQL_HOST,&#xA;        user=MYSQL_USER,&#xA;        password=MYSQL_PASSWORD,&#xA;        database=MYSQL_DB&#xA;    )&#xA;&#xA;def executedb(query):&#xA;    cursor = db.cursor()&#xA;    cursor.execute(query)&#xA;    db.commit()&#xA;&#xA;def time_now():&#xA;    now = datetime.now().strftime(&#x27;%Y-%m-%d %H:%M:%S&#x27;)&#xA;    return now&#xA;&#xA;def write_log(msg):&#xA;    log_msg = f"{time_now()}: {msg}"&#xA;    with open(&#x27;log.txt&#x27;,&#x27;a&#x27;) as f:&#xA;        f.write(log_msg)&#xA;        f.write(&#x27;\n&#x27;)&#xA;&#xA;def change_extension(files):&#xA;    for file in files:&#xA;        os.rename(file, file.split(&#x27;.&#x27;)[0] &#x2B; &#x27;.mkv&#x27;)&#xA;&#xA;def change_all_videos_extension():&#xA;    # changing all the vidoe&#x27;s extension to mkv format&#xA;    MKV_files = glob.glob(&#x27;*.MKV&#x27;)&#xA;    AVI_files = glob.glob(&#x27;*.AVI&#x27;)&#xA;    avi_files = glob.glob(&#x27;*.avi&#x27;)&#xA;    MP4_files = glob.glob(&#x27;*.MK4&#x27;)&#xA;    mp4_files = glob.glob(&#x27;*.mp4&#x27;)&#xA;    webm_files = glob.glob(&#x27;*.webm&#x27;)&#xA;    ts_files = glob.glob(&#x27;*.ts&#x27;)&#xA;&#xA;    if len(avi_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting avi videos to mkv format&#x27;)&#xA;        change_extension(avi_files)&#xA;&#xA;    if len(MKV_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting MKV videos to mkv format&#x27;)&#xA;        change_extension(MKV_files)&#xA;&#xA;    if len(AVI_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting AVI videos to mkv format&#x27;)&#xA;        change_extension(AVI_files)&#xA;&#xA;    if len(MP4_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting MP4 videos to mkv format&#x27;)&#xA;        change_extension(MP4_files)&#xA;&#xA;    if len(mp4_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting mp4 videos to mkv format&#x27;)&#xA;        change_extension(mp4_files)&#xA;&#xA;    if len(webm_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting webm videos to mkv format&#x27;)&#xA;        change_extension(webm_files)&#xA;&#xA;    if len(ts_files) > 0:&#xA;        print(f&#x27;[{time_now()}] Converting ts videos to mkv format&#x27;)&#xA;        change_extension(ts_files)&#xA;        &#xA;def encode_480(filename):&#xA;    FILENAME_PATH = filename&#xA;    newname = filename.split(&#x27;.&#x27;)[0]&#xA;    newname_path = os.path.join(VIDEO_PATH, newname)&#xA;&#xA;    poster_cmd = f&#x27;ffmpeg -y -ss  00:00:10 -i {FILENAME_PATH} -vframes  1 -q:v  2 poster.jpg&#x27;&#xA;    os.system(poster_cmd)&#xA;    &#xA;    if not os.path.exists(newname_path):&#xA;        os.mkdir(newname_path)&#xA;&#xA;    os.replace(&#x27;poster.jpg&#x27;, os.path.join(newname_path, &#x27;poster.jpg&#x27;))&#xA;    &#xA;&#xA;    ffmpeg480_cmd = f&#x27;ffmpeg -hide_banner -y -i {FILENAME_PATH} -sn -c:a aac -ac 2 -c:v libx264 -crf 23 -preset fast -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -maxrate 1024k -bufsize 1536k -b:a 128k -pix_fmt yuv420p -hls_segment_filename 4835JRK9%03d.ts 480p.m3u8&#x27;&#xA;    os.system(ffmpeg480_cmd)&#xA;    &#xA;    # os.remove(FILENAME_PATH)&#xA;    &#xA;    ts_files = glob.glob(&#x27;*.ts&#x27;)&#xA;    for ts_file in ts_files:&#xA;        os.replace(ts_file, os.path.join(newname_path, ts_file))&#xA;    &#xA;    master_text = &#x27;&#x27;&#x27;#EXTM3U&#xA;#EXT-X-STREAM-INF:BANDWIDTH=1500000,RESOLUTION=854x480&#xA;480p.m3u8&#xA;#EXT-X-STREAM-INF:BANDWIDTH=840000,RESOLUTION=640x360&#xA;360p.m3u8&#xA;&#x27;&#x27;&#x27;&#xA;    with open(&#x27;master.m3u8&#x27;, &#x27;w&#x27;) as f:&#xA;        f.write(master_text)&#xA;    &#xA;    m3u8_files = glob.glob(&#x27;*.m3u8&#x27;)&#xA;    for m3u8_file in m3u8_files:&#xA;        os.replace(m3u8_file, os.path.join(newname_path, m3u8_file))&#xA;&#xA;&#xA;def encode_360(filename):&#xA;    FILENAME_PATH = filename&#xA;    newname = filename.split(&#x27;.&#x27;)[0]&#xA;    newname_path = os.path.join(VIDEO_PATH,newname)&#xA;    poster_cmd = f&#x27;ffmpeg -y -ss 00:00:10 -i {FILENAME_PATH} -vframes 1 -q:v 2 poster.jpg&#x27;&#xA;    os.system(poster_cmd)&#xA;    if not os.path.exists(newname_path):&#xA;        os.mkdir(newname_path)&#xA;    os.replace(&#x27;poster.jpg&#x27;,os.path.join(newname_path, &#x27;poster.jpg&#x27;))&#xA;&#xA;    &#xA;    ffmpeg360_cmd = f&#x27;ffmpeg -hide_banner -y -i {FILENAME_PATH} -sn -c:a aac -ac 2 -c:v libx264 -crf 23 -preset fast -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -maxrate 512k -bufsize 768k -b:a 128k -pix_fmt yuv420p -hls_segment_filename 365RL6TJ%03d.ts 360p.m3u8&#x27;&#xA;    os.system(ffmpeg360_cmd)&#xA;    # return&#xA;    &#xA;    &#xA;    ts_files = glob.glob(&#x27;*.ts&#x27;)&#xA;    for ts_file in ts_files:&#xA;        os.replace(ts_file, os.path.join(newname_path,ts_file))&#xA;    master_text = &#x27;&#x27;&#x27;#EXTM3U&#xA;#EXT-X-STREAM-INF:BANDWIDTH=840000,RESOLUTION=640x360&#xA;360p.m3u8&#xA;    &#x27;&#x27;&#x27;&#xA;    with open(&#x27;master.m3u8&#x27;, &#x27;w&#x27;) as f:&#xA;        f.write(master_text)&#xA;    m3u8_files = glob.glob(&#x27;*.m3u8&#x27;)&#xA;    for m3u8_file in m3u8_files:&#xA;        os.replace(m3u8_file, os.path.join(newname_path, m3u8_file))&#xA;&#xA;&#xA;def encode_720(filename):&#xA;    FILENAME_PATH = filename&#xA;    newname = filename.split(&#x27;.&#x27;)[0]&#xA;    newname_path = os.path.join(VIDEO_PATH,newname)&#xA;    poster_cmd = f&#x27;ffmpeg -y -ss 00:00:10 -i {FILENAME_PATH} -vframes 1 -q:v 2 poster.jpg&#x27;&#xA;    os.system(poster_cmd)&#xA;&#xA;    if not os.path.exists(newname_path):&#xA;        os.mkdir(newname_path)&#xA;&#xA;    os.replace(&#x27;poster.jpg&#x27;,os.path.join(newname_path, &#x27;poster.jpg&#x27;))&#xA;&#xA;    ffmpeg720_cmd = f&#x27;ffmpeg -hide_banner -y -i {FILENAME_PATH} -sn -c:a aac -ac 2 -c:v libx264 -crf 23 -preset fast -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -maxrate 2048k -bufsize 3072k -b:a 160k -pix_fmt yuv420p -hls_segment_filename 7269TKL0%03d.ts 720p.m3u8&#x27;&#xA;    os.system(ffmpeg720_cmd)&#xA;&#xA;    # os.remove(FILENAME_PATH)&#xA;&#xA;    ts_files = glob.glob(&#x27;*.ts&#x27;)&#xA;    for ts_file in ts_files:&#xA;        os.replace(ts_file, os.path.join(newname_path,ts_file))&#xA;        &#xA;    m3u8_text = &#x27;&#x27;&#x27;#EXTM3U&#xA;#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720&#xA;720p.m3u8&#xA;#EXT-X-STREAM-INF:BANDWIDTH=840000,RESOLUTION=640x360&#xA;360p.m3u8        &#xA;        &#x27;&#x27;&#x27;&#xA;&#xA;    with open(&#x27;master.m3u8&#x27;,&#x27;w&#x27;) as f:&#xA;        f.write(m3u8_text)&#xA;            &#xA;    m3u8_files = glob.glob(&#x27;*.m3u8&#x27;)&#xA;    for m3u8_file in m3u8_files:&#xA;        os.replace(m3u8_file, os.path.join(newname_path,m3u8_file))  &#xA;&#xA;&#xA;def encode_1080(filename):&#xA;    FILENAME_PATH = filename&#xA;    newname = filename.split(&#x27;.&#x27;)[0]&#xA;    newname_path = os.path.join(VIDEO_PATH, newname)&#xA;    poster_cmd = f&#x27;ffmpeg -y -ss 00:00:10 -i {FILENAME_PATH} -vframes 1 -q:v 2 poster.jpg&#x27;&#xA;    os.system(poster_cmd)&#xA;&#xA;    if not os.path.exists(newname_path):&#xA;        os.mkdir(newname_path)&#xA;&#xA;    os.replace(&#x27;poster.jpg&#x27;, os.path.join(newname_path, &#x27;poster.jpg&#x27;))&#xA;&#xA;    ffmpeg1080_cmd = f&#x27;ffmpeg -hide_banner -y -i {FILENAME_PATH} -sn -c:a aac -ac 2 -c:v libx264 -crf 23 -preset fast -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -maxrate 4000k -bufsize 6000k -b:a 192k -pix_fmt yuv420p -hls_segment_filename 108YUT8T%03d.ts 1080p.m3u8&#x27;&#xA;    os.system(ffmpeg1080_cmd)&#xA;&#xA;    # os.remove(FILENAME_PATH)&#xA;&#xA;    ts_files = glob.glob(&#x27;*.ts&#x27;)&#xA;    for ts_file in ts_files:&#xA;        os.replace(ts_file, os.path.join(newname_path, ts_file))&#xA;&#xA;    m3u8_text = &#x27;&#x27;&#x27;#EXTM3U&#xA;#EXT-X-STREAM-INF:BANDWIDTH=4000000,RESOLUTION=1920x1080&#xA;1080p.m3u8&#xA;#EXT-X-STREAM-INF:BANDWIDTH=2800000,RESOLUTION=1280x720&#xA;720p.m3u8&#xA;#EXT-X-STREAM-INF:BANDWIDTH=840000,RESOLUTION=640x360&#xA;360p.m3u8&#xA;&#x27;&#x27;&#x27;&#xA;&#xA;    with open(&#x27;master.m3u8&#x27;, &#x27;w&#x27;) as f:&#xA;        f.write(m3u8_text)&#xA;&#xA;    m3u8_files = glob.glob(&#x27;*.m3u8&#x27;)&#xA;    for m3u8_file in m3u8_files:&#xA;        os.replace(m3u8_file, os.path.join(newname_path, m3u8_file))&#xA;&#xA;&#xA;&#xA;def vod(filename, resolution):&#xA;    os.replace(filename,filename.replace(&#x27; &#x27;,&#x27;_&#x27;))&#xA;    filename = filename.replace(&#x27; &#x27;,&#x27;_&#x27;)&#xA;    FILENAME_PATH = filename&#xA;    width_cmd = f&#x27;ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=nw=1:nk=1 {FILENAME_PATH}&#x27;&#xA;    height_cmd= f&#x27;ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=nw=1:nk=1 {FILENAME_PATH}&#x27;&#xA;&#xA;    width_result = subprocess.run(width_cmd.split(), stdout=subprocess.PIPE)&#xA;    width = width_result.stdout.strip().decode(&#x27;utf-8&#x27;)&#xA;&#xA;    height_result = subprocess.run(height_cmd.split(), stdout=subprocess.PIPE)&#xA;    height = height_result.stdout.strip().decode(&#x27;utf-8&#x27;)&#xA;&#xA;    if not os.path.exists(VIDEO_PATH):&#xA;        os.mkdir(VIDEO_PATH)&#xA;&#xA;    if resolution == 360: &#xA;        write_log(f&#x27;Encoding {filename} in 360p&#x27;) &#xA;        encode_360(filename)&#xA;&#xA;    if resolution == 720 :&#xA;        if int(height) >=400 :&#xA;            if int(height) &lt;= 700:&#xA;                query = f"""UPDATE videos SET encoding_status = &#x27;encoding480&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                executedb(query)&#xA;                write_log(f&#x27;Encoding {filename} in 480p&#x27;)&#xA;                encode_480(filename) &#xA;                query = f"""UPDATE videos SET encoding_status = &#x27;done480&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                executedb(query)&#xA;                query = f"""UPDATE videos SET quality = 2 WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                executedb(query)&#xA;            else:&#xA;                if int(height) >= 800:&#xA;                    query = f"""UPDATE videos SET encoding_status = &#x27;encoding720&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    write_log(f&#x27;Encoding {filename} in 720p&#x27;)&#xA;                    encode_720(filename)   &#xA;                    query = f"""UPDATE videos SET encoding_status = &#x27;done720&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    query = f"""UPDATE videos SET quality = 3 WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    query = f"""UPDATE videos SET encoding_status = &#x27;encoding1080&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    write_log(f&#x27;Encoding {filename} in 720p&#x27;)&#xA;                    encode_1080(filename)   &#xA;                    query = f"""UPDATE videos SET encoding_status = &#x27;done1080&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    query = f"""UPDATE videos SET quality = 4 WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                else:&#xA;                    query = f"""UPDATE videos SET encoding_status = &#x27;encoding720&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    write_log(f&#x27;Encoding {filename} in 720p&#x27;)&#xA;                    encode_720(filename)   &#xA;                    query = f"""UPDATE videos SET encoding_status = &#x27;done720&#x27; WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                    query = f"""UPDATE videos SET quality = 3 WHERE file_uid LIKE &#x27;%{filename.replace(".mkv","")}&#x27;"""&#xA;                    executedb(query)&#xA;                &#xA;        &#xA;            &#xA;        os.remove(filename)&#xA;&#xA;&#xA;def move_to_rclone():&#xA;    if not os.path.exists(RCLONE_PATH):&#xA;        os.mkdir(RCLONE_PATH)&#xA;    folders = os.listdir(VIDEO_PATH)&#xA;    if len(folders) > 0:&#xA;        for folder in folders:&#xA;            folder_in_rclone = os.path.join(RCLONE_PATH, folder)&#xA;            if not os.path.exists(folder_in_rclone):&#xA;                os.mkdir(folder_in_rclone)&#xA;            files = os.listdir(os.path.join(VIDEO_PATH, folder))&#xA;            for file in files:&#xA;                os.replace(os.path.join(VIDEO_PATH, folder, file), os.path.join(folder_in_rclone, file))&#xA;            os.system(MOVE_CMD.format(folder_in_rclone))&#xA;        if len(os.listdir(os.path.join(VIDEO_PATH, folder))) == 0:&#xA;            os.rmdir(os.path.join(VIDEO_PATH, folder))&#xA;    # rclone_folders = os.listdir(RCLONE_PATH)&#xA;    # if len(rclone_folders)> 0:&#xA;    #     for rclone_folder in rclone_folders:&#xA;    #         os.system(MOVE_CMD.format(rclone_folder))&#xA;&#xA;def get_length(input_video):&#xA;    result = subprocess.run([&#x27;ffprobe&#x27;, &#x27;-v&#x27;, &#x27;error&#x27;, &#x27;-show_entries&#x27;, &#x27;format=duration&#x27;, &#x27;-of&#x27;, &#x27;default=noprint_wrappers=1:nokey=1&#x27;, input_video], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)&#xA;    try:&#xA;        output = float(result.stdout)&#xA;    except:&#xA;        output = 0&#xA;    return output&#xA;&#xA;def move_to_error(file):&#xA;    if not os.path.exists(ERROR_PATH):&#xA;        os.mkdir(ERROR_PATH)&#xA;    print(f&#x27;[red][-][/red] Moving {file} to the error folder&#x27;)&#xA;    os.replace(file, os.path.join(ERROR_PATH, file.split(&#x27;/&#x27;)[-1]))&#xA;&#xA;def main():&#xA;    db = sql.connect(&#xA;        host=MYSQL_HOST,&#xA;        user=MYSQL_USER,&#xA;        password=MYSQL_PASSWORD,&#xA;        database=MYSQL_DB&#xA;    )&#xA;&#xA;    def get_video_data(file_uid):&#xA;        cursor = db.cursor()&#xA;        query = f"""SELECT id, post_title, file_uid, group_id FROM videos WHERE file_uid LIKE %s"""&#xA;        cursor.execute(query, (f&#x27;%{file_uid}%&#x27;,))&#xA;        data = cursor.fetchone()&#xA;        return data if data else None&#xA;    &#xA;    if os.path.exists(UPLOAD_PATH):&#xA;        upload_files = os.listdir(UPLOAD_PATH)&#xA;        upload_files = [os.path.join(UPLOAD_PATH, x) for x in upload_files]&#xA;        upload_files.sort(key=lambda x: os.path.getmtime(x))&#xA;    else:&#xA;        upload_files = []&#xA;&#xA;    if len(upload_files) > 0:&#xA;        for upload_file in upload_files:&#xA;            if get_length(upload_file) &lt; VIDEO_LENGTH:&#xA;                move_to_error(upload_file)&#xA;                write_log(f&#x27;Length of {upload_file} is less than 60 sec.  Moving to error folder&#x27;)&#xA;                continue&#xA;            try:&#xA;                os.replace(upload_file, upload_file.split(&#x27;/&#x27;)[-1])&#xA;                query = f"""UPDATE videos SET encoding_status = &#x27;uploaded&#x27; WHERE file_uid LIKE &#x27;%{upload_file.replace(".mkv","").replace(".mp4","")}&#x27;"""&#xA;                executedb(query)&#xA;            except:&#xA;                print(e)&#xA;                continue&#xA;            change_all_videos_extension()&#xA;&#xA;            mkv_files = glob.glob(&#x27;*.mkv&#x27;)&#xA;            if len(mkv_files) > 0:&#xA;                for mkv_file in mkv_files:&#xA;                    try:&#xA;                        query = f"""UPDATE videos SET encoding_status = &#x27;encoding360&#x27; WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;                        vod(mkv_file, 360)&#xA;                        query = f"""UPDATE videos SET encoding_status = &#x27;done360&#x27; WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;                        query = f"""UPDATE videos SET quality = 1 WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;&#xA;                        &#xA;                        vod(mkv_file, 720)&#xA;                        &#xA;                    except Exception as e:&#xA;                        query = f"""UPDATE videos SET encoding_status = &#x27;error&#x27; WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;                        video_data = get_video_data(&#xA;                            mkv_file.replace(".mkv", ""))&#xA;                        if video_data:&#xA;                            cursor = db.cursor()&#xA;                            video_id, video_title, video_uid, group_id = video_data&#xA;                            error_log_query = f"""INSERT INTO error_logs (video_id, video_title, video_uid, group_id, log, created_at, updated_at) &#xA;                                                VALUES (%s, %s, %s, %s, %s, NOW(), NOW())"""&#xA;                            cursor.execute(&#xA;                                error_log_query, (video_id, video_title, video_uid, group_id, str(e)))&#xA;                            db.commit()&#xA;                        write_log(f&#x27;Error: {e}&#x27;)&#xA;                        move_to_error(mkv_file)&#xA;                    move_to_rclone()&#xA;    else:&#xA;        print(f&#x27;[{time_now()}] No new video found on upload folder&#x27;)&#xA;&#xA;    if os.path.exists(UPLOAD_PATH2):&#xA;        upload_files2 = os.listdir(UPLOAD_PATH2)&#xA;        upload_files2 = [os.path.join(UPLOAD_PATH2, x) for x in upload_files2]&#xA;        upload_files2.sort(key=lambda x: os.path.getmtime(x))&#xA;    else:&#xA;        upload_files2 = []&#xA;&#xA;    if len(upload_files2) > 0:&#xA;        for upload_file2 in upload_files2:&#xA;            if get_length(upload_file2) &lt; VIDEO_LENGTH:&#xA;                move_to_error(upload_file2)&#xA;                continue&#xA;            if len(os.listdir(UPLOAD_PATH)) != 0:&#xA;                main()&#xA;            try:&#xA;                os.replace(upload_file2, upload_file2.split(&#x27;/&#x27;)[-1])&#xA;                query = f"""UPDATE videos SET encoding_status = &#x27;uploaded&#x27; WHERE file_uid LIKE &#x27;%{upload_file2.replace(".mkv","").replace(".mp4","")}&#x27;"""&#xA;                executedb(query)&#xA;            except:&#xA;                continue&#xA;&#xA;            change_all_videos_extension()&#xA;&#xA;            mkv_files = glob.glob(&#x27;*.mkv&#x27;)&#xA;            if len(mkv_files) > 0:&#xA;                for mkv_file in mkv_files:&#xA;                    try:&#xA;                        query = f"""UPDATE videos SET encoding_status = &#x27;encoding360&#x27; WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;                        vod(mkv_file, 360)&#xA;                        query = f"""UPDATE videos SET encoding_status = &#x27;done360&#x27; WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;                        query = f"""UPDATE videos SET quality = 1 WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;&#xA;&#xA;                        vod(mkv_file, 720)&#xA;&#xA;                    except Exception as e:&#xA;                        query = f"""UPDATE videos SET encoding_status = &#x27;error&#x27; WHERE file_uid LIKE &#x27;%{mkv_file.replace(".mkv","")}&#x27;"""&#xA;                        executedb(query)&#xA;                        video_data = get_video_data(&#xA;                            mkv_file.replace(".mkv", ""))&#xA;                        if video_data:&#xA;                            cursor = db.cursor()&#xA;                            video_id, video_title, video_uid, group_id = video_data&#xA;                            error_log_query = f"""INSERT INTO error_logs (video_id, video_title, video_uid, group_id, log, created_at, updated_at) &#xA;                                                VALUES (%s, %s, %s, %s, %s, NOW(), NOW())"""&#xA;                            cursor.execute(&#xA;                                error_log_query, (video_id, video_title, video_uid, group_id, str(e)))&#xA;                            db.commit()&#xA;                        write_log(f&#x27;Error: {e}&#x27;)&#xA;                        move_to_error(mkv_file)&#xA;            move_to_rclone()&#xA;    else:&#xA;        print(f&#x27;[{time_now()}] No new video found on upload2 folder.&#x27;)&#xA;    move_to_rclone()&#xA;    db.close()&#xA;&#xA;if __name__=="__main__":&#xA;    while True:&#xA;        try:&#xA;            main()&#xA;        except Exception as e:&#xA;            print(f&#x27;Error: {e}&#x27;)&#xA;        for i in range(WAIT):&#xA;            print(f&#x27;[green][&#x2B;][/green] Waiting for {WAIT-i} seconds  &#x27;, end="\r")&#xA;            time.sleep(1)&#xA;&#xA;&#xA;

    &#xA;

    looking for suggestion to improve encoding speed

    &#xA;