Newest 'ffmpeg' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/ffmpeg

Les articles publiés sur le site

  • Video generation and ffmpeg and locally stored images [closed]

    3 juillet, par Rahul Patil

    I am facing issue in ffmpeg while running the below code

    I'm working on a Flask application that generates a video by combining a sequence of images from a folder and a synthesized audio track using Bark (suno/bark-small). The idea is to use FFmpeg to stitch the images into a video, apply padding and scaling, and then merge it with the generated audio. I'm triggering the /generate-video endpoint with a simple curl POST request, passing a script that gets converted to audio. While the image and audio processing work as expected, FFmpeg fails during execution, and the server returns a 500 error. I've added error logging to capture FFmpeg’s stderr output, which suggests something is going wrong either with the generated input.txt file or the format of the inputs passed to FFmpeg. I'm not sure if the issue is related to file paths, the concat demuxer formatting, or possibly audio/video duration mismatch. Any insights on how to debug or correct the FFmpeg command would be appreciated.

    the curl request is

    curl -X POST http://localhost:5000/generate-video \
         -H "Content-Type: application/json" \
         -d '{"script": "Hello, this is a test script to generate a video."}' \
         --output output_video.mp4
    
    import os
    import uuid
    import subprocess
    from pathlib import Path
    import numpy as np
    from flask import Flask, request, jsonify, send_file
    from transformers import AutoProcessor, AutoModelForTextToWaveform
    from scipy.io.wavfile import write as write_wav
    import torch
    
    # ========== CONFIG ==========
    IMAGE_FOLDER = "./images"
    OUTPUT_FOLDER = "./output"
    RESOLUTION = (1280, 720)
    IMAGE_DURATION = 3  # seconds per image
    SAMPLE_RATE = 24000
    
    app = Flask(__name__)
    os.makedirs(OUTPUT_FOLDER, exist_ok=True)
    
    # Load Bark-small model and processor
    device = "cuda" if torch.cuda.is_available() else "cpu"
    processor = AutoProcessor.from_pretrained("suno/bark-small")
    model = AutoModelForTextToWaveform.from_pretrained("suno/bark-small").to(device)
    
    
    # ========== UTILS ==========
    def run_ffmpeg(cmd):
        result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if result.returncode != 0:
            print("[FFmpeg ERROR]\n", result.stderr.decode())
            raise RuntimeError("FFmpeg failed.")
        else:
            print("[FFmpeg] Success.")
    
    
    def find_images(folder):
        return sorted([
            f for f in Path(folder).glob("*")
            if f.suffix.lower() in {".jpg", ".jpeg", ".png"}
        ])
    
    
    def create_ffmpeg_input_list(images, list_file_path):
        with open(list_file_path, "w") as f:
            for img in images:
                f.write(f"file '{img.resolve()}'\n")
                f.write(f"duration {IMAGE_DURATION}\n")
            # Repeat last image to avoid cutoff
            f.write(f"file '{images[-1].resolve()}'\n")
    
    
    # ========== FLASK ROUTE ==========
    @app.route('/generate-video', methods=['POST'])
    def generate_video():
        data = request.get_json()
        script = data.get("script")
        if not script:
            return jsonify({"error": "No script provided"}), 400
    
        images = find_images(IMAGE_FOLDER)
        if not images:
            return jsonify({"error": "No images found in ./images"}), 400
    
        # Generate audio
        print("[1/3] Generating audio with Bark...")
        inputs = processor(script, return_tensors="pt").to(device)
        with torch.no_grad():
            audio_values = model.generate(**inputs)
    
        audio_np = audio_values[0].cpu().numpy().squeeze()
        audio_np = np.clip(audio_np, -1.0, 1.0)
        audio_int16 = (audio_np * 32767).astype(np.int16)
    
        audio_path = os.path.join(OUTPUT_FOLDER, f"{uuid.uuid4()}.wav")
        write_wav(audio_path, SAMPLE_RATE, audio_int16)
    
        # Create FFmpeg concat file
        print("[2/3] Preparing image list for FFmpeg...")
        list_file = os.path.join(OUTPUT_FOLDER, "input.txt")
        create_ffmpeg_input_list(images, list_file)
    
        # Final video path
        final_video_path = os.path.join(OUTPUT_FOLDER, f"{uuid.uuid4()}.mp4")
    
        # Run FFmpeg
        print("[3/3] Running FFmpeg to create video...")
        ffmpeg_cmd = [
            "ffmpeg", "-y",
            "-f", "concat", "-safe", "0", "-i", list_file,
            "-i", audio_path,
            "-vf", f"scale={RESOLUTION[0]}:{RESOLUTION[1]}:force_original_aspect_ratio=decrease,"
                   f"pad={RESOLUTION[0]}:{RESOLUTION[1]}:(ow-iw)/2:(oh-ih)/2:color=black",
            "-c:v", "libx264", "-pix_fmt", "yuv420p",
            "-c:a", "aac", "-b:a", "192k",
            "-shortest", "-movflags", "+faststart",
            final_video_path
        ]
    
        try:
            run_ffmpeg(ffmpeg_cmd)
        except RuntimeError:
            return jsonify({"error": "FFmpeg failed. Check server logs."}), 500
    
        return send_file(final_video_path, as_attachment=True)
    
    
    # ========== RUN APP ==========
    if __name__ == '__main__':
        app.run(debug=True)
    
  • Flutter ffmpeg_kit_flutter_new can't build Android app in any version

    3 juillet, par user31929

    I can't build my project on Android ( on Ios it works and the project itself without ffmpeg_kit_flutter_new builds without problems ) This is the error i obtain:

    /GeneratedPluginRegistrant.java:51: error: cannot find symbol
          com.antonkarpenko.ffmpegkit.MainActivity.registerWith(shimPluginRegistry.registrarFor("com.antonkarpenko.ffmpegkit.MainActivity"));
                                     ^
      symbol:   class MainActivity
      location: package com.antonkarpenko.ffmpegkit
    

    This is my flutter doctor :

    [✓] Flutter (Channel stable, 3.19.4, on macOS 15.4.1 24E263 darwin-x64, locale it-IT)
        • Flutter version 3.19.4 on channel stable at ….
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 68bfaea224 (1 year, 2 months ago), 2024-03-20 15:36:31 -0700
        • Engine revision a5c24f538d
        • Dart version 3.3.2
        • DevTools version 2.31.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
        • Android SDK at …..
        • Platform android-35, build-tools 34.0.0
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16E140
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2023.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.99.3)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (5 available)
        • SM A135F (mobile)              • RF8T40TMS6Z               • android-arm    • Android 12 (API 31)
        • cri SE 128 (mobile)      • 00008030-001268303E38402E • ios            • iOS 18.4.1 22E252
        • iPhone di WacMini (mobile) • 00008030-00121D543CE8802E • ios            • iOS 18.4.1 22E252
        • macOS (desktop)                • macos                     • darwin-x64     • macOS 15.4.1 24E263 darwin-x64
        • Chrome (web)                   • chrome                    • web-javascript • Google Chrome 136.0.7103.93
    
    [✓] Network resources
        • All expected network resources are available.
    

    My android/app/build.gradle

    def localProperties = new Properties()
    def localPropertiesFile = rootProject.file('local.properties')
    if (localPropertiesFile.exists()) {
        localPropertiesFile.withReader('UTF-8') { reader ->
            localProperties.load(reader)
        }
    }
    
    def keystoreProperties = new Properties()
    def keystorePropertiesFile = rootProject.file('key.properties')
    if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    }
    
    def flutterRoot = localProperties.getProperty('flutter.sdk')
    if (flutterRoot == null) {
        throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
    }
    
    def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
    if (flutterVersionCode == null) {
        flutterVersionCode = '1'
    }
    
    def flutterVersionName = localProperties.getProperty('flutter.versionName')
    if (flutterVersionName == null) {
        flutterVersionName = '1.0'
    }
    
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'com.google.firebase.crashlytics'
    apply plugin: 'org.jetbrains.kotlin.android'
    
    
    android {
    
        compileSdkVersion 35
    
        namespace = "com.app.app"
        sourceSets {
            main.java.srcDirs += 'src/main/kotlin'
        }
    
        defaultConfig {
            applicationId "com.appid.appid"
            minSdkVersion 24
            targetSdkVersion 35
            versionCode flutterVersionCode.toInteger()
            versionName flutterVersionName
           
            // insert this line of code in order to manage correct build abi configuration only on supported devices not supported tablet device emulator
           /* ndk {
                abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
            }*/
        }
    
        signingConfigs {
            release {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                storePassword keystoreProperties['storePassword']
            }
        }
    
        buildTypes {
            debug {
                debuggable true
            }
    
            release {
                signingConfig signingConfigs.release
                debuggable false
                shrinkResources true
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
    }
    
    flutter {
        source '../..'
    }
    
    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.24"
    }
    

    My android/build.gradle

    buildscript {
        ext.kotlin_version = '1.9.24'
        repositories {
            google()
            mavenCentral()
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:8.4.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath 'com.google.gms:google-services:4.3.14'
            classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1'
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
            jcenter()
        }
    
        
        subprojects {
            tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
                kotlinOptions.jvmTarget = "1.8"
            }
            afterEvaluate { project ->
                if (project.hasProperty('android')) {
                    project.android {
                        if (namespace == null) {
                            namespace project.group
                        }
                    }
                }
            }
        }
        
    }
    
    
    ext {
        flutterFFmpegPackage = "min-gpl-lts"
    }
    
    
    rootProject.buildDir = '../build'
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
    }
    subprojects {
        project.evaluationDependsOn(':app')
    }
    
    tasks.register("clean", Delete) {
        delete rootProject.buildDir
    }
    

    My gradle.wrapper.properties

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
    

    What i have already tried :

    • flutter clean/flutter pub get
    • remove .gradle folder/flutter clean/flutter pub get
    • remove GeneratedPluginRegistrant.java file then remove .gradle/flutter clean/flutter pub get

    I have this issue in every version of the plugin. There is something wrong in my configurations or maybe this is a plugin issue ?

  • Extract motion vectors from x265 (HEVC) encoded video with ffmpeg/libavcodec ?

    3 juillet, par John Allard

    I know that one can extract the motion vectors from an h264 encoded via by first setting the flag

    av_dict_set(&opts, "flags2", "+export_mvs", 0);
    

    then you can query the side-data for the motion vectors by doing this

    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
    

    When I looked online to see if you can do something similar with HEVC encoded videos, I wasn't able to find any information. All I found was this by the definition of "AV_FRAME_DATA_MOTION_VECTORS"

    Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec AVCodecContext flags2 option).

    The data is the AVMotionVector struct defined in libavutil/motion_vector.h.

    but there was no information on exactly which codecs export this motion vector information. How would I go about finding this out?

  • How to comp portion of first frame onto rest of video without re-encoding ? [closed]

    2 juillet, par jonathan

    I am working for a company recording educational screencasts using Goodnotes. The first frame looks fine, but as I work on the document, the page number pops up on the bottom left.

    My bosses don't like this, so I've been trying to find a way to fix it on the screencast, since I can't find a way to turn it off in Goodnotes.

    My current solution is taking a screenshot of the clean frame and using Gimp to crop out all but a small rectangle around where the page number would be, exporting that as a PNG and using ffmpeg to overlay that. The resulting image looks great and my bosses approve, but the script takes time to run since it needs to reencode. I'm wondering if there's a way to do this all with ffmpeg and run it with the -c copy flag since the first frame is clean. Please let me know if this is even possible.

  • Use FFmpeg to create MPEG-DASH files

    2 juillet, par angel_30

    I know using ffmpeg, we can create MPEG-DASH ready files, including the segments and the .mpd manifest file. For instance, I'm trying this command which works:

    ffmpeg -re -i .\video-h264.mkv -map 0 -map 0 -c:a aac -c:v libx264 -b:v:0 800k -b:v:1 300k -s:v:1 320x170 -profile:v:1 baseline -profile:v:0 main -bf 1 -keyint_min 120 -g 120 -sc_threshold 0 -b_strategy 0 -ar:a:1 22050 -use_timeline 1 -use_template 1 -window_size 5 -adaptation_sets "id=0,streams=v id=1,streams=a" -f dash out.mpd
    

    But I don't want to segment the video- so a simpler version where we have multiple versions of the whole video, no chunks. Does MPEG-DASH allow it? If so, how can I use ffmpeg to do it without creating the chunks?