Skip to content

Commit e52d864

Browse files
authored
Better error message if metadata is missing in approximate mode (#540)
1 parent b7271fa commit e52d864

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,9 @@ int64_t VideoDecoder::secondsToIndexLowerBound(double seconds) {
15361536
case SeekMode::approximate: {
15371537
auto& streamMetadata =
15381538
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1539+
TORCH_CHECK(
1540+
streamMetadata.averageFps.has_value(),
1541+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
15391542
return std::floor(seconds * streamMetadata.averageFps.value());
15401543
}
15411544
default:
@@ -1560,6 +1563,9 @@ int64_t VideoDecoder::secondsToIndexUpperBound(double seconds) {
15601563
case SeekMode::approximate: {
15611564
auto& streamMetadata =
15621565
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1566+
TORCH_CHECK(
1567+
streamMetadata.averageFps.has_value(),
1568+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
15631569
return std::ceil(seconds * streamMetadata.averageFps.value());
15641570
}
15651571
default:
@@ -1575,6 +1581,9 @@ int64_t VideoDecoder::getPts(int64_t frameIndex) {
15751581
case SeekMode::approximate: {
15761582
auto& streamMetadata =
15771583
containerMetadata_.allStreamMetadata[activeStreamIndex_];
1584+
TORCH_CHECK(
1585+
streamMetadata.averageFps.has_value(),
1586+
"Cannot use approximate mode since we couldn't find the average fps from the metadata.");
15781587
return secondsToClosestPts(
15791588
frameIndex / streamMetadata.averageFps.value(), streamInfo.timeBase);
15801589
}
@@ -1591,8 +1600,12 @@ int64_t VideoDecoder::getNumFrames(const StreamMetadata& streamMetadata) {
15911600
switch (seekMode_) {
15921601
case SeekMode::exact:
15931602
return streamMetadata.numFramesFromScan.value();
1594-
case SeekMode::approximate:
1603+
case SeekMode::approximate: {
1604+
TORCH_CHECK(
1605+
streamMetadata.numFrames.has_value(),
1606+
"Cannot use approximate mode since we couldn't find the number of frames from the metadata.");
15951607
return streamMetadata.numFrames.value();
1608+
}
15961609
default:
15971610
throw std::runtime_error("Unknown SeekMode");
15981611
}
@@ -1613,8 +1626,12 @@ double VideoDecoder::getMaxSeconds(const StreamMetadata& streamMetadata) {
16131626
switch (seekMode_) {
16141627
case SeekMode::exact:
16151628
return streamMetadata.maxPtsSecondsFromScan.value();
1616-
case SeekMode::approximate:
1629+
case SeekMode::approximate: {
1630+
TORCH_CHECK(
1631+
streamMetadata.durationSeconds.has_value(),
1632+
"Cannot use approximate mode since we couldn't find the duration from the metadata.");
16171633
return streamMetadata.durationSeconds.value();
1634+
}
16181635
default:
16191636
throw std::runtime_error("Unknown SeekMode");
16201637
}

0 commit comments

Comments
 (0)