Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • Decode MPEG-2,4 AAC LATM Bitstream to .wav files [closed]

    26 avril, par buliet suka

    I'm tring to use ffmpeg to transform the AAC LATM raw data to WAV file(or mp4 file), but it shows Error. Is the command correct? enter image description here

    Part of the raw data: enter image description here

    I use ffmpeg to change AAC ATDS file to mp4 file, succeed. enter image description here Part of the ATDS file: enter image description here Maybe the raw data format is not correct, but I dont know how to change it. Please help me.

  • How do I include FFMpeg for development in a windows program ?

    25 avril, par Lama Mao

    I'm trying to follow a tutorial about using the FFMpeg libraries to extract frames from videos in C++ (by Bartholomew).

    In order to include the libraries, according to the tutorial, it is advised to use pkg-config to find the libraries and put them together so that CMake can include them. In the video he uses homebrew to install ffmpeg and somehow pkg-config is able to find the header files for ffmpeg (libavcodec, libavformat, libavdevice ...)

    The problem is I don't know how to get the ffmpeg libraries installed so that pkg-config can find them.

    On windows, I've tried installing the compiled windows binaries, and I've tried installing ffmpeg-full package using chocolatey. However, I fail to see where these header files are installed to.

    There's a ffmpeg folder in C:/ffmpeg/ but when I look in that there's no header files or libraries, just the binaries. Perhaps I need to clone the entire source project, but then how is pkg-config supposed to find them? When I try and compile I get this output:

     Found PkgConfig: C:/Strawberry/perl/bin/pkg-config.bat (found version "0.26") 
     Checking for module 'libavcodec'
     Can't find libavcodec.pc in any of C:/Strawberry/c/lib/pkgconfig
     use the PKG_CONFIG_PATH environment variable, or
     specify extra search paths via 'search_paths'
     CMake Error at C:/Program Files/CMake/share/cmake-3.24/Modules/FindPkgConfig.cmake:607 (message):
     A required package was not found
     Call Stack (most recent call first):
     C:/Program Files/CMake/share/cmake-3.24/Modules/FindPkgConfig.cmake:829 (_pkg_check_modules_internal)
     lib/FFMpeg/CMakeLists.txt:5 (pkg_check_modules)
     Checking for module 'libavcodec'        <----- here
     Can't find libavcodec.pc in any of C:/Strawberry/c/lib/pkgconfig
     use the PKG_CONFIG_PATH environment variable, or
     specify extra search paths via 'search_paths'
     CMake Error at C:/Program Files/CMake/share/cmake-3.24/Modules/FindPkgConfig.cmake:607 (message):
     A required package was not found
     Call Stack (most recent call first):
     C:/Program Files/CMake/share/cmake-3.24/Modules/FindPkgConfig.cmake:829 (_pkg_check_modules_internal)
     lib/FFMpeg/CMakeLists.txt:5 (pkg_check_modules)
    

    The contents of C:/Strawberry/c/lib/pkgconfig seem to be a whole load of libraries none of which are from ffmpeg.

    In case the problem is in my CMakeLists file, here are the contents of the subdirectory file:

    cmake_minimum_required(VERSION 3.9)
    project(FFMpeg)
    
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(AVCODEC    REQUIRED    IMPORTED_TARGET libavcodec)
    pkg_check_modules(AVFILTER   REQUIRED    IMPORTED_TARGET libavformat)
    pkg_check_modules(AVDEVICE   REQUIRED    IMPORTED_TARGET libavdevice)
    pkg_check_modules(AVUTIL     REQUIRED    IMPORTED_TARGET libavutil)
    pkg_check_modules(SWRESAMPLE REQUIRED    IMPORTED_TARGET libswresample)
    pkg_check_modules(SWSCALE    REQUIRED    IMPORTED_TARGET libswscale)
    
    add_library(FFMpeg INTERFACE IMPORTED GLOBAL)
    
    target_include_directories(
        FFmpeg INTERFACE
        ${AVCODEC_INCLUDE_DIRS}
        ${AVFILTER_INCLUDE_DIRS}
        ${AVDEVICE_INCLUDE_DIRS}
        ${AVUTIL_INCLUDE_DIRS}
        ${SWRESAMPLE_INCLUDE_DIRS}
        ${SWSCALE_INCLUDE_DIRS}
    )
    
    target_link_options(
        FFmpeg INTERFACE
        ${AVCODEC_LDFLAGS}
        ${AVFILTER_LDFLAGS}
        ${AVDEVICE_LDFLAGS}
        ${AVUTIL_LDFLAGS}
        ${SWRESAMPLE_LDFLAGS}
        ${SWSCALE_LDFLAGS}
    )
    

    What have I done wrong/am not understanding?

  • How to copy CUdeviceptr planes from FFmpeg AVFrame into my own CUDA memory ?

    25 avril, par SexyBoooom

    I'm trying to copy a frame planes decoded by FFmpeg(CUDA) into my own CUDA memory, but i tried a lot of times and all failed.

    1, from FFmpeg doc, it says:

    /// HW acceleration through CUDA. data[i] contain CUdeviceptr pointers exactly as for system memory frames.
    @AV_PIX_FMT_CUDA = 117
    

    that means AVFrame.data[n] are the CUdeviceptr to copy from.

    2, i have created my own CUDA memory without problem:

    var dstPtr = new CUdeviceptr();
    var error = cuMemAlloc_v2(ref dstPtr, length);
    

    3, try to copy from AVFrame into dstPtr, but failed:

    var dstPtr = new CUdeviceptr();
    var error = cuMemAlloc_v2(ref dstPtr, length);  // allocate a CUDA mem for test
    
    var plane = ffmpeg.av_frame_get_plane_buffer(avFrame, 0);  // get the plane 0 info
    var srcPtr = new CUdeviceptr((long)plane->data);  // get CUdeviceptr from data[0]
    
    error = cuMemcpyDtoD_v2(dstPtr, srcPtr, plane->size);  // try to copy from avFrame into my own CUDA mem block
    Debug.Assert(error == CUResult.Success);  //  alway fail.....
    

    the copy result is alwasy ErrorInvalidValue indicates that the CUdeviceptr returned from AVFrame.data[0] is invalid or something....

    enter image description here

    I really have no idea what am i doing wrong.... BTW, I have created a CUDA context for memory allocating, that said, i will set my own CUDA context as current before allocating memory, does this operation break the FFmpeg's current context? so the copy will fail?

    but anyway, no matter i use cuCtxPushCurrent_v2 or cuCtxSetCurrent, both not work. this is just a really simple copy test, no? but why.... :(

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