Repository: Project-MONAI/MONAI

Issue:
PermissionError in `TestVideoFileDataset`
```
======================================================================
ERROR: setUpClass (tests.test_video_datasets.TestVideoFileDataset)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 616, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\tmpr6xf1_c9\\test.avi'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 802, in onerror
    _os.unlink(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\tmpr6xf1_c9\\test.avi'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\a\MONAI\MONAI\tests\test_video_datasets.py", line 103, in setUpClass
    codecs = VideoFileDataset.get_available_codecs()
  File "D:\a\MONAI\MONAI\monai\data\video_dataset.py", line 183, in get_available_codecs
    codecs[codec] = ext
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 827, in __exit__
    self.cleanup()
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 831, in cleanup
    self._rmtree(self.name)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 813, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 618, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 805, in onerror
    cls._rmtree(path)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\tempfile.py", line 813, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 599, in _rmtree_unsafe
    onerror(os.scandir, path, sys.exc_info())
  File "C:\hostedtoolcache\windows\Python\3.8.10\x64\lib\shutil.py", line 596, in _rmtree_unsafe
    with os.scandir(path) as scandir_it:
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\tmpr6xf1_c9\\test.avi'

----------------------------------------------------------------------
Ran 11260 tests in 1712.517s

FAILED (errors=1, skipped=1201)
OpenCV: FFMPEG: tag 0x3234504d/'MP42' is not supported with codec id 15 and format 'mp4 / MP4 (MPEG-4 Part 14)'
[mp4 @ 0000015e03f55300] Could not find tag for codec msmpeg4v2 in stream #0, codec not currently supported in container
OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 7 and format 'mjpeg / raw MJPEG video'
```


Discussion:

Resolving patch:
diff --git a/monai/data/video_dataset.py b/monai/data/video_dataset.py
--- a/monai/data/video_dataset.py
+++ b/monai/data/video_dataset.py
@@ -173,15 +173,15 @@ def get_available_codecs() -> dict[str, str]:
         all_codecs = {"mp4v": ".mp4", "X264": ".avi", "H264": ".mp4", "MP42": ".mp4", "MJPG": ".mjpeg", "DIVX": ".avi"}
         codecs = {}
         with SuppressStderr():
-            writer = cv2.VideoWriter()
             with tempfile.TemporaryDirectory() as tmp_dir:
                 for codec, ext in all_codecs.items():
+                    writer = cv2.VideoWriter()
                     fname = os.path.join(tmp_dir, f"test{ext}")
                     fourcc = cv2.VideoWriter_fourcc(*codec)  # type: ignore[attr-defined]
                     noviderr = writer.open(fname, fourcc, 1, (10, 10))
                     if noviderr:
                         codecs[codec] = ext
-            writer.release()
+                    writer.release()
         return codecs
 
     def get_num_frames(self) -> int:


Test patch: