Newest 'ffmpeg' Questions - Stack Overflow

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

Les articles publiés sur le site

  • How to fix the ffmpeg python issue "Error applying option 'original_size' to filter 'ass' : Invalid argument" ?

    16 février, par Gauthier Buttez

    ENVIRONMENT:

    Python 3.10

    Windows 11

    ffmpeg-python==0.2.0

    CONTEXT:

    I am trying to add hardcoded subtitles on a video with ffmpeg and Pyton.

    PROBLEM:

    ffmpeg is not able to find the path of my ass subtitle file. So I tried different ways with different methods. I asked different AI to try to find a solution. Impossible! It is such a crazy issue that I tested the code from a "test" folder located on the root "C:" from the command line wid nows. No AI was able to fixed this issue. I tried chatGPT, deepseek, claude.ai. They proposed different solutions which were not working.

    THE CODE :

    import os
    import ffmpeg
    import subprocess
    
    def add_subtitles_with_ass(video_path, ass_path, output_path):
    
        video_path = os.path.abspath(video_path)
        ass_path = os.path.abspath(ass_path)
        output_path = os.path.abspath(output_path)
    
        print(f"🚀 Add susbtitiles with ASS : {ass_path}")
        ass_path_escaped = rf'"{ass_path}"'
    
        try:
            ffmpeg.input(video_path).output(output_path, vf=f"ass='{ass_path_escaped}'").run(overwrite_output=True)
            print(f"✅ Subtitles added with success : {output_path}")
        except Exception as e:
            print(f"❌ ERROR when adding subtitles : {e}")
    
    
    def add_subtitles_with_ass_with_subprocess(video_path, ass_path, output_path):
        video_path = os.path.abspath(video_path)
        ass_path = os.path.abspath(ass_path)
        output_path = os.path.abspath(output_path)
    
        print(f"🚀 Add subtitles with ASS : {ass_path}")
    
        try:
            command = [
                'ffmpeg',
                '-i', video_path,
                '-vf', f"ass='{ass_path}'",
                '-c:a', 'copy',
                output_path,
                '-y'  # Overwrite output file without asking
            ]
    
            subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            print(f"✅ Subtitles added with success : {output_path}")
        except subprocess.CalledProcessError as e:
            print(f"❌ ERROR when adding subtitles : {e.stderr.decode('utf-8')}")
    
    
    print(f"Test 1 -----------------------------------------------------------------------")
    ass_path="G:\Mi unidad\Python\Scripts\shorts_videos\10000views_and_higher\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path="G:\Mi unidad\Python\Scripts\shorts_videos\10000views_and_higher\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path="G:\Mi unidad\Python\Scripts\shorts_videos\10000views_and_higher\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path, ass_path, output_path)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 2 -----------------------------------------------------------------------")
    ass_path2="G:\\Mi unidad\\Python\\Scripts\\shorts_videos\\10000views_and_higher\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path2="G:\\Mi unidad\\Python\\Scripts\\shorts_videos\\10000views_and_higher\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path2="G:\\Mi unidad\\Python\\Scripts\\shorts_videos\\10000views_and_higher\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path2, ass_path2, output_path2)
    
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 3 -----------------------------------------------------------------------")
    ass_path3="G:/\Mi unidad/\Python/\Scripts/\shorts_videos/\10000views_and_higher/\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path3="G:/\Mi unidad/\Python/\Scripts/\shorts_videos/\10000views_and_higher/\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path3="G:/\Mi unidad/\Python/\Scripts/\shorts_videos/\10000views_and_higher/\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path3, ass_path3, output_path3)
    
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 4 -----------------------------------------------------------------------")
    ass_path4="G:/\\Mi unidad/\\Python/\\Scripts/\\shorts_videos/\\10000views_and_higher/\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path4="G:/\\Mi unidad/\\Python/\\Scripts/\\shorts_videos/\\10000views_and_higher/\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path4="G:/\\Mi unidad/\\Python/\\Scripts/\\shorts_videos/\\10000views_and_higher/\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path4, ass_path4, output_path4)
    
    
    
    
    print(f"Test 1 with subprocess -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path, ass_path, output_path)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 2 with subprocess -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path2, ass_path2, output_path2)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 3 with subprocess -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path3, ass_path3, output_path3)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 4 with subprocess -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path4, ass_path4, output_path4)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    #======================= IT DIDN'T WORK, SO LET'S DO WITH RELATIVE PATH ===================================
    
    
    print(f"Test 1 with relative path -----------------------------------------------------------------------")
    ass_path_relative="10000views_and_higher\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path_relative="10000views_and_higher\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path_relative="10000views_and_higher\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path_relative, ass_path_relative, output_path_relative)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 2  with relative path -----------------------------------------------------------------------")
    ass_path2_relative="10000views_and_higher\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path2_relative="10000views_and_higher\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path2_relative="10000views_and_higher\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path2_relative, ass_path2_relative, output_path2_relative)
    
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 3  with relative path -----------------------------------------------------------------------")
    ass_path3_relative="10000views_and_higher/\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path3_relative="10000views_and_higher/\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path3_relative="10000views_and_higher/\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path3_relative, ass_path3_relative, output_path3_relative)
    
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 4  with relative path -----------------------------------------------------------------------")
    ass_path4_relative="10000views_and_higher/\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.ass"
    video_path4_relative="10000views_and_higher/\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo.mp4"
    output_path4_relative="10000views_and_higher/\\Li2zY7XcOf0_Matthew_Hussey_flip_h_BW_with_logo_EN.mp4"
    add_subtitles_with_ass(video_path4_relative, ass_path4_relative, output_path4_relative)
    
    
    
    
    print(f"Test 1 with subprocess  with relative path -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path, ass_path, output_path)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 2 with subprocess  with relative path -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path2_relative, ass_path2_relative, output_path2_relative)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 3 with subprocess  with relative path -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path3_relative, ass_path3_relative, output_path3_relative)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    
    print(f"Test 4 with subprocess  with relative path -----------------------------------------------------------------------")
    add_subtitles_with_ass_with_subprocess(video_path4_relative, ass_path4_relative, output_path4_relative)
    print(50*"*")
    print(50*"*")
    print(50*"*")
    

    THE OUTPUT:

    The output was too large to post it here. I uploaded it on my github :

    https://github.com/gauthierbuttez/public/blob/master/output_ffmpeg.log

    I also zipped and uploaded all the files to make reproducing the issue on your computer easy:

    https://github.com/gauthierbuttez/public/blob/master/test.zip

    Is there any kind super Python Master in the place?

  • Split a video with ffmpeg, without reencoding, at timestamps given in a txt file

    16 février, par Basj

    Let's say we have a video input.mp4, and a file split.csv containing:

    start;end;name
    00:00:27.132;00:07:42.422;"Part A.mp4"
    00:07:48.400;00:17:17.921;"Part B.mp4"
    

    (or I could format the text file in any other format, but the timestamps must be hh:mm:ss.ddd)

    How to split the MP4 into different parts with the given start / end timestamps, and the given filename for each part?

    Is it possible directly with ffmpeg, and if not with a Python script?

  • Python ffmpeg won't accept path, why ?

    16 février, par Messaoud dhia

    everytime i launch the code and set the correct path it gives me this error, I tried including ffmpeg path, uninstalling and installing the library back but no luck. I've also tried using diffrent ways to set the path like putting it directly without saving it to a variable, this is getting me crazy please help me with a solution .


                                                         Code
    

    from pytube import *
    import ffmpeg
    
    global str
    userurl = (input("Enter a youtube video URL : "))
    q = str(input("Which quality you want ?  360p,480p,720p,1080p,4K,Flh :")).lower()
    yt = YouTube(userurl)
    print ("Title of the video : ",yt.title)
    
    
    def hd1080p():
        print("Downloading a HD 1080p video...")
        v = yt.streams.filter(mime_type="video/mp4", res="1080p", adaptive = True).first().download(filename = "HD1080P.mp4")
        print("Video downloaded")
        yt.streams.filter(mime_type="audio")
        a = yt.streams.get_audio_only()
        print("Downloading audio")
        a.download(filename = "audio.mp4")
        print("audio downloaded")
        input_video = ffmpeg.input("HD1080P.mp4")
        added_audio = ffmpeg.input("audio.mp4").audio.filter('adelay', "1500|1500")
    
        merged_audio = ffmpeg.filter([input_video.audio, added_audio], 'amix')
    
        (
            ffmpeg
            .concat(input_video, merged_audio, v=1, a=1)
            .output("mix_delayed_audio.mp4")
            .run(overwrite_output=True)
        )
        
    
    if q == "1080" or q == "1080p":
        hd1080p()
    elif q == "720" or q == "720p":
        hd720p()
    elif q == "480" or q == "480p":
        l480p()
    elif q == "360" or q == "360p":
        l360p()
    elif q ==  "4" or q == "4k":
        hd4k()
    else:
        print("invalid choice")
    

                                                      THE ERROR
    

    Traceback (most recent call last):
      File "c:\Users\messa\Desktop\upcoming project\videodownloader.py", line 65, in 
        hd1080p()
      File "c:\Users\messa\Desktop\upcoming project\videodownloader.py", line 26, in hd1080p
        ffmpeg
      File "E:\Users\messa\AppData\Local\Programs\Python\Python39\lib\site-packages\ffmpeg\_run.py", line 313, in run
        process = run_async(
      File "E:\Users\messa\AppData\Local\Programs\Python\Python39\lib\site-packages\ffmpeg\_run.py", line 284, in run_async
        return subprocess.Popen(
      File "E:\Users\messa\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 947, in __init__
        self._execute_child(args, executable, preexec_fn, close_fds,
      File "E:\Users\messa\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1416, in _execute_child
        hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
    FileNotFoundError: [WinError 2] The system cannot find the file specified
    
  • Convert 720p Mp4 to 480p with php ffmpeg

    15 février, par Parm Dhillon

    I need to covert 720p or 1080p video to 480p mp4 video , i found below code

    ffmpeg -i input -vf scale=-1:480 -vcodec mpeg4 -qscale 3 output.mp4
    

    and please help me , i am unable to give it INPUT VIDEO ex.

    $video='/path/to/mp4/video';
    exec('ffmpeg -i $video -vf scale=-1:480 -vcodec mpeg4 -qscale 3 output.mp4');
    

    why above code is not working

  • How to merge segmented webvtt subtitle files and output a single file ?

    15 février, par Dobbelina

    How to merge a segmented webvtt subtitle file and output a single file?, m3u8 looks like this example:

    #EXTM3U
    #EXT-X-VERSION:4
    #EXT-X-PLAYLIST-TYPE:VOD
    #EXT-X-MEDIA-SEQUENCE:1
    #EXT-X-INDEPENDENT-SEGMENTS
    #EXT-X-TARGETDURATION:4
    #USP-X-TIMESTAMP-MAP:MPEGTS=900000,LOCAL=1970-01-01T00:00:00Z
    #EXTINF:4, no desc
    0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-1.webvtt
    #EXTINF:4, no desc
    0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-2.webvtt
    #EXTINF:4, no desc
    0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-3.webvtt
    #EXTINF:4, no desc
    0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-4.webvtt
    #EXTINF:4, no desc
    0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-5.webvtt
    #EXTINF:4, no desc
    0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-6.webvtt
    #EXT-X-ENDLIST
    

    I noticed that each segment is not synchronized/cued against total playing time, but against the individual ts segments. If ffmpeg could be used to do this, what magic input do i need to give it?

    A single correctly cued vtt or srt file is what i want.

    I have a great appetite and don't like chunks, lol!

    Thanks for any replies you lovely people!


    With this i get a merged vtt file, but the cues are all wrong:

    ffmpeg -i "https://cmoreseusphlsvod60.akamaized.net/vod/bea44/0ghzi1b2cz5(11792107_ISMUSP).ism/0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000.m3u8" -f segment -segment_time 4 -segment_format webvtt -scodec copy out-%05d.vtt
    

    Each segment is not synchronized/cued against total playing time, but against the individual ts segments. Example output of above command:

    WEBVTT
    
    00:00.000 --> 00:03.040
    Du har aktier i ett företag
    som saknar framtid.
    
    00:00.000 --> 00:03.280
    De vill ha aktierna.
    Du känner dem inte, Olga.
    
    00:00.000 --> 00:01.720
    De som får Kastrups aktier vinner.
    

    Cues all start like this which isn't very helpfull: 00:00.000

    Some segments contains no cues, like segment 15 for example: https://cmoreseusphlsvod60.akamaized.net/vod/bea44/0ghzi1b2cz5(11792107_ISMUSP).ism/0ghzi1b2cz5(11792107_ISMUSP)-textstream_swe=2000-15.webvtt

    "A WebVTT Segment MAY contain no cues; this indicates that no subtitles are to be displayed during that period."