Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • color of soundwaves in ffmpeg [closed]

    20 janvier, par Jad Alaouie

    I am trying to create a video that has a background image and an audio and I want to display the soundwaves in the middle of the video and in the center I have a play button. I am done with 99% of the work but still stuck on the soundwaves colors, which is gray somehow but I want them to be white, and is it possible to make the soundwave smoother?

    filter_complex = (
    "[0:a]showwaves=s=700x300:mode=line:colors=white[v]:draw=scale:scale=sqrt[sw_left];"  
    "[0:a]showwaves=s=700x300:mode=line:colors=white[v]:draw=scale:scale=sqrt[sw_right];"  
    "[1][sw_left]overlay=(W-w)/2-500:(H-h)/2:format=auto[bg_left];"
    "[bg_left][sw_right]overlay=(W+w)/2-200:(H-h)/2:format=auto[bg_with_waves];"
    "[2]scale={0}:{1}[play_button];"
    "[bg_with_waves][play_button]overlay=(W-w)/2:(H-h)/2:format=auto[bg_with_play_button];"
    "[bg_with_play_button]format=yuv420p[v]"  
    ).format(play_button_width, play_button_height)
    
    ffmpeg_command = [
    "ffmpeg", "-hwaccel", "cuda", "-i", audio_path, "-i", background_path, "-i", play_button_path, 
    "-filter_complex", filter_complex,
    "-map", "[v]", "-map", "0:a", "-c:v", "h264_nvenc", "-preset", "p7", 
    "-c:a", "aac", "-b:a", "192k", "-movflags", "+faststart", "output.mp4"
     ]
    
  • How to reduce the size of the .wav file exported from Pydub

    20 janvier, par Dayo

    When I do a conversion using ffmpeg, I am able to convert a 3.5MB mp3 file into an ~3.5MB wav file (using ffmpeg -i input.mp3 -acodec pcm_s16le -ac 1 -ar 8000 output.wav). When I use pydub however with the below code

    s1 = AudioSegment.from_file("input.mp3", format="mp3")
    o1 = s1.export("output.wav",format="wav", parameters=["-acodec","pcm_s16le","-ac","1","-ar","8000"])
    

    the exported wav file is 34.5MB. How can I get pydub to behave as expected?

  • How to run encoding and decoding sequentially with ffmpeg

    20 janvier, par Minwoo Kim

    After creating FFmpeg processes for encoding and decoding in C#, I tried to handle the following process. I also applied Thread.Sleep(1000/30) because I wanted to do it in 1/30 second intervals instead of 1 second.

    1. Get dummy raw data
    2. Stream to ffmpeg process for encoding
    3. Read encoded data and stream it to the ffmpeg process for decoding
    4. Outputting byte size to the console

    Here's my actual code, why doesn't it work?

    internal byte[] GetFrame(int width, int height)
            {
                int frameSize = width * height * 3;
    
                byte[] frameData = new byte[frameSize];
    
                Random random = new Random();
    
                for(int i = 0; i < frameData.Length; i++)
                {
                    frameData[i] = (byte)random.Next(256);
                }
    
                return frameData;
            }
    
    internal void EncodeAndDecode(int width, int height, int fps, int bitrate, string codec, string preset, string tune)
            {
                Process ffmpegEncodeProcess = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "ffmpeg",
                        Arguments = $"-f rawvideo -pixel_format rgb24 -s 540x720 -r 30 " +
                                    $"-i - -c:v libx264 -preset ultrafast -tune zerolatency -maxrate 1500k -b:v 1500k " +
                                    $"-f h264 pipe:1",
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        UseShellExecute = false,      
                        CreateNoWindow = true  
                    }
                };
    
                Process ffmpegDecodeProcess = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "ffmpeg",
                        Arguments = $"-f h264 -i pipe:0 " +
                                    $"-f rawvideo -pixel_format rgb24 -s 540x720 pipe:1",
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    }
                };
    
                //start process
                ffmpegEncodeProcess.Start();
                ffmpegDecodeProcess.Start();
    
                //input stream
                var ffmpegEncodeInput = ffmpegEncodeProcess.StandardInput.BaseStream;
                var ffmpegDecodeInput = ffmpegDecodeProcess.StandardInput.BaseStream;
    
                //output stream
                var ffmpegEncodeOutput = ffmpegEncodeProcess.StandardOutput.BaseStream;
                var ffmpegDecodeOutput = ffmpegDecodeProcess.StandardOutput.BaseStream;
    
                new Thread(async () =>
                {
                    try
                    {
                        while(true)
                        {
                            byte[] encodedChunk = new byte[width * height * 3];
                            byte[] decodedChunk = new byte[width * height * 3];
    
                            byte[] frameData = GetFrame(width, height);
    
                            Console.WriteLine($"[INFO] Original frame size: {frameData.Length}bytes.");
                            
                            if(frameData.Length == 0)
                            {
                                break;
                            }
    
                            await ffmpegEncodeInput.WriteAsync(frameData, 0, frameData.Length);
                            await ffmpegEncodeInput.FlushAsync();
    
                            int encodedBytesRead = await ffmpegEncodeOutput.ReadAsync(encodedChunk, 0, encodedChunk.Length);
                            
                            Console.WriteLine($"[INFO] Encoded frame size: {encodedBytesRead}bytes.");
    
                            if(encodedBytesRead > 0)
                            {
                                await ffmpegDecodeInput.WriteAsync(encodedChunk, 0, encodedBytesRead);
                                await ffmpegDecodeInput.FlushAsync();
    
                                int decodedBytesRead = await ffmpegDecodeOutput.ReadAsync(decodedChunk, 0, decodedChunk.Length);
    
                                Console.WriteLine($"[INFO] Decoded frame size: {decodedBytesRead}bytes.");
    
                                if(decodedBytesRead > 0)
                                {
                                    //do something...
                                }
                            }
    
                            Thread.Sleep(1000 / fps); //1/30s
                        }
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    finally
                    {
                        ffmpegEncodeInput.Close();
                        ffmpegDecodeInput.Close();
    
                        if(!ffmpegEncodeProcess.HasExited)
                        {
                            ffmpegEncodeProcess.Kill();
                        }
    
                        if(!ffmpegDecodeProcess.HasExited)
                        {
                            ffmpegDecodeProcess.Kill();
                        }
    
                        Console.WriteLine("[INFO]: FFmpeg process clean up.");
                    }
                }).Start();
            }
    

    To be precise, it only outputs the "Original" and "Encoded" sizes once to the console and then doesn't do anything. What am I doing wrong?

  • Why is my FastAPI process being suspended, and how can I avoid this ?

    19 janvier, par blermen

    I'm working on a web app using FastAPI that uses ffmpeg to overlay audio onto video for the user. I'm running into an issue where, when I use subprocess.run(cmd), it automatically suspends the process running my FastAPI app. I can't figure out how to get the error logs to help deduce why this is, and I haven't found anything online talking about this.

    @app.get("/overlay-audio/")
    async def get_video(audio_file: str, forged_name: Annotated[str, Query()] = "default"):
        video_path = os.path.join(output_path, "sample.mp4")
        audio_path = os.path.join(output_path, audio_file)
        forged_path = os.path.join(output_path, forged_name + ".mp4")
        print("Video path: " + video_path)
        print("Audio path: " + audio_path)
        print("Output path: " + forged_path)
    
        # command to recreate
        # ffmpeg -i input.mp4 -i input.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4
    
        cmd = ["/opt/homebrew/bin/ffmpeg", 
               "-i", video_path,
               "-i", audio_path,
               "-c:v", "copy",
               "-map", "0:v:0",
               "-map", "1:a:0",
               "-c:a", "aac",
               "-b:a", "192k",
               forged_path]
        
        subprocess.run(cmd)
               
        return {"forged_vid": f"forged_{forged_name}"}
    
    
    if __name__ == "__main__":
        uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
    

    I've tried not writing output to the terminal, as I've read that could be a reason why it suspends using result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE), and I've also tried running it asynchronously to avoid blocking the event loop using

    result = await asyncio.create_subprocess_exec(
            *cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
    

    but nothing works. Any help or possible other ways to go about this would be greatly appreciated. Terminal output about the suspension: [1] + 12526 suspended (tty output) "/Users//Tech Projects/project/tts/videnv/bin/python"

  • Add subtitles and storyboards to mpeg dash [closed]

    19 janvier, par ahamid555

    I need to stream a youtube video in my website and I need to do it not using the embed mode. I tried several steps and I failed to make it work completely. Please provide me any solutions for each step if you know.

    1. I tried to download the video in mpeg-dash format (with manifest and all chunk files) and I failed. Is it possible at first place?
    2. I downloaded the video using ytdlp and I transcoded the movie to mpeg-dash format using ffmpeg, but I couldn't add subtitles and storyboards during the transcode, because the vvt subtitles are not compatible with mp4 containers.
    3. I decided to transcode the video first and then add the subtitles and storyboards using other tools (MP4Box) which I failed, too. Do you know any tool that does it as I want?