Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • How do I configure codec parameters for a RTSP stream ?

    16 mai, par Kermit

    I am trying to stream a network camera source into /dev/video0. However, all attempts keep telling me I have set incorrect codec parameters. Any suggestions?

    # works
    ffplay rtsp://admin:password@172.16.0.184:554/live
    

    Then trying any of these to feed into /dev/video0 (valid from list of ls /dev/video*):

    ffmpeg -rtsp_transport tcp -i "rtsp://admin:password@172.16.0.184:554/live" -c:v rawvideo -pix_fmt yuv420p -f v4l2 /dev/video0
    

    Results in

    ffmpeg version 5.1.4-0+rpt3+deb12u1 Copyright (c) 2000-2023 the FFmpeg developers
      built with gcc 12 (Debian 12.2.0-14)
      configuration: --prefix=/usr --extra-version=0+rpt3+deb12u1 --toolchain=hardened --incdir=/usr/include/aarch64-linux-gnu --enable-gpl --disable-stripping --disable-mmal --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sand --enable-sdl2 --disable-sndio --enable-libjxl --enable-neon --enable-v4l2-request --enable-libudev --enable-epoxy --libdir=/usr/lib/aarch64-linux-gnu --arch=arm64 --enable-pocketsphinx --enable-librsvg --enable-libdc1394 --enable-libdrm --enable-vout-drm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
      libavutil      57. 28.100 / 57. 28.100
      libavcodec     59. 37.100 / 59. 37.100
      libavformat    59. 27.100 / 59. 27.100
      libavdevice    59.  7.100 / 59.  7.100
      libavfilter     8. 44.100 /  8. 44.100
      libswscale      6.  7.100 /  6.  7.100
      libswresample   4.  7.100 /  4.  7.100
      libpostproc    56.  6.100 / 56.  6.100
    Input #0, rtsp, from 'rtsp://admin:password@172.16.0.184:554/live':
      Metadata:
        title           : Media Server
      Duration: N/A, start: 0.000000, bitrate: N/A
      Stream #0:0: Video: hevc (Main), yuv420p(tv), 3840x2160, 30 fps, 100 tbr, 90k tbn
      Stream #0:1: Audio: aac (LC), 32000 Hz, mono, fltp
    Stream mapping:
      Stream #0:0 -> #0:0 (hevc (native) -> rawvideo (native))
    Press [q] to stop, [?] for help
    [video4linux2,v4l2 @ 0x55562d9faa70] ioctl(VIDIOC_G_FMT): Invalid argument
    Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
    Error initializing output stream 0:0 -- 
    Conversion failed!
    
  • Generating timestamps in webm transport stream with ffmpeg

    16 mai, par Vlad Feinstein

    I am recording screen capture of my Electron app to a file, as suggested here: Save captured video to file in Electron

    It works great, but the file is a “transport stream”. I can play it in Chrome browser, but can’t adjust the time slider.

    The suggestion was to use ffmpeg to post-process the file. The simplest, straight-forward command I found is:

    ffmpeg -fflags +genpts -i in.webm out.webm
    

    I thought that this simply generates timestamps. However, the out.webm file is 15 times smaller than in.webm! I don’t see any change in the quality. The downside is - the processing takes about the same time as the duration of the video.

    Two questions:

    1. Is there any way I can speedup this “timestamping” process?
    2. Can I record a video that is already 15 times smaller in size?

    I specify maxFrameRate: 30 when calling webkitGetUserMedia().

    Here is the output of that ffmpeg command:

    vlad$ ffmpeg -fflags +genpts -i in.webm out.webm
    ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
      built with Apple clang version 12.0.0 (clang-1200.0.32.29)
      configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
      libavutil      56. 51.100 / 56. 51.100
      libavcodec     58. 91.100 / 58. 91.100
      libavformat    58. 45.100 / 58. 45.100
      libavdevice    58. 10.100 / 58. 10.100
      libavfilter     7. 85.100 /  7. 85.100
      libavresample   4.  0.  0 /  4.  0.  0
      libswscale      5.  7.100 /  5.  7.100
      libswresample   3.  7.100 /  3.  7.100
      libpostproc    55.  7.100 / 55.  7.100
    Input #0, matroska,webm, from 'in.webm':
      Metadata:
        encoder         : Chrome
      Duration: N/A, start: 0.000000, bitrate: N/A
        Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv), 2560x1416, SAR 1:1 DAR 320:177, 1k tbr, 1k tbn, 1k tbc (default)
        Metadata:
          alpha_mode      : 1
    Stream mapping:
      Stream #0:0 -> #0:0 (vp9 (native) -> vp9 (libvpx-vp9))
    Press [q] to stop, [?] for help
    [libvpx-vp9 @ 0x7f85f2012600] v1.9.0
    [libvpx-vp9 @ 0x7f85f2012600] Neither bitrate nor constrained quality specified, using default CRF of 32
    Output #0, webm, to 'out.webm':
      Metadata:
        encoder         : Lavf58.45.100
        Stream #0:0(eng): Video: vp9 (libvpx-vp9), yuv420p, 2560x1416 [SAR 1:1 DAR 320:177], q=-1--1, 1k fps, 1k tbn, 1k tbc (default)
        Metadata:
          alpha_mode      : 1
          encoder         : Lavc58.91.100 libvpx-vp9
        Side data:
          cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
    frame=  209 fps=5.8 q=0.0 Lsize=     881kB time=00:00:17.81 bitrate= 405.0kbits/s speed=0.494x    
    video:879kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.231567%
    

    And here is the information on both files from ffprobe:

    vlad$ ffprobe in.webm
    ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
      built with Apple clang version 12.0.0 (clang-1200.0.32.29)
      configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
      libavutil      56. 51.100 / 56. 51.100
      libavcodec     58. 91.100 / 58. 91.100
      libavformat    58. 45.100 / 58. 45.100
      libavdevice    58. 10.100 / 58. 10.100
      libavfilter     7. 85.100 /  7. 85.100
      libavresample   4.  0.  0 /  4.  0.  0
      libswscale      5.  7.100 /  5.  7.100
      libswresample   3.  7.100 /  3.  7.100
      libpostproc    55.  7.100 / 55.  7.100
    Input #0, matroska,webm, from 'in.webm':
      Metadata:
        encoder         : Chrome
      Duration: N/A, start: 0.000000, bitrate: N/A
        Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv), 2560x1416, SAR 1:1 DAR 320:177, 1k tbr, 1k tbn, 1k tbc (default)
        Metadata:
          alpha_mode      : 1
    

    and

    vlad$ ffprobe out.webm
    ffprobe version 4.3.2 Copyright (c) 2007-2021 the FFmpeg developers
      built with Apple clang version 12.0.0 (clang-1200.0.32.29)
      configuration: --prefix=/usr/local/Cellar/ffmpeg/4.3.2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
      libavutil      56. 51.100 / 56. 51.100
      libavcodec     58. 91.100 / 58. 91.100
      libavformat    58. 45.100 / 58. 45.100
      libavdevice    58. 10.100 / 58. 10.100
      libavfilter     7. 85.100 /  7. 85.100
      libavresample   4.  0.  0 /  4.  0.  0
      libswscale      5.  7.100 /  5.  7.100
      libswresample   3.  7.100 /  3.  7.100
      libpostproc    55.  7.100 / 55.  7.100
    Input #0, matroska,webm, from 'out.webm':
      Metadata:
        ENCODER         : Lavf58.45.100
      Duration: 00:00:17.82, start: 0.000000, bitrate: 405 kb/s
        Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv), 2560x1416, SAR 1:1 DAR 320:177, 1k tbr, 1k tbn, 1k tbc (default)
        Metadata:
          ALPHA_MODE      : 1
          ENCODER         : Lavc58.91.100 libvpx-vp9
          DURATION        : 00:00:17.815000000
    
  • C# server problem with FFmpeg Visual Studio 2022

    16 mai, par seanofdead

    Hello everyone I've spent countless hours on my server I'm trying to create that will share video from one stream to another client computer. That is the goal anyway. I'm using Visual Studio 2022 most recent update. Currently this is my code

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading.Tasks;
    using FFmpeg.AutoGen;
    using NLog;
    
    public class VideoStreamServer
    {
        private const int Port = 5000;
        private TcpListener server;
        private FFmpegVideoEncoder encoder;
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
    
        public VideoStreamServer()
        {
            // Initialize FFmpeg network components
            Console.WriteLine("Initializing FFmpeg network components...");
            int result = ffmpeg.avformat_network_init();
            if (result != 0)
            {
                throw new ApplicationException($"Failed to initialize FFmpeg network components: {result}");
            }
    
            // Initialize encoder
            encoder = new FFmpegVideoEncoder(1920, 1080, AVCodecID.AV_CODEC_ID_H264); // Screen size & codec
        }
    
        public static void Main()
        {
            // Initialize NLog
            var logger = LogManager.GetCurrentClassLogger();
            logger.Info("Application started");
    
            var server = new VideoStreamServer();
            server.Start().Wait();
        }
    
        public async Task Start()
        {
            server = new TcpListener(IPAddress.Any, Port);
            server.Start();
            logger.Info($"Server started on port {Port}...");
    
            try
            {
                while (true)
                {
                    TcpClient client = await server.AcceptTcpClientAsync();
                    logger.Info("Client connected...");
                    _ = HandleClientAsync(client);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "An error occurred");
            }
        }
    
        private async Task HandleClientAsync(TcpClient client)
        {
            using (client)
            using (NetworkStream netStream = client.GetStream())
            {
                try
                {
                    await encoder.StreamEncodedVideoAsync(netStream);
                }
                catch (IOException ex)
                {
                    logger.Error(ex, $"Network error: {ex.Message}");
                    // Additional logging and recovery actions
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $"Unexpected error: {ex.Message}");
                    // Log and handle other exceptions
                }
                finally
                {
                    // Ensure all resources are cleaned up properly
                    client.Close();
                    logger.Info("Client connection closed properly.");
                }
            }
        }
    }
    
    public class FFmpegVideoEncoder
    {
        private readonly int width;
        private readonly int height;
        private readonly AVCodecID codecId;
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
    
        public FFmpegVideoEncoder(int width, int height, AVCodecID codecId)
        {
            this.width = width;
            this.height = height;
            this.codecId = codecId;
            InitializeEncoder();
        }
    
        private void InitializeEncoder()
        {
            // Initialize FFmpeg encoder here
            logger.Debug("FFmpeg encoder initialized");
        }
    
        public async Task StreamEncodedVideoAsync(NetworkStream netStream)
        {
            // Initialize reusable buffers for efficiency
            byte[] buffer = new byte[4096];
    
            // Capture and encode video frames
            while (true)
            {
                try
                {
                    // Simulate capture and encoding
                    byte[] videoData = new byte[1024]; // This would come from the encoder
    
                    // Simulate streaming
                    for (int i = 0; i < videoData.Length; i += buffer.Length)
                    {
                        int chunkSize = Math.Min(buffer.Length, videoData.Length - i);
                        Buffer.BlockCopy(videoData, i, buffer, 0, chunkSize);
                        await netStream.WriteAsync(buffer, 0, chunkSize);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex, "Error streaming video data");
                    throw; // Rethrow to handle in the calling function
                }
    
                await Task.Delay(1000 / 30); // For 30 FPS
            }
        }
    }
    

    When I build no issues but when i run it in debug mode I get this error

    System.DllNotFoundException
      HResult=0x80131524
      Message=Unable to load DLL 'avformat.59 under C:\Users\phlfo\Downloads\cfolder\server\ConsoleApp\bin\Debug\': The specified module could not be found.
      Source=FFmpeg.AutoGen
      StackTrace:
       at FFmpeg.AutoGen.ffmpeg.LoadLibrary(String libraryName, Boolean throwException)
       at FFmpeg.AutoGen.ffmpeg.<>c.<.cctor>b__7_0(String libraryName)
       at FFmpeg.AutoGen.ffmpeg.<>c.<.cctor>b__7_242()
       at FFmpeg.AutoGen.ffmpeg.avformat_network_init()
       at VideoStreamServer..ctor() in C:\Users\phlfo\Downloads\cfolder\server\ConsoleApp\Program.cs:line 20
       at VideoStreamServer.Main() in C:\Users\phlfo\Downloads\cfolder\server\ConsoleApp\Program.cs:line 36
    

    I have FFmpeg.AutoGen package installed through VS i originally started with 7.0 but then switched to 5.0 to see if an older version might work (it did not). I also have the files in the same directory as the .exe for testing purposes as seen in the screenshot. Can someone please help me figure out this error. enter image description here

  • PHP/ffmpeg (Windows 7) : - Permission Denied. Tried everything, but still doesn't work

    16 mai, par user365465

    I have a command-line PHP script that, when given the folder name as an argument when executing the script, will find all images in said folder and resize them using FFmpeg. Problem is, I always get a Permission Denied error in FFmpeg when it attempts to get at the files within the folder. This is while being on Windows 7.

    The big problem is that I've tried every solution that keeps popping up on the interwebs when I search for how to fix it. I've changed the settings in Avast!, did the attrib command (both as normal and "Run as Administrator") to unmark as read-only, took ownership of the folder both via command line and using that GUI takeown context menu thing everyone keeps throwing around (obviously before my efforts to mark everything as not-read-only), messed with permissions, everything. I keep searching and searching for a solution, but all of the sites keep throwing out the same things over and over again - things that I've already tried multiple times. I'm at the end of my rope here. All I want to do is resize images quickly and automatically so I can put them up on my website, yet this has been a massive headache...

    Please help me! :(

    Update (Additional Points):

    • The error is through FFmpeg, not PHP. FFmpeg spits out the permission denied error, the script just continues doing what it does normally (or, at least, it would if it weren't for my die statement that kicks in in the event of FFmpeg failing).

    • FFmpeg will execute just fine if it's run normally through the command line instead of through the script. However, this does not help the problem of me having to resize hundreds of photos by hand!

    • I'm just running this script in the normal terminal using the CLI engine. Weird use for PHP, I know, but since I already knew how to work with FFmpeg in PHP (but in Linux!) so I figured I might as well use the code as a normal, stand-alone program...

    • FFmpeg spits out the same error regardless of which PHP function is used to execute it. It happens for shell_exec, exec, passthru, and system.

  • How to stop ffmpeg remotely ?

    16 mai, par Adam

    I'm running ffmpeg on another machine for screen capture. I'd like to be able to stop it recording remotely. FFMPEG requires that q is pressed to stop encoding as it has to do some finalization to finish the file cleanly. I know I could kill it with kill/killall however this can lead to corrupt videos.

    Press [q] to stop encoding
    

    I can't find anything on google specifically for this, but some there is suggestion that echoing into /proc//fd/0 will work.

    I've tried this but it does not stop ffmpeg. The q is however shown in the terminal in which ffmpeg is running.

    echo -n q > /proc/16837/fd/0
    

    So how can I send a character to another existing process in such a way it is as if it were typed locally? Or is there another way of remotely stopping ffmpeg cleanly.