1212#include < iostream>
1313#include < limits>
1414#include < numeric>
15+ #include < sstream>
1516#include < string_view>
1617#include " Demuxer.h"
1718#include " Metadata.h"
@@ -24,6 +25,29 @@ extern "C" {
2425
2526namespace facebook ::torchcodec {
2627
28+ namespace {
29+
30+ // FFmpeg reports "this seek cannot be performed" as a bare -1, i.e. EPERM,
31+ // which renders as the very misleading "Operation not permitted". It covers
32+ // both a target that the demuxer can't reach and a demuxer with no seeking
33+ // support whatsoever.
34+ std::string get_seek_error_message (
35+ const AVFormatContext* format_context,
36+ int64_t desired_pts,
37+ int status) {
38+ std::stringstream ss;
39+ ss << " Could not seek file to pts=" << desired_pts << " : "
40+ << get_ffmpeg_error_string_from_error_code (status) << " ." ;
41+ if (status == AVERROR (EPERM )) {
42+ ss << " This is either because that timestamp is out of range, or because"
43+ << " the '" << format_context->iformat ->name << " ' format does not"
44+ << " support seeking." ;
45+ }
46+ return ss.str ();
47+ }
48+
49+ } // namespace
50+
2751// --------------------------------------------------------------------------
2852// CONSTRUCTORS, INITIALIZATION, DESTRUCTORS
2953// --------------------------------------------------------------------------
@@ -350,9 +374,7 @@ void SingleStreamDecoder::scan_file_and_update_metadata_and_index() {
350374 // Reset the seek-cursor back to the beginning.
351375 int status = avformat_seek_file (format_context_.get (), 0 , INT64_MIN , 0 , 0 , 0 );
352376 STD_TORCH_CHECK (
353- status >= 0 ,
354- " Could not seek file to pts=0: " ,
355- get_ffmpeg_error_string_from_error_code (status));
377+ status >= 0 , get_seek_error_message (format_context_.get (), 0 , status));
356378
357379 // Sort all frames by their pts.
358380 sort_all_frames ();
@@ -1511,10 +1533,7 @@ bool SingleStreamDecoder::maybe_seek_to_before_desired_pts() {
15111533 0 );
15121534 STD_TORCH_CHECK (
15131535 status >= 0 ,
1514- " Could not seek file to pts=" ,
1515- std::to_string (desired_pts),
1516- " : " ,
1517- get_ffmpeg_error_string_from_error_code (status));
1536+ get_seek_error_message (format_context_.get (), desired_pts, status));
15181537
15191538 decode_stats_.num_flushes ++;
15201539 device_interface_->flush ();
0 commit comments