Recherche avancée

Médias (33)

Mot : - Tags -/creative commons

Autres articles (50)

  • La file d’attente de SPIPmotion

    28 novembre 2010, par

    Une file d’attente stockée dans la base de donnée
    Lors de son installation, SPIPmotion crée une nouvelle table dans la base de donnée intitulée spip_spipmotion_attentes.
    Cette nouvelle table est constituée des champs suivants : id_spipmotion_attente, l’identifiant numérique unique de la tâche à traiter ; id_document, l’identifiant numérique du document original à encoder ; id_objet l’identifiant unique de l’objet auquel le document encodé devra être attaché automatiquement ; objet, le type d’objet auquel (...)

  • Contribute to documentation

    13 avril 2011

    Documentation is vital to the development of improved technical capabilities.
    MediaSPIP welcomes documentation by users as well as developers - including : critique of existing features and functions articles contributed by developers, administrators, content producers and editors screenshots to illustrate the above translations of existing documentation into other languages
    To contribute, register to the project users’ mailing (...)

  • Selection of projects using MediaSPIP

    2 mai 2011, par

    The examples below are representative elements of MediaSPIP specific uses for specific projects.
    MediaSPIP farm @ Infini
    The non profit organizationInfini develops hospitality activities, internet access point, training, realizing innovative projects in the field of information and communication technologies and Communication, and hosting of websites. It plays a unique and prominent role in the Brest (France) area, at the national level, among the half-dozen such association. Its members (...)

Sur d’autres sites (5429)

  • ffmpeg-python Unable to open .srt, .vtt files. Error initializing filter 'subtitles' with args ... Error initializing complex filters

    3 novembre 2022, par Batuhan Yılmaz

    I'm trying to build a web app where users can upload a video file and a transcript as .srt or .vtt file to get a video with subtitles. But keep getting an error with the subtitles.
    
Here's my code :

    


    import streamlit as st
from streamlit_lottie import st_lottie
from utils import write_vtt, write_srt
import ffmpeg
import requests
from typing import Iterator
from io import StringIO
import numpy as np
import pathlib
import os
import components.authenticate as authenticate


st.set_page_config(page_title="Auto Subtitled Video Generator", page_icon=":movie_camera:", layout="wide")

# Define a function that we can use to load lottie files from a link.
@st.cache(allow_output_mutation=True)
def load_lottieurl(url: str):
    r = requests.get(url)
    if r.status_code != 200:
        return None
    return r.json()


APP_DIR = pathlib.Path(__file__).parent.absolute()

LOCAL_DIR = APP_DIR / "local_transcript"
LOCAL_DIR.mkdir(exist_ok=True)
save_dir = LOCAL_DIR / "output"
save_dir.mkdir(exist_ok=True)


col1, col2 = st.columns([1, 3])
with col1:
    lottie = load_lottieurl("https://assets1.lottiefiles.com/packages/lf20_HjK9Ol.json")
    st_lottie(lottie)

with col2:
    st.write("""
    ## Auto Subtitled Video Generator 
    ##### Upload a video file and a transcript as .srt file and get a video with subtitles.""")


def getSubs(segments: Iterator[dict], format: str, maxLineWidth: int) -> str:
    segmentStream = StringIO()

    if format == 'vtt':
        write_vtt(segments, file=segmentStream, maxLineWidth=maxLineWidth)
    elif format == 'srt':
        write_srt(segments, file=segmentStream, maxLineWidth=maxLineWidth)
    else:
        raise Exception("Unknown format " + format)

    segmentStream.seek(0)
    return segmentStream.read()


def generate_subtitled_video(uploaded_file):
    with open(f"{save_dir}/input.mp4", "wb") as f:
            f.write(uploaded_file.read())
    audio = ffmpeg.input(f"{save_dir}/input.mp4")
    audio = ffmpeg.output(audio, f"{save_dir}/output.wav", acodec="pcm_s16le", ac=1, ar="16k")
    ffmpeg.run(audio, overwrite_output=True)


def main():
    video_file = st.file_uploader("File", type=["mp4", "avi", "mov", "mkv"])
    # get the name of the input_file
    if video_file is not None:
        filename = video_file.name[:-4]
    else:
        filename = None
    transcript_file = st.file_uploader("Transcript", type=["srt", "vtt"])
    if transcript_file is not None:
        transcript_name = transcript_file.name
    else:
        transcript_name = None
    if video_file is not None and transcript_file is not None:
        if transcript_name[-3:] == "vtt":
            with open(f"{save_dir}/transcript.vtt", "wb") as f:
                f.writelines(transcript_file)
                f.close()
            with open(os.path.join(os.getcwd(), f"{save_dir}/transcript.vtt"), "rb") as f:
                vtt_file = f.read()
            if st.button("Generate Video with Subtitles"):
                generate_subtitled_video(video_file)
                video_file = ffmpeg.input(f"{save_dir}/input.mp4")
                audio_file = ffmpeg.input(f"{save_dir}/output.wav")
                ffmpeg.concat(video_file.filter("subtitles", vtt_file), audio_file, v=1, a=1).output("final.mp4").global_args('-report').run(quiet=True, overwrite_output=True)
                video_with_subs = open("final.mp4", "rb")
                col3, col4 = st.columns([3, 1])
                with col3:
                    st.video(video_with_subs)
                with col4:
                    st.download_button(label="Download Video with Subtitles",
                                        data=video_with_subs,
                                        file_name=f"{filename}_with_subs.mp4")
            else:
                st.error("Please upload a video file and a transcript file.")
        elif transcript_name[-3:] == "srt":
            with open(f"{save_dir}/transcript.srt", "wb") as f:
                f.writelines(transcript_file)
                f.close()
            with open(os.path.join(os.getcwd(), f"{save_dir}/transcript.srt"), "rb") as f:
                srt_file = f.read()
                f.close()
            if st.button("Generate Video with Subtitles"):
                generate_subtitled_video(video_file)
                video_file = ffmpeg.input(f"{save_dir}/input.mp4")
                audio_file = ffmpeg.input(f"{save_dir}/output.wav")
                ffmpeg.concat(video_file.filter("subtitles", f'{save_dir}/transcript.srt'), audio_file, v=1, a=1).output("final.mp4").global_args('-report').run(quiet=True, overwrite_output=True)
                video_with_subs = open("final.mp4", "rb")

                col3, col4 = st.columns([3, 1])
                with col3:
                    st.video(video_with_subs)
                with col4:
                    st.download_button(label="Download Video with Subtitles",
                                        data=video_with_subs,
                                        file_name=f"{filename}_with_subs.mp4")
        else:
            st.error("Please upload a .srt or .vtt file")
    else:
        st.info("Please upload a video file and a transcript file")


if __name__ == "__main__":
    authenticate.set_st_state_vars()
    if st.session_state["authenticated"]:
        main()
        authenticate.button_logout()
    else:
        st.info("Please log in or sign up to use the app.")
        authenticate.button_login()
        



    


    I couldn't figure out what I'm doing wrong. Please help

    


    And the log file of ffmpeg error :

    


    ffmpeg started on 2022-11-03 at 21:29:27
Report written to "ffmpeg-20221103-212927.log"
Log level: 48
Command line:
ffmpeg -i "C:\\Users\\batuh\\Auto-Subtitled-Video-Generator - Copy2\\pages\\local_transcript\\output/input.mp4" -i "C:\\Users\\batuh\\Auto-Subtitled-Video-Generator - Copy2\\pages\\local_transcript\\output/output.wav" -filter_complex "[0]subtitles=C\\\\\\\\\\\\:\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\batuh\\\\\\\\\\\\\\\\Auto-Subtitled-Video-Generator - Copy2\\\\\\\\\\\\\\\\pages\\\\\\\\\\\\\\\\local_transcript\\\\\\\\\\\\\\\\output/transcript.srt[s0];[s0][1]concat=a=1:n=1:v=1[s1]" -map "[s1]" final.mp4 -report -y
ffmpeg version 2022-10-24-git-d79c240196-full_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12.1.0 (Rev2, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --ena  libavutil      57. 39.101 / 57. 39.101
  libavcodec     59. 51.100 / 59. 51.100
  libavformat    59. 34.101 / 59. 34.101
  libavdevice    59.  8.101 / 59.  8.101
  libavfilter     8. 49.101 /  8. 49.101
  libswscale      6.  8.112 /  6.  8.112
  libswresample   4.  9.100 /  4.  9.100
  libpostproc    56.  7.100 / 56.  7.100
Splitting the commandline.
Reading option '-i' ... matched as input url with argument 'C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/input.mp4'.
Reading option '-i' ... matched as input url with argument 'C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/output.wav'.
Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument '[0]subtitles=C\\\\\\:\\\\\\\\Users\\\\\\\\batuh\\\\\\\\Auto-Subtitled-Video-Generator - Copy2\\\\\\\\pages\\\\\\\\local_transcript\\\\\\\\output/transcript.srt[s0];[s0][1]concat=a=1:n=1:v=1[s1]'.
Reading option '-map' ... matched as option 'map' (set input stream mapping) with argument '[s1]'.
Reading option 'final.mp4' ... matched as output url.
Reading option '-report' ... matched as option 'report' (generate a report) with argument '1'.
Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option filter_complex (create a complex filtergraph) with argument [0]subtitles=C\\\\\\:\\\\\\\\Users\\\\\\\\batuh\\\\\\\\Auto-Subtitled-Video-Generator - Copy2\\\\\\\\pages\\\\\\\\local_transcript\\\\\\\\output/transcript.srt[s0];[s0][1]concat=a=1:n=1:v=1[s1].
Applying option report (generate a report) with argument 1.
Applying option y (overwrite output files) with argument 1.
Successfully parsed a group of options.
Parsing a group of options: input url C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/input.mp4.
Successfully parsed a group of options.
Opening an input file: C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/input.mp4.
[NULL @ 000001baaeb55300] Opening 'C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/input.mp4' for reading
[file @ 000001baaeb55800] Setting default whitelist 'file,crypto,data'
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] ISO: File Type Major Brand: mp42
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Unknown dref type 0x206c7275 size 12
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Processing st: 0, edit list 0 - media time: 3003, duration: 8201160
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Offset DTS by 3003 to make first pts zero.
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Setting codecpar->delay to 1 for stream st: 0
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Unknown dref type 0x206c7275 size 12
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] Before avformat_find_stream_info() pos: 34044 bytes read:65536 seeks:0 nb_streams:2
[h264 @ 000001baaeb68bc0] nal_unit_type: 7(SPS), nal_ref_idc: 3
[h264 @ 000001baaeb68bc0] nal_unit_type: 8(PPS), nal_ref_idc: 3
[h264 @ 000001baaeb68bc0] nal_unit_type: 7(SPS), nal_ref_idc: 3
[h264 @ 000001baaeb68bc0] nal_unit_type: 8(PPS), nal_ref_idc: 3
[h264 @ 000001baaeb68bc0] nal_unit_type: 5(IDR), nal_ref_idc: 3
[h264 @ 000001baaeb68bc0] Format yuv420p chosen by get_format().
[h264 @ 000001baaeb68bc0] Reinit context to 1280x720, pix_fmt: yuv420p
[h264 @ 000001baaeb68bc0] no picture 
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] All info found
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001baaeb55300] After avformat_find_stream_info() pos: 109849 bytes read:131072 seeks:0 frames:15
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/input.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-08-24T03:50:36.000000Z
  Duration: 00:01:31.14, start: 0.000000, bitrate: 1149 kb/s
  Stream #0:0[0x1](und), 14, 1/90000: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 1020 kb/s, 29.97 fps, 29.97 tbr, 90k tbn (default)
    Metadata:
      creation_time   : 2016-08-24T03:50:36.000000Z
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und), 1, 1/44100: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      creation_time   : 2016-08-24T03:50:36.000000Z
      handler_name    : ISO Media file produced by Google Inc.
      vendor_id       : [0][0][0][0]
Successfully opened the file.
Parsing a group of options: input url C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/output.wav.
Successfully parsed a group of options.
Opening an input file: C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/output.wav.
[NULL @ 000001baaec0e8c0] Opening 'C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/output.wav' for reading
[file @ 000001baaf24ee40] Setting default whitelist 'file,crypto,data'
[wav @ 000001baaec0e8c0] Format wav probed with size=2048 and score=99
[wav @ 000001baaec0e8c0] Before avformat_find_stream_info() pos: 78 bytes read:65614 seeks:1 nb_streams:1
[wav @ 000001baaec0e8c0] probing stream 0 pp:32
[wav @ 000001baaec0e8c0] probing stream 0 pp:31
[wav @ 000001baaec0e8c0] probing stream 0 pp:30
[wav @ 000001baaec0e8c0] probing stream 0 pp:29
[wav @ 000001baaec0e8c0] probing stream 0 pp:28
[wav @ 000001baaec0e8c0] probing stream 0 pp:27
[wav @ 000001baaec0e8c0] probing stream 0 pp:26
[wav @ 000001baaec0e8c0] probing stream 0 pp:25
[wav @ 000001baaec0e8c0] probing stream 0 pp:24
[wav @ 000001baaec0e8c0] probing stream 0 pp:23
[wav @ 000001baaec0e8c0] probing stream 0 pp:22
[wav @ 000001baaec0e8c0] probing stream 0 pp:21
[wav @ 000001baaec0e8c0] probing stream 0 pp:20
[wav @ 000001baaec0e8c0] probing stream 0 pp:19
[wav @ 000001baaec0e8c0] probing stream 0 pp:18
[wav @ 000001baaec0e8c0] probing stream 0 pp:17
[wav @ 000001baaec0e8c0] probing stream 0 pp:16
[wav @ 000001baaec0e8c0] probing stream 0 pp:15
[wav @ 000001baaec0e8c0] probing stream 0 pp:14
[wav @ 000001baaec0e8c0] probing stream 0 pp:13
[wav @ 000001baaec0e8c0] probing stream 0 pp:12
[wav @ 000001baaec0e8c0] probing stream 0 pp:11
[wav @ 000001baaec0e8c0] probing stream 0 pp:10
[wav @ 000001baaec0e8c0] probing stream 0 pp:9
[wav @ 000001baaec0e8c0] probing stream 0 pp:8
[wav @ 000001baaec0e8c0] probing stream 0 pp:7
[wav @ 000001baaec0e8c0] probing stream 0 pp:6
[wav @ 000001baaec0e8c0] probing stream 0 pp:5
[wav @ 000001baaec0e8c0] probing stream 0 pp:4
[wav @ 000001baaec0e8c0] probing stream 0 pp:3
[wav @ 000001baaec0e8c0] probing stream 0 pp:2
[wav @ 000001baaec0e8c0] probing stream 0 pp:1
[wav @ 000001baaec0e8c0] probed stream 0
[wav @ 000001baaec0e8c0] parser not found for codec pcm_s16le, packets or times may be invalid.
[wav @ 000001baaec0e8c0] max_analyze_duration 5000000 reached at 5120000 microseconds st:0
[wav @ 000001baaec0e8c0] After avformat_find_stream_info() pos: 176206 bytes read:262222 seeks:1 frames:42
Guessed Channel Layout for Input Stream #1.0 : mono
Input #1, wav, from 'C:\Users\batuh\Auto-Subtitled-Video-Generator - Copy2\pages\local_transcript\output/output.wav':
  Metadata:
    encoder         : Lavf59.34.101
  Duration: 00:01:31.14, bitrate: 256 kb/s
  Stream #1:0, 42, 1/16000: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, 1 channels, s16, 256 kb/s
Successfully opened the file.
[Parsed_subtitles_0 @ 000001baaeb7d040] Setting 'filename' to value 'C\:\\Users\\batuh\\Auto-Subtitled-Video-Generator - Copy2\\pages\\local_transcript\\output/transcript.srt'
[Parsed_subtitles_0 @ 000001baaeb7d040] libass API version: 0x1600010
[Parsed_subtitles_0 @ 000001baaeb7d040] libass source: commit: 0.16.0-48-g75a3dbac9bd41842a4d00b0d42c9513e2c8aec67
[Parsed_subtitles_0 @ 000001baaeb7d040] Raster: FreeType 2.12.1
[Parsed_subtitles_0 @ 000001baaeb7d040] Shaper: FriBidi 1.0.12 (SIMPLE) HarfBuzz-ng 5.3.1 (COMPLEX)
[Parsed_subtitles_0 @ 000001baaeb7d040] Initialized
[NULL @ 000001baaf473400] Opening 'C\:\\Users\\batuh\\Auto-Subtitled-Video-Generator - Copy2\\pages\\local_transcript\\output/transcript.srt' for reading
[file @ 000001baaeb7dc40] Setting default whitelist 'file,crypto,data'
[Parsed_subtitles_0 @ 000001baaeb7d040] Unable to open C\:\\Users\\batuh\\Auto-Subtitled-Video-Generator - Copy2\\pages\\local_transcript\\output/transcript.srt
[AVFilterGraph @ 000001baaf24ef40] Error initializing filter 'subtitles' with args 'C\\\:\\\\Users\\\\batuh\\\\Auto-Subtitled-Video-Generator - Copy2\\\\pages\\\\local_transcript\\\\output/transcript.srt'
Error initializing complex filters.
Invalid argument
[AVIOContext @ 000001baaeb5f880] Statistics: 131072 bytes read, 0 seeks
[AVIOContext @ 000001baaf10b2c0] Statistics: 262222 bytes read, 1 seeks



    


    I tried doing all ffmpeg related things under the generate_subtitled_video function but couldn't make it work either.

    


  • ffmpeg can't stop, when running with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)

    9 novembre 2022, par haizhohuang

    when i use local ffmpeg, it exit normally.

    


    cmd = f'ffmpeg -nostdin -vsync 0 -i {sec_replace(video_path)} -r {marked_paras.get_divide_frame_fps()}' \
      f'-q:v 2 -f image2 {crop_info} {sec_replace(default_frames_dir)}%08d.png'
logger.info(cmd)
p = subprocess.Popen(cmd,
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE, shell=True)

# p.communicate()
command.utp_command("ps aux | grep ffmpeg")
timer = Timer(60, p.kill)
try:
    timer.start()
    stdout, stderr = p.communicate()
finally:
    timer.cancel()
command.utp_command("ffmpeg --version")
logger.info("stdout: {}".format(stdout))
logger.info("stdout: {}".format(stderr))


    


    local stdout
ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers\n built with Apple clang version 14.0.0

    


    stdout: b"ffmpeg version 5.1.2 Copyright (c) 2000-2022 the FFmpeg developers\n  built with Apple clang version 14.0.0 (clang-1400.0.29.102)\n  configuration: --prefix=/usr/local/Cellar/ffmpeg/5.1.2 --enable-shared --enable-pthreads --enable-version3 --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-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --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-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox\n  libavutil      57. 28.100 / 57. 28.100\n  libavcodec     59. 37.100 / 59. 37.100\n  libavformat    59. 27.100 / 59. 27.100\n  libavdevice    59.  7.100 / 59.  7.100\n  libavfilter    =-0.0 size=N/A time=00:00:07.56 bitrate=N/A speed=0.71x    \r[image2 @ 0x7fd891906080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 231 >= 231\nframe=  281 fps= 25 q=-0.0 size=N/A time=00:00:07.90 bitrate=N/A speed=0.708x    \rframe=  291 fps= 25 q=-0.0 size=N/A time=00:00:08.23 bitrate=N/A speed=0.706x    \r[image2 @ 0x7fd891906080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 247 >= 247\nframe=  300 fps= 25 q=-0.0 size=N/A time=00:00:08.50 bitrate=N/A speed=0.698x    \r[image2 @ 0x7fd891906080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 261 >= 261\nframe=  312 fps= 25 q=-0.0 size=N/A time=00:00:08.86 bitrate=N/A speed=0.699x    \r[image2 @ 0x7fd891906080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 276 >= 276\nframe=  325 fps= 25 q=-0.0 size=N/A time=00:00:09.26 bitrate=N/A speed=0.702x    \rframe=  335 fps= 24 q=-0.0 size=N/A time=00:00:09.60 bitrate=N/A speed= 0.7x    \r[image2 @ 0x7fd891906080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 290 >= 290\n[image2 @ 0x7fd891906080] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 305 >= 305\nframe=  344 fps= 24 q=-0.0 Lsize=N/A time=00:00:10.23 bitrate=N/A speed=0.708x    \nvideo:1096936kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown\n"


    


    But when i put it in cloud. it can't stop the subprocess and blocking in communicate util being killed

    


    remote output
ffmpeg version 3.4.11 Copyright (c) 2000-2022 the FFmpeg developers\n built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)

    


    stdout: b"ffmpeg version 3.4.11 Copyright (c) 2000-2022 the FFmpeg developers\n  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)\n  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --docdir=/usr/share/doc/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --extra-cflags=' ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-fontconfig --enable-gcrypt --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libcdio --enable-libdrm --enable-indev=jack --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libmp3lame --enable-nvenc --enable-openal --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libopus --disable-encoder=libopus --enable-libpulse --enable-librsvg --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvidstab --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzvbi --enable-avfilter --enable-avresample --enable-libmodplug --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-libmfx --enable-runtime-cpudetect\n  libavutil      55. 78.100 / 55. 78.100\n  libavcodec     57.107.100 / 57.107.100\n  libavformat    57. 83.100 / 57. 83.100\n  libavdevice    57. 10.100 / 57. 10.100\n  libavfilter     6.107.100 /  6.107.100\n  libavresample   3.  7.  0 /  3.  7.  0\n  libswscale      4.  8.100 /  4.  8.100\n  libswresample   2.  9.100 /  2.  9.100\n  libpostproc    54.  7.100 / 54.  7.100\nInput #0, matroska,webm, from 'case.videoquality.recommend_video_quality_performance_case.recommend_video_quality_test/record_video/com.tencent.mtt/1667903766/test.mkv':\n  Metadata:\n    COMMENT         : Recorded by scrcpy 1.24\n    ENCODER         : Lavf57.83.100\n  Duration: N/A, start: 0.000000, bitrate: N/A\n    Stream #0:0: Video: h264 (Constrained Baseline), yuv420p(progressive), 1080x2240, 1k fps, 59.94 tbr, 1k tbn, 2k tbc (default)\nUsing -vsync 0 and -r can produce invalid output files\nStream mapping:\n  Stream #0:0 -> #0:0 (h264 (native) -> png (native))\nPress [q] to stop, [?] for help\nOutput #0, image2, to 'case.videoquality.recommend_video_quality_performance_case.recommend_video_quality_test/record_video/com.tencent.mtt/1667903766/frames_end/%08d.png':\n  Metadata:\n    COMMENT         : Recorded by scrcpy 1.24\n    encoder         : Lavf57.83.100\n    Stream #0:0: Video: png, rgb24, 1080x2240, q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc (default)\n    Metadata:\n      encoder         : Lavc57.107.100 png\nframe=    6 fps=0.0 q=-0.0 size=N/A time=00:00:00.06 bitrate=N/A speed=0.109x    \r[image2size=N/A time=00:00:17.06 bitrate=N/A speed=0.296x    \r[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 512 >= 512\n[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 514 >= 514\nframe=  703 fps= 12 q=-0.0 size=N/A time=00:00:17.20 bitrate=N/A speed=0.295x    \r[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 516 >= 516\n[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 517 >= 517\n[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 518 >= 518\nframe=  711 fps= 12 q=-0.0 size=N/A time=00:00:17.36 bitrate=N/A speed=0.295x    \r[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 520 >= 520\n[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 521 >= 521\n[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 522 >= 522\nframe=  717 fps= 12 q=-0.0 size=N/A time=00:00:17.46 bitrate=N/A speed=0.293x    \r[image2 @ 0x1a3e900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 524 >= 524\n"


    


    i want to stop the subprocess by ffmpeg self in cloud

    


  • Saving frames as JPG with FFMPEG (Visual Studio / C++)

    10 novembre 2022, par Diego Satizabal

    I am trying to save all frames from a mp4 video in separate JPG files, I have a code that runs and actually saves something to JPG files but files are not recognized as images and nothing is showing.

    


    Below my full code, I am using Visual Studio 2022 in Windows 11 and FFMPEG 5.1. The function that saves the images is save_frame_as_jpeg which is actually an adaption from the code provided here but changing the use of avcodec_encode_video2 for avcodec_send_frame/avcodec_receive_packet as indicated in the documentation.

    


    I am obiously doing something wrong but cannot quite find it, BTW, I know that a simple command (ffmpeg -i input.mp4 -vf fps=1 vid_%d.png) will do this but I am requiring to do it by code.

    


    Any help is appreciated, thanks in advance !

    


        // FfmpegTests.cpp : This file contains the &#x27;main&#x27; function. Program execution begins and ends there.&#xA;//&#xA;#pragma warning(disable : 4996)&#xA;extern "C"&#xA;{&#xA;    #include "libavformat/avformat.h"&#xA;    #include "libavcodec/avcodec.h"&#xA;    #include "libavfilter/avfilter.h"&#xA;    #include "libavutil/opt.h"&#xA;    #include "libavutil/avutil.h"&#xA;    #include "libavutil/error.h"&#xA;    #include "libavfilter/buffersrc.h"&#xA;    #include "libavfilter/buffersink.h"&#xA;    #include "libswscale/swscale.h"&#xA;}&#xA;&#xA;#pragma comment(lib, "avcodec.lib")&#xA;#pragma comment(lib, "avformat.lib")&#xA;#pragma comment(lib, "avfilter.lib")&#xA;#pragma comment(lib, "avutil.lib")&#xA;#pragma comment(lib, "swscale.lib")&#xA;&#xA;#include <cstdio>&#xA;#include <iostream>&#xA;#include <chrono>&#xA;#include <thread>&#xA;&#xA;&#xA;static AVFormatContext* fmt_ctx;&#xA;static AVCodecContext* dec_ctx;&#xA;AVFilterGraph* filter_graph;&#xA;AVFilterContext* buffersrc_ctx;&#xA;AVFilterContext* buffersink_ctx;&#xA;static int video_stream_index = -1;&#xA;&#xA;const char* filter_descr = "scale=78:24,transpose=cclock";&#xA;static int64_t last_pts = AV_NOPTS_VALUE;&#xA;&#xA;static int open_input_file(const char* filename)&#xA;{&#xA;    const AVCodec* dec;&#xA;    int ret;&#xA;&#xA;    if ((ret = avformat_open_input(&amp;fmt_ctx, filename, NULL, NULL)) &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");&#xA;        return ret;&#xA;    }&#xA;&#xA;    if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");&#xA;        return ret;&#xA;    }&#xA;&#xA;    /* select the video stream */&#xA;    ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &amp;dec, 0);&#xA;    if (ret &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");&#xA;        return ret;&#xA;    }&#xA;    video_stream_index = ret;&#xA;&#xA;    /* create decoding context */&#xA;    dec_ctx = avcodec_alloc_context3(dec);&#xA;    if (!dec_ctx)&#xA;        return AVERROR(ENOMEM);&#xA;    avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[video_stream_index]->codecpar);&#xA;&#xA;    /* init the video decoder */&#xA;    if ((ret = avcodec_open2(dec_ctx, dec, NULL)) &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");&#xA;        return ret;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;static int init_filters(const char* filters_descr)&#xA;{&#xA;    char args[512];&#xA;    int ret = 0;&#xA;    const AVFilter* buffersrc = avfilter_get_by_name("buffer");&#xA;    const AVFilter* buffersink = avfilter_get_by_name("buffersink");&#xA;    AVFilterInOut* outputs = avfilter_inout_alloc();&#xA;    AVFilterInOut* inputs = avfilter_inout_alloc();&#xA;    AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;&#xA;    enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };&#xA;&#xA;    filter_graph = avfilter_graph_alloc();&#xA;    if (!outputs || !inputs || !filter_graph) {&#xA;        ret = AVERROR(ENOMEM);&#xA;        goto end;&#xA;    }&#xA;&#xA;    /* buffer video source: the decoded frames from the decoder will be inserted here. */&#xA;    snprintf(args, sizeof(args),&#xA;        "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",&#xA;        dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,&#xA;        time_base.num, time_base.den,&#xA;        dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);&#xA;&#xA;    ret = avfilter_graph_create_filter(&amp;buffersrc_ctx, buffersrc, "in",&#xA;        args, NULL, filter_graph);&#xA;    if (ret &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");&#xA;        goto end;&#xA;    }&#xA;&#xA;    /* buffer video sink: to terminate the filter chain. */&#xA;    ret = avfilter_graph_create_filter(&amp;buffersink_ctx, buffersink, "out",&#xA;        NULL, NULL, filter_graph);&#xA;    if (ret &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");&#xA;        goto end;&#xA;    }&#xA;&#xA;    ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);&#xA;    if (ret &lt; 0) {&#xA;        av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");&#xA;        goto end;&#xA;    }&#xA;&#xA;    outputs->name = av_strdup("in");&#xA;    outputs->filter_ctx = buffersrc_ctx;&#xA;    outputs->pad_idx = 0;&#xA;    outputs->next = NULL;&#xA;&#xA;    inputs->name = av_strdup("out");&#xA;    inputs->filter_ctx = buffersink_ctx;&#xA;    inputs->pad_idx = 0;&#xA;    inputs->next = NULL;&#xA;&#xA;    if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,&#xA;        &amp;inputs, &amp;outputs, NULL)) &lt; 0)&#xA;        goto end;&#xA;&#xA;    if ((ret = avfilter_graph_config(filter_graph, NULL)) &lt; 0)&#xA;        goto end;&#xA;&#xA;end:&#xA;    avfilter_inout_free(&amp;inputs);&#xA;    avfilter_inout_free(&amp;outputs);&#xA;&#xA;    return ret;&#xA;}&#xA;&#xA;static void display_frame(const AVFrame* frame, AVRational time_base)&#xA;{&#xA;    int x, y;&#xA;    uint8_t* p0, * p;&#xA;    int64_t delay;&#xA;&#xA;    if (frame->pts != AV_NOPTS_VALUE) {&#xA;        if (last_pts != AV_NOPTS_VALUE) {&#xA;            /* sleep roughly the right amount of time;&#xA;             * usleep is in microseconds, just like AV_TIME_BASE. */&#xA;            AVRational timeBaseQ;&#xA;            timeBaseQ.num = 1;&#xA;            timeBaseQ.den = AV_TIME_BASE;&#xA;&#xA;            delay = av_rescale_q(frame->pts - last_pts, time_base, timeBaseQ);&#xA;            if (delay > 0 &amp;&amp; delay &lt; 1000000)&#xA;                std::this_thread::sleep_for(std::chrono::microseconds(delay));&#xA;        }&#xA;        last_pts = frame->pts;&#xA;    }&#xA;&#xA;    /* Trivial ASCII grayscale display. */&#xA;    p0 = frame->data[0];&#xA;    puts("\033c");&#xA;    for (y = 0; y &lt; frame->height; y&#x2B;&#x2B;) {&#xA;        p = p0;&#xA;        for (x = 0; x &lt; frame->width; x&#x2B;&#x2B;)&#xA;            putchar(" .-&#x2B;#"[*(p&#x2B;&#x2B;) / 52]);&#xA;        putchar(&#x27;\n&#x27;);&#xA;        p0 &#x2B;= frame->linesize[0];&#xA;    }&#xA;    fflush(stdout);&#xA;}&#xA;&#xA;int save_frame_as_jpeg(AVCodecContext* pCodecCtx, AVFrame* pFrame, int FrameNo) {&#xA;    int ret = 0;&#xA;&#xA;    const AVCodec* jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);&#xA;    if (!jpegCodec) {&#xA;        return -1;&#xA;    }&#xA;    AVCodecContext* jpegContext = avcodec_alloc_context3(jpegCodec);&#xA;    if (!jpegContext) {&#xA;        return -1;&#xA;    }&#xA;&#xA;    jpegContext->pix_fmt = pCodecCtx->pix_fmt;&#xA;    jpegContext->height = pFrame->height;&#xA;    jpegContext->width = pFrame->width;&#xA;    jpegContext->time_base = AVRational{ 1,10 };&#xA;&#xA;    ret = avcodec_open2(jpegContext, jpegCodec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;    FILE* JPEGFile;&#xA;    char JPEGFName[256];&#xA;&#xA;    AVPacket packet;&#xA;    packet.data = NULL;&#xA;    packet.size = 0;&#xA;    av_init_packet(&amp;packet);&#xA;&#xA;    int gotFrame;&#xA;&#xA;    ret = avcodec_send_frame(jpegContext, pFrame);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_packet(jpegContext, &amp;packet);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;&#xA;    sprintf(JPEGFName, "c:\\folder\\dvr-%06d.jpg", FrameNo);&#xA;    JPEGFile = fopen(JPEGFName, "wb");&#xA;    fwrite(packet.data, 1, packet.size, JPEGFile);&#xA;    fclose(JPEGFile);&#xA;&#xA;    av_packet_unref(&amp;packet);&#xA;    avcodec_close(jpegContext);&#xA;    return 0;&#xA;}&#xA;&#xA;int main(int argc, char** argv)&#xA;{&#xA;    AVFrame* frame;&#xA;    AVFrame* filt_frame;&#xA;    AVPacket* packet;&#xA;    int ret;&#xA;&#xA;    if (argc != 2) {&#xA;        fprintf(stderr, "Usage: %s file\n", argv[0]);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    filt_frame = av_frame_alloc();&#xA;    packet = av_packet_alloc();&#xA;&#xA;    if (!frame || !filt_frame || !packet) {&#xA;        fprintf(stderr, "Could not allocate frame or packet\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if ((ret = open_input_file(argv[1])) &lt; 0)&#xA;        goto end;&#xA;    if ((ret = init_filters(filter_descr)) &lt; 0)&#xA;        goto end;&#xA;&#xA;    while (true)&#xA;    {&#xA;        if ((ret = av_read_frame(fmt_ctx, packet)) &lt; 0)&#xA;            break;&#xA;&#xA;        if (packet->stream_index == video_stream_index) {&#xA;            ret = avcodec_send_packet(dec_ctx, packet);&#xA;            if (ret &lt; 0) {&#xA;                av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n");&#xA;                break;&#xA;            }&#xA;&#xA;            while (ret >= 0)&#xA;            {&#xA;                ret = avcodec_receive_frame(dec_ctx, frame);&#xA;                if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;                    break;&#xA;                }&#xA;                else if (ret &lt; 0) {&#xA;                    av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n");&#xA;                    goto end;&#xA;                }&#xA;&#xA;                frame->pts = frame->best_effort_timestamp;&#xA;&#xA;                /* push the decoded frame into the filtergraph */&#xA;                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) &lt; 0) {&#xA;                    av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");&#xA;                    break;&#xA;                }&#xA;&#xA;                /* pull filtered frames from the filtergraph */&#xA;                while (1) {&#xA;                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);&#xA;                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;                        break;&#xA;                    if (ret &lt; 0)&#xA;                        goto end;&#xA;                    display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);&#xA;                    av_frame_unref(filt_frame);&#xA;                    &#xA;                    ret = save_frame_as_jpeg(dec_ctx, frame, dec_ctx->frame_number);&#xA;                    if (ret &lt; 0)&#xA;                        goto end;&#xA;                }&#xA;                av_frame_unref(frame);&#xA;            }&#xA;        }&#xA;        av_packet_unref(packet);&#xA;    }&#xA;&#xA;end:&#xA;    avfilter_graph_free(&amp;filter_graph);&#xA;    avcodec_free_context(&amp;dec_ctx);&#xA;    avformat_close_input(&amp;fmt_ctx);&#xA;    av_frame_free(&amp;frame);&#xA;    av_frame_free(&amp;filt_frame);&#xA;    av_packet_free(&amp;packet);&#xA;&#xA;    if (ret &lt; 0 &amp;&amp; ret != AVERROR_EOF) {&#xA;        char errBuf[AV_ERROR_MAX_STRING_SIZE]{0};&#xA;        int res = av_strerror(ret, errBuf, AV_ERROR_MAX_STRING_SIZE);&#xA;        fprintf(stderr, "Error:  %s\n", errBuf);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    exit(0);&#xA;}&#xA;</thread></chrono></iostream></cstdio>

    &#xA;