About double quotes in QProcess and display cmd.exe when debug
-
Hi,
You should use QDir::toNativeSeparators for your file paths. Since you're on Windows, ffmpeg might not understand the forward slash notation.
-
Hi,
You should use QDir::toNativeSeparators for your file paths. Since you're on Windows, ffmpeg might not understand the forward slash notation.
-
Hi,
You should use QDir::toNativeSeparators for your file paths. Since you're on Windows, ffmpeg might not understand the forward slash notation.
-
you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use ", but likely they do not be passed correctly,
When you put your own pair of double-quotes into the second argument passed as an argument list to
QProcess::start()
, Qt code decided you wanted to pass those quotes as part of the argument and so it will have put further quotes (and backslashes) around yours. That's why it would then be wrong.When you choose the
QProcess::start()
overload which accepts a list of arguments, Qt will quote each one as necessary: your job is to pass in those arguments without quotes, and let Qt do it for you. When you choose theQProcess::start()
overload which accepts a single string of argument(s), it requires you to have done your own quoting, and it does not further-quote it. -
you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use ", but likely they do not be passed correctly,
When you put your own pair of double-quotes into the second argument passed as an argument list to
QProcess::start()
, Qt code decided you wanted to pass those quotes as part of the argument and so it will have put further quotes (and backslashes) around yours. That's why it would then be wrong.When you choose the
QProcess::start()
overload which accepts a list of arguments, Qt will quote each one as necessary: your job is to pass in those arguments without quotes, and let Qt do it for you. When you choose theQProcess::start()
overload which accepts a single string of argument(s), it requires you to have done your own quoting, and it does not further-quote it.@JonB I have read the documentation fo QProcess carefully, and also have the same question: Why I do not need to add double-quotes when I choose the
QProcess::start()
overload which accepts a list of arguments?
I try this command in Windows cmd.exe(without double-quotes in second argument), it is useless :
> ffmpeg.exe -i concat:file1.mp3|file2.mp3 -acodec copy out.mp3
and try these code(have\"
) in Qt code, it is useful:QProcess *cmd = new QProcess(this); QString cmdArgv = QString("ffmpeg.exe -i \"concat:%1\" -acodec copy %2").arg(fileList.join("|")).arg(savePath); cmd->start(cmdArgv);
Above all, the second argument has a pair of double-quotes as part of itself. However these code is useful too!!!
QProcess *cmd = new QProcess(this); QString program = "ffmpeg.exe"; QStringList argv; argv.append(QString("-i")); argv.append(QString("concat:%1").arg(compaxList.join("|"))); argv.append(QString("-acodec")); argv.append(QString("copy")); argv.append(savePath); cmd->start(program,argv);
This method makes all argument have double-quotes or have no double-quotes, but is useful.
Although my Qt program works successfully, I still can not figure out howQProcess::start()
works. I hope your can explain for me, or give me any document link I will learn it by myself.
Thanks. -
Because you have the
|
char which is a pipe and is handled by the command line interpreter. If you don't escape that string properly like you have to do on a command line you end up with something that cannot be interpreted. -
@JonB I have read the documentation fo QProcess carefully, and also have the same question: Why I do not need to add double-quotes when I choose the
QProcess::start()
overload which accepts a list of arguments?
I try this command in Windows cmd.exe(without double-quotes in second argument), it is useless :
> ffmpeg.exe -i concat:file1.mp3|file2.mp3 -acodec copy out.mp3
and try these code(have\"
) in Qt code, it is useful:QProcess *cmd = new QProcess(this); QString cmdArgv = QString("ffmpeg.exe -i \"concat:%1\" -acodec copy %2").arg(fileList.join("|")).arg(savePath); cmd->start(cmdArgv);
Above all, the second argument has a pair of double-quotes as part of itself. However these code is useful too!!!
QProcess *cmd = new QProcess(this); QString program = "ffmpeg.exe"; QStringList argv; argv.append(QString("-i")); argv.append(QString("concat:%1").arg(compaxList.join("|"))); argv.append(QString("-acodec")); argv.append(QString("copy")); argv.append(savePath); cmd->start(program,argv);
This method makes all argument have double-quotes or have no double-quotes, but is useful.
Although my Qt program works successfully, I still can not figure out howQProcess::start()
works. I hope your can explain for me, or give me any document link I will learn it by myself.
Thanks.And also, one more time: there are 2 different overloads for
QProcess::start()
:-
Originally you used
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite)
. HereQStringList &arguments
is a list of separate arguments. So Qt code knows each one is an argument, and quotes each one for you. So you must not quote the arguments yourself. -
Then you use
void QProcess::start(const QString &command, OpenMode mode = ReadWrite)
. Hereconst QString &command
is a single string, the executable followed by any arguments. So Qt code does not know where each argument is, and so does no quoting for you. So you must quote the arguments yourself as you construct the string.
-
-
And also, one more time: there are 2 different overloads for
QProcess::start()
:-
Originally you used
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite)
. HereQStringList &arguments
is a list of separate arguments. So Qt code knows each one is an argument, and quotes each one for you. So you must not quote the arguments yourself. -
Then you use
void QProcess::start(const QString &command, OpenMode mode = ReadWrite)
. Hereconst QString &command
is a single string, the executable followed by any arguments. So Qt code does not know where each argument is, and so does no quoting for you. So you must quote the arguments yourself as you construct the string.
@JonB Run this command in cmd.exe, and know little more.
> ffmpeg.exe "-i" "concat:file1.mp3|file2.mp3" "-acodec" "copy" "output.mp3"
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite)
quotes each argument, and this style of command line is effective. In the original command line, only second argument has double-quotes, it confused me.Thanks for your patient explanation!
And also thank you @SGaist .
-
-
@JonB Run this command in cmd.exe, and know little more.
> ffmpeg.exe "-i" "concat:file1.mp3|file2.mp3" "-acodec" "copy" "output.mp3"
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite)
quotes each argument, and this style of command line is effective. In the original command line, only second argument has double-quotes, it confused me.Thanks for your patient explanation!
And also thank you @SGaist .
Because you are using the "separate arguments list", Qt quotes (or may quote) each & every argument, just in case. It may look odd, but does no harm, and works.
If you do not want to allow Qt to produce too many unnecessarily-quoted args, you must use the "single string all-arguments". Then you are responsible for pre-quoting, and in your example the only one which requires quoting is the
"concat:file1.mp3|file2.mp3"
, because it contains a|
character.