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?

Mehdi Souregi
  • 3,153
  • 5
  • 36
  • 53
  • 1
    Incidentally, the linked duplicate is someone making the exact same mistake _also_ with video download/encoding tools. – Charles Duffy Aug 25 '22 at 18:35

1 Answers1

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']
furas
  • 134,197
  • 12
  • 106
  • 148