Skip to content

Commit

Permalink
Fix ffmpeg protocols and properly mangle their cache path
Browse files Browse the repository at this point in the history
  • Loading branch information
myrsloik committed Apr 12, 2024
1 parent 29e67a2 commit cfbb5d2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Note that the *BSSource* function by default will silently ignore errors when op

## Argument explanation

*source*: The source filename. Note that image sequences also can be opened by using %d or %03d for zero padded numbers. Sequences may start at any number between 0 and 4 unless otherwise specified with *start_number*.
*source*: The source filename. Note that image sequences also can be opened by using %d or %03d for zero padded numbers. Sequences may start at any number between 0 and 4 unless otherwise specified with *start_number*. It's also possible to pass urls and other ffmpeg protocols like concat.

*track*: Either a positive number starting from 0 specifying the absolute track number or a negative number to select the nth audio or video track. Throws an error on wrong type or no matching track.

Expand Down
7 changes: 6 additions & 1 deletion src/audiosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,12 @@ BestAudioFrame *BestAudioSource::Cache::GetFrame(int64_t N) {
}

BestAudioSource::BestAudioSource(const std::filesystem::path &SourceFile, int Track, int AjustDelay, bool VariableFormat, int Threads, int CacheMode, const std::filesystem::path &CachePath, const std::map<std::string, std::string> *LAVFOpts, double DrcScale, const ProgressFunction &Progress)
: Source(std::filesystem::absolute(SourceFile)), AudioTrack(Track), VariableFormat(VariableFormat), DrcScale(DrcScale), Threads(Threads) {
: Source(SourceFile), AudioTrack(Track), VariableFormat(VariableFormat), DrcScale(DrcScale), Threads(Threads) {
// Only make file path absolute if it exists to pass through special protocol paths
std::error_code ec;
if (std::filesystem::exists(SourceFile, ec))
Source = std::filesystem::absolute(SourceFile);

if (LAVFOpts)
LAVFOptions = *LAVFOpts;

Expand Down
10 changes: 9 additions & 1 deletion src/bsshared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ static std::filesystem::path GetDefaultCachePath() {

static std::filesystem::path MangleCachePath(const std::filesystem::path &CacheBasePath, const std::filesystem::path &Source) {
std::filesystem::path CachePath = std::filesystem::absolute(CacheBasePath);
CachePath /= Source.relative_path();
// Since it's possible to pass in urls, ffmpeg protocols and other things with characters not allowed on disk we now have to remove them from the path
std::string Tmp = Source.relative_path().u8string();
for (auto &iter : Tmp) {
if (iter == '?' || iter == '*' || iter == '<' || iter == '>' || iter == '|' || iter == '"')
iter = '_';
else if (iter == ':')
iter = '/';
}
CachePath /= std::filesystem::u8path(Tmp);
return CachePath.make_preferred();
}

Expand Down
7 changes: 6 additions & 1 deletion src/videosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,12 @@ BestVideoFrame *BestVideoSource::Cache::GetFrame(int64_t N) {
}

BestVideoSource::BestVideoSource(const std::filesystem::path &SourceFile, const std::string &HWDeviceName, int ExtraHWFrames, int Track, bool VariableFormat, int Threads, int CacheMode, const std::filesystem::path &CachePath, const std::map<std::string, std::string> *LAVFOpts, const ProgressFunction &Progress)
: Source(std::filesystem::absolute(SourceFile)), HWDevice(HWDeviceName), ExtraHWFrames(ExtraHWFrames), VideoTrack(Track), VariableFormat(VariableFormat), Threads(Threads) {
: Source(SourceFile), HWDevice(HWDeviceName), ExtraHWFrames(ExtraHWFrames), VideoTrack(Track), VariableFormat(VariableFormat), Threads(Threads) {
// Only make file path absolute if it exists to pass through special protocol paths
std::error_code ec;
if (std::filesystem::exists(SourceFile, ec))
Source = std::filesystem::absolute(SourceFile);

if (LAVFOpts)
LAVFOptions = *LAVFOpts;

Expand Down

0 comments on commit cfbb5d2

Please sign in to comment.