Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • ffmpeg : how to prevent wait in case of error

    14 février, par xrfang

    I use ffmpeg to add watermark to images/videos, using the following command line:

    ffmpeg -y -f mjpeg -i input -i /tmp/watermark.png -filter_complex "..." -f mjpeg output
    

    The problem is, in case the input file is not a jpeg file, the process will HANG instead of quit with an error:

    Press [q] to stop, [?] for help
    [mjpeg @ 0x126dc60] dqt: len 28602 is too large
    Error while decoding stream #0:0: Invalid data found when processing input
    frame=    0 fps=0.0 q=0.0 size=       0kB time=-577014:32:22.77 bitrate=  -0.0kbits/s speed=N/A
    

    In my situation, the file has no extension, and I have to specify -f. In case I got the file type wrong, it is an acceptable bug, which I can fix as soon as I found such problem. However it is not good for me if ffmpeg hangs without returning to the parent process, because I have a very large queue of files to process, and I don't want any bug to block the processing task.

  • I am trying to append the video segments to an existing video segment file, so that multiple videos can be streamed at once. How to achieve it ?

    14 février, par Prafulla Nayak

    Here is what I do:

    1. Fetch the existing m3u8 file
    2. Generate the segments to the new video file.
    3. fetch old segments from the m3u8 file and combine the new segments details. and then add it in between under "mergedSegments" as mentioned below.
    #EXTM3U
    
    #EXT-X-VERSION:3
    
    #EXT-X-TARGETDURATION:10
    
    #EXT-X-MEDIA-SEQUENCE:${newMediaSequence}
    
    #EXT-X-PLAYLIST-TYPE:VOD
    
    ${mergedSegments}
    
    #EXT-X-ENDLIST
    

    While doing so, everything works fine. But when I play it, The 1st video gets played without skipping any segments. But afterwords, few segments are getting skipped.

    Why it that? Is there any other effective way to do this?

        ffmpeg(tempFilePath)
            .outputOptions([
              '-codec:v libx264',
              '-codec:a aac',
              '-map 0',
              '-f segment',
              '-segment_time 10',
              '-reset_timestamps 1'
            ])
            .output(m3u8FilePath)
            .outputOptions([
              `-start_number ${startNumber}`, // Sets the starting number for the HLS segments.
              '-hls_time 10', // Duration of each HLS segment in seconds.
              '-hls_list_size 0', // Ensures all segments are included in the playlist.
              '-hls_playlist_type vod', // Designates the playlist as VOD and includes the EXT-X-ENDLIST tag.
              '-f hls', // Specifies the output format as HLS (HTTP Live Streaming).
            ])
            .on('end', async () => {
              fs.unlinkSync(tempFilePath); // Remove the temporary file
    
              const segmentFiles = fs.readdirSync(outputDir);
              const uploadPromises = segmentFiles.map((file) => {
                // if the file is a m3u8 file, update the content with the new segments
                if (file.endsWith(".m3u8") && doc.exists  && existingSegmentUrls.length > 0) {
                  
                  
                  const m3u8ContentPath = path.join(outputDir, file);
                  const newContent = fs.readFileSync(m3u8ContentPath, "utf8");
                  const oldSegments = m3u8Content.match(/#EXTINF:[\d.]+,\n[^\n]+/g);
                  const newSegments = newContent.match(/#EXTINF:[\d.]+,\n[^\n]+/g);
                  const newMediaSequence = oldSegments.length;
                  //log both old and new segments
                  //log newcontent
                  console.log("------newContent------",newContent);
                  console.log("old",oldSegments);
                  console.log("new",newSegments);
    
                  const mergedSegments = [...oldSegments, ...newSegments].join(
                    "\n"
                  );
                  console.log("merged",mergedSegments);
                  const mergedM3u8Content = `#EXTM3U
    #EXT-X-VERSION:3
    #EXT-X-TARGETDURATION:10
    #EXT-X-MEDIA-SEQUENCE:${newMediaSequence}
    #EXT-X-PLAYLIST-TYPE:VOD
    ${mergedSegments}
    #EXT-X-ENDLIST
    
  • How to extract the 1st frame and restore as an image with ffmpeg ? [closed]

    14 février, par lex

    Anyone knows the trick?

    And how to install ffmpeg ? yum install mpeg only returns this:

    ======================================================================================== Matched: mpeg ========================================================================================
    libiec61883.i386 : Streaming library for IEEE1394
    libiec61883.x86_64 : Streaming library for IEEE1394
    qffmpeg-devel.i386 : Development package for qffmpeg
    qffmpeg-devel.x86_64 : Development package for qffmpeg
    qffmpeg-libs.i386 : Libraries for qffmpeg
    qffmpeg-libs.x86_64 : Libraries for qffmpeg
    
  • Using swr_convert to resample audio frames resulted in audio with significant noise [closed]

    14 février, par Bruce Hu

    I attempted to use swr_convert to resample audio frames from 44100 Hz to 16000 Hz, processing the frames one by one for testing purposes. However, the resulting audio appears to be mixed with noise.

    The following code shows how I resample a single audio frame:

    
       AVFrame* output_frame = av_frame_alloc();
        if (!output_frame) {
            throw std::runtime_error("Failed to allocate output AVFrame");
        }
    
        AVRational output_time_base = {1, output_sample_rate};
        
        SwrContext* swr_ctx = swr_alloc();
        if (!swr_ctx) {
            av_frame_free(&output_frame);
            throw std::runtime_error("Failed to allocate SwrContext");
        }
    
        av_opt_set_int(swr_ctx, "in_sample_fmt", aframe->format, 0);
        av_opt_set_int(swr_ctx, "out_sample_fmt", output_format, 0);
        av_opt_set_int(swr_ctx, "in_channel_layout", aframe->channel_layout, 0);
        av_opt_set_int(swr_ctx, "out_channel_layout", output_channel_layout, 0);
        av_opt_set_int(swr_ctx, "in_sample_rate", aframe->sample_rate, 0);
        av_opt_set_int(swr_ctx, "out_sample_rate", output_sample_rate, 0);
    
        if (swr_init(swr_ctx) < 0) {
            swr_free(&swr_ctx);
            av_frame_free(&output_frame);
            throw std::runtime_error("Failed to initialize SwrContext");
        }
    
        output_frame->format = output_format;
        output_frame->channel_layout = output_channel_layout;
        output_frame->sample_rate = output_sample_rate;
    
        int64_t delay = swr_get_delay(swr_ctx, aframe->sample_rate);
        output_frame->nb_samples = av_rescale_rnd(
            delay + aframe->nb_samples, output_sample_rate, aframe->sample_rate, AV_ROUND_UP);
            
        output_frame->nb_samples = av_rescale_rnd(
            aframe->nb_samples, output_sample_rate, aframe->sample_rate, AV_ROUND_UP);
    
        if (av_frame_get_buffer(output_frame, 0) < 0) {
            swr_free(&swr_ctx);
            av_frame_free(&output_frame);
            throw std::runtime_error("Failed to allocate buffer for output AVFrame");
        }
    
        int ret = swr_convert(swr_ctx,
                              output_frame->data, output_frame->nb_samples,
                              (const uint8_t**)aframe->data, aframe->nb_samples);
    
        if (ret < 0) {
            swr_free(&swr_ctx);
            av_frame_free(&output_frame);
            throw std::runtime_error("Failed to resample audio data");
        }
    
        if (aframe->pts != AV_NOPTS_VALUE) {
            output_frame->pts = av_rescale_q(aframe->pts, {1, aframe->sample_rate}, output_time_base);
        } else {
            output_frame->pts = AV_NOPTS_VALUE;
        }
    
  • What tool can I use to analyze audio and clip webm files ? [closed]

    14 février, par Phillip Feldman

    I have a bunch of 2-3 hour long podcast webms from Youtube. I want to extract the audio from them, and programmatically analyze things like volume, or who's speaking. I then want to use my analysis to clip the original webms. What tools can I use to do this, and is size of the audio files something that I need to consider? I know python and have used ffmpeg.