Unsolved 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.
-
In his particular case, OP started by showing his command worked with forward slashes outside of Qt. It would be better to use
QDir::toNativeSeparators
, but just so he understands why his existing code will work. -
@JonB good point, I misread that line.
-
@SGaist Thanks for your reminder. In fact, I get the path through
QFileDialog::getOpenFileName
andgetSaveFileName
, so nothing about slash or backslash. :D -
@JonB Thanks very much! It's useful!! I don't know if the argument is string (QString), it will send double-quotes automatically. Live and learn.
-
@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. -
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 .
-
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.