Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Error with FFmpeg and FS in React : "ErrnoError : FS error"

    25 avril, par namwan

    I'm working on a React application where I'm using the @ffmpeg/ffmpeg library to compress images and videos. I'm facing an issue with the virtual file system (FS) when trying to read and write files using FFmpeg. I'm getting the following error:

    ErrnoError: FS error
    

    Here's the relevant part of my code:

    import React, { useState } from "react";
    import { FFmpeg } from "@ffmpeg/ffmpeg";
    
    const ffmpeg = new FFmpeg();
    
    const fetchFile = async (filePath) => {
        const file = await ffmpeg.readFile(filePath);
        alert("hello");
        return new Uint8Array(file).buffer;
    };
    
    
    const Main = () => {
        const [file, setFile] = useState(null);
        const [compressedFile, setCompressedFile] = useState("");
    
        const loadFFmpeg = async () => {
            if (!ffmpeg.isLoaded) {
              await ffmpeg.load();
            }
          };      
    
        const getFile = (event) => {
            const selectedFile = event.target.files[0];
            
            if (selectedFile) {
                setFile(selectedFile);
            }
        };
    
        const compressImage = (selectedFile) => {
            const img = new Image();
            img.src = URL.createObjectURL(selectedFile);
            img.onload = () => {
                const canvas = document.createElement('canvas');
                const MAX_WIDTH = 300;
                const MAX_HEIGHT = 300;
                let width = img.width;
                let height = img.height;
    
                if (width > height) {
                    if (width > MAX_WIDTH) {
                        height *= MAX_WIDTH / width;
                        width = MAX_WIDTH;
                    }
                } else {
                    if (height > MAX_HEIGHT) {
                        width *= MAX_HEIGHT / height;
                        height = MAX_HEIGHT;
                    }
                }
    
                canvas.width = width;
                canvas.height = height;
                const ctx = canvas.getContext('2d');
                ctx.drawImage(img, 0, 0, width, height);
                const dataUrl = canvas.toDataURL('image/jpeg', 1.0);
                setCompressedFile(dataUrl);
            };
        };
    
        const compressVideo = async (selectedFile) => {
            try {
                await loadFFmpeg();
        
                const arrayBuffer = await selectedFile.arrayBuffer();
                const fileName = selectedFile.name;
        
                await ffmpeg.writeFile(fileName, new Uint8Array(arrayBuffer));
        
                await ffmpeg.exec(
                    '-i',
                    fileName,
                    '-vf',
                    'scale=640:-1',
                    '-c:a',
                    'aac',
                    '-strict',
                    '-2',
                    'output.mp4'
                );
        
                const data = await fetchFile('output.mp4');
                const compressedVideoBlob = new Blob([data], { type: 'video/mp4' });
                const compressedVideoUrl = URL.createObjectURL(compressedVideoBlob);
                setCompressedFile(compressedVideoUrl);
        
                await ffmpeg.unlink(fileName);
                await ffmpeg.unlink('output.mp4');
        
                alert('Compression successful');
            } catch (error) {
                console.error('Error:', error);
                alert('Compression failed. Please check the console for more details.');
            }
        };
            
    
        const handleSubmit = async (e) => {
            e.preventDefault();
    
            if (file) {
                const fileType = file.name.split('.').pop().toLowerCase();
    
                if (fileType === 'png' || fileType === 'jpg' || fileType === 'jpeg') {
                    compressImage(file);
                } else if (fileType === 'mp4' || fileType === 'h264') {
                    compressVideo(file);
                } else {
                    alert('Please select a valid file type (png, jpg, jpeg for images or mp4, h264 for videos).');
                }
            }
        };
    
        const handleDownload = () => {
            if (file) {
                const downloadLink = document.createElement('a');
                downloadLink.href = compressedFile;
    
                const fileExtension = file.name.split('.').pop().toLowerCase();
    
                downloadLink.download = `compressed_file.${fileExtension}`;
        
                document.body.appendChild(downloadLink);
                downloadLink.click();
                document.body.removeChild(downloadLink);
            }
        };
    
        return (
            <>
                

    Main Page



    {compressedFile && ( <>

    Compressed File Preview

    {file && file.name && ( file.name.split('.').pop().toLowerCase() === 'mp4' || file.name.split('.').pop().toLowerCase() === 'h264' ? ( ) : ( Compressed file preview ) )}

    > )} > ); }; export default Main;

    I'm using ffmpeg.readFile and ffmpeg.writeFile to read and write files to FFmpeg's virtual file system. I've also tried using ffmpeg.read and ffmpeg.write but still encounter the same issue.

    Could someone please help me understand what might be causing this FS error and how to resolve it?

  • Difficulty getting the expected output for .avi conversions to .mp4 using FFMPEG [closed]

    25 avril, par Ericel

    I am trying to write a video converting script that converts an input video file to .mp4 output. I am using ffmpeg libx264 encoder. Unfortunately, when I convert from .avi to .mp4, the output video is not smooth, a little skippy. Here is how I set up the encoder code:

    AVCodecContext *Video::setupVideoEncoder(const AVCodec *encoder, AVStream *inVideoStream, AVFormatContext *outFormatCtx)
    {
        AVCodecContext *codecCtx = avcodec_alloc_context3(encoder);
        if (!codecCtx)
        {
            std::cerr << "Failed to allocate the video codec context." << std::endl;
            return nullptr;
        }
    
        if (avcodec_parameters_to_context(codecCtx, inVideoStream->codecpar) < 0)
        {
            std::cerr << "Failed to copy codec parameters to encoder context." << std::endl;
            avcodec_free_context(&codecCtx);
            return nullptr;
        }
    
        // Correctly assign pixel format based on encoder support
        if (!check_pix_fmt(encoder, inVideoStream->codecpar->format))
        {
            codecCtx->pix_fmt = encoder->pix_fmts[0];
        }
        else
        {
            codecCtx->pix_fmt = static_cast(inVideoStream->codecpar->format);
        }
        codecCtx->width = inVideoStream->codecpar->width;
        codecCtx->height = inVideoStream->codecpar->height;
        codecCtx->bit_rate = 2000000; // 2 Mbps
        codecCtx->gop_size = 12;
        codecCtx->max_b_frames = 3;
    
        // Setting frame rate and time base using guessed values
        AVRational framerate = av_guess_frame_rate(outFormatCtx, inVideoStream, nullptr);
        codecCtx->framerate = framerate;
        codecCtx->time_base = av_inv_q(framerate);
    
        AVDictionary *opts = nullptr;
        av_dict_set(&opts, "x264-params", "keyint=25:min-keyint=25:no-scenecut=1", 0);
    
        if (outFormatCtx->oformat->flags & AVFMT_GLOBALHEADER)
        {
            codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
        }
    
        if (avcodec_open2(codecCtx, encoder, &opts) < 0)
        {
            std::cerr << "Failed to open the video encoder." << std::endl;
            avcodec_free_context(&codecCtx);
            av_dict_free(&opts);
            return nullptr;
        }
    
        av_dict_free(&opts);
        return codecCtx;
    } 
    

    I can only thing the configurations here are the problem, because if I converted a .mov to .mp4, I get the expected output.

  • Discord bot transmitting audio not working

    24 avril, par R5B3NBB

    this discord bot is supposed to join the users voice channel and play music, everything works except for it not transmitting audio. from what I can gather the error is happening somewhere between it playing the audio file and it being transmitted. any helps appreciated and I'm sure it's not on the discord bot side since I gave it admin permissions and it still didn't work. should also be mentioned that im not a veteran when it comes to JavaScript so if the error s obvious sorry.

    const { Client, GatewayIntentBits, SlashCommandBuilder, MessageEmbed } = require("discord.js");
    const { joinVoiceChannel, createAudioPlayer, NoSubscriberBehavior, createAudioResource } = require('@discordjs/voice');
    const { token } = require("./config.json");
    const fs = require('fs');
    
    const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions] });
    
    client.once("ready", () => {
        console.log("Bot active.");
    
        const ping = new SlashCommandBuilder()
            .setName('ping')
            .setDescription('Replies with pong!');
    
        const hello = new SlashCommandBuilder()
            .setName('hello')
            .setDescription('Says hello!')
            .addUserOption(option =>
                option
                    .setName('user')
                    .setDescription('The user to say hi to')
                    .setRequired(false)
            );
    
        const echo = new SlashCommandBuilder()
            .setName('echo')
            .setDescription("Echo's a message")
            .addStringOption(option =>
                option
                    .setName('text')
                    .setDescription('The text to repeat')
                    .setRequired(true)
            );
    
        const mata = new SlashCommandBuilder()
            .setName('mata')
            .setDescription('Green, ma-ta is here!');
    
        const eddy = new SlashCommandBuilder()
            .setName('eddy')
            .setDescription('Original reggae music!')
            .addStringOption(option =>
                option
                    .setName('local_audio')
                    .setDescription('Provide the name of the local audio file')
                    .setRequired(true)
            );
    
        const disconnect = new SlashCommandBuilder()
            .setName('disconnect')
            .setDescription('Disconnects the bot from the voice channel');
    
        const commands = [ping, hello, echo, mata, eddy, disconnect];
    
        commands.forEach(command => client.guilds.cache.forEach(guild => guild.commands.create(command)));
    
    
    });
    
    client.on('interactionCreate', async interaction => {
        console.log("Interaction received.");
        
        if (!interaction.isCommand()) {
            console.log("Not a command interaction.");
            return;
        }
    
        const { commandName, options, member, guild } = interaction;
        console.log("Command name:", commandName);
    
        if (commandName === 'ping') {
            console.log("Ping command executed.");
            await interaction.reply("Pong!");
        } else if (commandName === 'hello') {
            console.log("Hello command executed.");
            let user = options.getUser('user') || member.user;
            await interaction.reply(`Hello ${user.username}!`);
        } else if (commandName === 'echo') {
            console.log("Echo command executed.");
            const text = options.getString('text');
            await interaction.reply(text);
        } else if (commandName === 'mata') {
            console.log("Mata command executed.");
            const embed = new MessageEmbed()
                .setColor('#00FF00')
                .setTitle('This is mata!')
                .setDescription('She is very ugly!')
                .setImage('https://scottbarrykaufman.com/wp-content/uploads/2011/08/pig-ugly-woman-fat-face.jpg');
    
            await interaction.reply({ embeds: [embed] });
        } else if (commandName === 'eddy') {
            console.log("Eddy command executed.");
    
            const voiceChannel = member.voice.channel;
            if (!voiceChannel) {
                await interaction.reply("You need to be in a voice channel to use this command.");
                return;
            }
    
            const connection = joinVoiceChannel({
                channelId: voiceChannel.id,
                guildId: guild.id,
                adapterCreator: guild.voiceAdapterCreator,
            });
    
            connection.on('stateChange', (state) => {
                console.log(`Connection state changed to ${state.status}`);
            });
    
            connection.on('error', (error) => {
                console.error('Connection error:', error);
            });
    
            const localAudioFile = options.getString('local_audio');
            const filePath = `./audio/Gorillaz.mp3`;
    
            console.log("File path:", filePath);
    
            if (!fs.existsSync(filePath)) {
                console.log("File does not exist.");
                await interaction.reply("The specified local audio file does not exist.");
                return;
            }
    
            const audioPlayer = createAudioPlayer({
                behaviors: {
                    noSubscriber: NoSubscriberBehavior.Pause,
                },
            });
    
            const stream = fs.createReadStream(filePath);
            const resource = createAudioResource(stream);
            audioPlayer.play(resource);
    
            connection.subscribe(audioPlayer);
    
            console.log("Audio playback started.");
            await interaction.reply("Now playing audio in your voice channel!");
        } else if (commandName === 'disconnect') {
            console.log("Disconnect command executed.");
            const voiceChannel = member.voice.channel;
    
            if (!voiceChannel) {
                await interaction.reply("The bot is not in a voice channel.");
                return;
            }
    
            const connection = joinVoiceChannel({
                channelId: voiceChannel.id,
                guildId: guild.id,
                adapterCreator: guild.voiceAdapterCreator,
            });
    
            connection.on('stateChange', (state) => {
                console.log(`Connection state changed to ${state.status}`);
            });
    
            connection.on('error', (error) => {
                console.error('Connection error:', error);
            });
    
            if (connection) {
                connection.destroy();
                console.log("Disconnected from the voice channel.");
            }
        }
    });
    
    client.login(token);
    
  • Video concatenation with ffmpeg and Java [closed]

    24 avril, par Marco Antonio Manjarrez Fernan

    I have this code that automatically makes a couple of videos using ffmpeg. The result is 2 or 3 .mp4 files that consist of an audio and a still image. I want to concatenate these files automatically by using ffmpeg concat demuxer, but when the concatenation finishes the videos do concatenate, but the images are hyperimposed on top of one another. So instead of having 12 seconds of a cat image and 10 seconds of a skyscraper, I get 22 seconds of a cat image, and when I skip forward in the video the image changes to the skyscraper and doesnt change back.

    Every single tutorial I see has the same command work in the intended way, and trying it directly in the command line does not help either. Since all the images are made automatically, they have the same codec, and I also keep their aspect ratios the same.

    I tried using this function on Java, which like I said, does concatenate the videos, but by "prioritizing" one of the images. The audios retain their length and are placed in order, which in my opinion makes the issue even weirder

     public static void concatenateVideos(){
            String[] listCommand = {
                    "cmd", "/c", "(for", "%i", "in", "(C:\\myPath*.mp4)", "do", "@echo", "file", "'%i')", ">", "C:\\myPath\\list.txt"
            };
            commandLine(listCommand);
    
            String[] ffmpegCommand = {"ffmpeg", "-safe", "0", "-f", "concat", "-i", "C:\\myPath\\list.txt", "-c", "copy", "C:\\myPath\\openAIVideo.mp4"};
            commandLine(ffmpegCommand);
       }   
    
  • 2 files found with path 'lib/arm64-v8a/libc++_shared.so' from inputs...-react native

    24 avril, par CrackerKSR

    I am trying to enable package of ffmpeg-kit-react-native in react-native. The sample commands given in the example executes successfully. But I want to use libwebp for converting gif files to webp which is under package named video. As instrcuted . I have to enable the package to use some libraries.

    2.2.1 Enabling a Package on Android Edit android/build.gradle file and add the package name in ext.ffmpegKitPackage variable.

    ext {
       ffmpegKitPackage = ""
    }
    

    So I added a line in the node_module/ffmpeg-kit-react-native/android/build.gradle

    android {
      compileSdkVersion 30
    
      defaultConfig {
        minSdkVersion safeExtGet('ffmpegKitPackage', 'https').contains("-lts") ? 16 : 24
        targetSdkVersion 30
        versionCode 451
        versionName "4.5.1"
      }
    
      buildTypes {
        release {
          minifyEnabled false
        }
      }
      lintOptions {
        disable 'GradleCompatible'
      }
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      }
    
      rootProject.ext.ffmpegKitPackage = "video" // Added this line here 
    
    }
    

    Error:

    * What went wrong:
    Execution failed for task ':app:mergeDebugNativeLibs'.
    > A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
       > 2 files found with path 'lib/arm64-v8a/libc++_shared.so' from inputs:
          - C:\Users\ADMIN\.gradle\caches\transforms-3\7403ebe5571a2ce5a6a5fc9876af4814\transformed\jetified-react-native-0.66.4\jni
          - C:\Users\ADMIN\.gradle\caches\transforms-3\4be54e44fe38656741a8345504588323\transformed\jetified-ffmpeg-kit-video-4.5.1-1\jni
         If you are using jniLibs and CMake IMPORTED targets, see
         https://developer.android.com/r/tools/jniLibs-vs-imported-targets
    

    I have tried ./gradlew clean but problem is still there. How to fix this error? Thanks