Skip to content

Commit 9e9b97d

Browse files
committed
Merge branch 'main' of github.com:meta-pytorch/torchcodec into removetodos
2 parents d2dff59 + 9dd5cc6 commit 9e9b97d

4 files changed

Lines changed: 1035 additions & 11 deletions

File tree

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ norecursedirs = ["third-party-interface"]
5454
# the CI, where we definitely want the 'slow' tests to run.
5555
addopts = "-v -m 'not slow'"
5656

57+
# Tells pytest to keep around temporary paths (via tmp_path fixture) for
58+
# *failed tests* only. Otherwise, the /tmp folder can grow too large.
59+
tmp_path_retention_policy = "failed"
60+
5761
testpaths = ["test"]

src/torchcodec/_core/Encoder.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ MultiStreamEncoder::MultiStreamEncoder() {
10571057
}
10581058

10591059
void MultiStreamEncoder::open(std::string_view fileName) {
1060+
STD_TORCH_CHECK(!closed_, "Cannot open after close() was called.");
10601061
STD_TORCH_CHECK(!headerWritten_, "open() was already called.");
10611062

10621063
AVFormatContext* avFormatContext = nullptr;
@@ -1086,6 +1087,7 @@ void MultiStreamEncoder::open(std::string_view fileName) {
10861087
void MultiStreamEncoder::open(
10871088
std::string_view formatName,
10881089
std::unique_ptr<AVIOContextHolder> avioContextHolder) {
1090+
STD_TORCH_CHECK(!closed_, "Cannot open after close() was called.");
10891091
STD_TORCH_CHECK(!headerWritten_, "open() was already called.");
10901092

10911093
avioContextHolder_ = std::move(avioContextHolder);
@@ -1148,6 +1150,13 @@ int MultiStreamEncoder::addAudioStream(
11481150
STD_TORCH_CHECK(sampleRate > 0, "sample_rate must be > 0, got ", sampleRate);
11491151
STD_TORCH_CHECK(
11501152
numChannels > 0, "num_channels must be > 0, got ", numChannels);
1153+
STD_TORCH_CHECK(
1154+
numChannels <= AV_NUM_DATA_POINTERS,
1155+
"Trying to encode ",
1156+
numChannels,
1157+
" channels, but FFmpeg only supports ",
1158+
AV_NUM_DATA_POINTERS,
1159+
" channels per frame.");
11511160

11521161
AudioStream audioStream;
11531162
audioStream.inSampleRate = sampleRate;
@@ -1412,6 +1421,7 @@ void MultiStreamEncoder::openStreamsAndWriteHeader() {
14121421
void MultiStreamEncoder::addFrames(
14131422
const torch::stable::Tensor& frames,
14141423
int streamIndex) {
1424+
STD_TORCH_CHECK(!closed_, "Cannot add frames after close() was called.");
14151425
STD_TORCH_CHECK(headerWritten_, "Call open() before addFrames().");
14161426
STD_TORCH_CHECK(
14171427
streamIndex >= 0 && streamIndex < static_cast<int>(videoStreams_.size()),
@@ -1501,6 +1511,7 @@ void MultiStreamEncoder::encodeVideoFrame(
15011511
void MultiStreamEncoder::addSamples(
15021512
const torch::stable::Tensor& samples,
15031513
int streamIndex) {
1514+
STD_TORCH_CHECK(!closed_, "Cannot add samples after close() was called.");
15041515
STD_TORCH_CHECK(headerWritten_, "Call open() before addSamples().");
15051516
STD_TORCH_CHECK(
15061517
streamIndex >= 0 && streamIndex < static_cast<int>(audioStreams_.size()),

src/torchcodec/encoders/_multi_stream_encoder.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,18 @@ def add_audio(
8989

9090
# TODO MultiStreamEncoder: Maybe there should 2 separate methods, one for
9191
# file, one for file-like.
92-
def open(self, dest, *, format: str | None = None) -> None:
92+
def open(self, dest, *, format: str | None = None) -> "StreamingEncoder":
9393
if format is not None:
9494
_core.streaming_encoder_open_file_like(self._encoder_tensor, format, dest)
9595
else:
9696
_core.streaming_encoder_open_file(self._encoder_tensor, str(dest))
97+
return self
9798

9899
def close(self) -> None:
99100
_core.streaming_encoder_close(self._encoder_tensor)
101+
102+
def __enter__(self) -> "StreamingEncoder":
103+
return self
104+
105+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
106+
self.close()

0 commit comments

Comments
 (0)