pythonsubprocessyt-dlp

"Invalid argument" While executing a command from python using subprocess


I want to execute a command from python. This is the original command:

yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00" --no-check-certificate https://youtu.be/YXfnjrbmKiQ

So I am doing this:

import subprocess

result = subprocess.call(['yt-dlp','-f','bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best','--downloader','ffmpeg','--downloader-args','"ffmpeg_i:-ss 00:00:10.00 -to 00:00:40.00"','--no-check-certificate','https://youtu.be/YXfnjrbmKiQ'])

print(result)

But it gives me the error:

ffmpeg_i:-ss 00:00:10.00 -to 00:00:40.00: Invalid argument

How can I fix it?


Solution

  • When you run without shell=True then you don't need " " in element '"ffmpeg_i:-ss 00:00:10.00 -to 00:00:40.00"' because normally only shell need it to recognize which element keep as one string - but later shell sends this string to system without " "

    import subprocess
    
    result = subprocess.call([
        'yt-dlp', 
        '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'
        '--downloader', 'ffmpeg'
        '--downloader-args', 'ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00'
        '--no-check-certificate',
        'https://youtu.be/YXfnjrbmKiQ'
    ])
    
    print(result)
    

    OR you could run it with shell=True and then you need single string and you have to use " "

    import subprocess
    
    result = subprocess.call(
       'yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00" --no-check-certificate https://youtu.be/YXfnjrbmKiQ',
       shell=True
    )
    
    print(result)
    

    BTW:

    There is standard module shlex which can convert string with command to list of arguments.

    import shlex
    
    cmd = 'yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best --downloader ffmpeg --downloader-args "ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00" --no-check-certificate https://youtu.be/YXfnjrbmKiQ'
    
    cmd = shlex.split(cmd)
    
    print(cmd)
    

    Result shows this element without " ":

    ['yt-dlp', 
    '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', 
    '--downloader', 'ffmpeg', 
    '--downloader-args', 'ffmpeg_i:-ss 00:19:10.00 -to 00:19:40.00', 
    '--no-check-certificate', 'https://youtu.be/YXfnjrbmKiQ']