0

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?

1
  • 1
    Incidentally, the linked duplicate is someone making the exact same mistake also with video download/encoding tools. Commented Aug 25, 2022 at 18:35

1 Answer 1

3

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']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.