Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Quote filenames with single quotes on POSIX systems #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/AudioStreamInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Params.h"
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include "File.h"
#if defined(_WIN32) && !defined(__MINGW32__)
Expand Down Expand Up @@ -60,15 +61,31 @@ class FfmpegStreamInput : public AudioStreamInput {
protected:
std::string GetCommandLine(const char* filename) {
// TODO: Windows
char message[4096] = {0};
if (_Offset_s == 0 && _Seconds == 0)
snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le - 2>%s",
filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, DEVNULL);
else
snprintf(message, NELEM(message), "ffmpeg -i \"%s\" -ac %d -ar %d -f s16le -t %d -ss %d - 2>%s",
filename, Params::AudioStreamInput::Channels, (uint) Params::AudioStreamInput::SamplingRate, _Seconds, _Offset_s, DEVNULL);

return std::string(message);
std::string filestr = std::string(filename);
#if defined(_WIN32) && !defined(__MINGW32__)
std::string quote = "\"";
#else
std::string quote = "'";
std::string::size_type next;
std::string search = "'";
std::string replace = "'\\''";
for (next = filestr.find(search); next != std::string::npos; next = filestr.find(search, next)) {
filestr.replace(next, search.length(), replace);
next += replace.length();
}
#endif
std::ostringstream message;
// ffmpeg -i '%s' -ac %d -ar %d -f s16le (-t %d -ss %d) - 2>DEVNULL
message << "ffmpeg -i " << quote << filestr << quote << " ";
message << "-ac " << Params::AudioStreamInput::Channels << " ";
message << "-ar " << (uint) Params::AudioStreamInput::SamplingRate << " ";
message << "-f s16le ";
if (_Offset_s > 0 && _Seconds > 0) {
message << "-t " << _Seconds << " ";
message << "-ss " << _Offset_s << " ";
}
message << "- 2> " << DEVNULL;
return message.str();
}
};

Expand Down