Python 播放 ffpyplayer 后删除文件
Python remove file after playing ffpyplayer
在我的项目中,我喜欢用 cv2 播放视频,用 ffpyplayer 播放声音。
除了最后一行外,下面显示的代码有效:
os.remove("testinn.mp4")
报错:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'testinn.mp4'.
我可以断定文件没有正确关闭,所以无法删除。问题不在 cv2(如果删除 ffpyplayer 部分,它确实会删除)。但是在 ffpyplayer.
如何关闭此文件以便在播放后将其删除?
import os
import cv2
import numpy as np
from ffpyplayer.player import MediaPlayer
video_name = "testinn.mp4"
window_name = "window"
interframe_wait_ms = 30
cap = cv2.VideoCapture(video_name)
player = MediaPlayer(video_name)
if not cap.isOpened():
print("Error: Could not open video.")
exit()
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
audio_frame, val = player.get_frame()
while (True):
ret, frame = cap.read()
if not ret:
print("Reached end of video, exiting.")
break
cv2.imshow(window_name, frame)
if cv2.waitKey(interframe_wait_ms) & 0x7F == ord('~'):
print("Exit requested.")
break
cap.release()
cv2.destroyAllWindows()
os.remove("testinn.mp4")
您忘记关闭 MediaPlayer
。
根据document:
close_player(self)
Closes the player and all resources.
我使用 pathlib.Path.unlink()
删除文件,但应该与 os.remove
相同。查看 pathlib.Path
,因为它提供了方便而强大的高级接口。
>>> from ffpyplayer.player import MediaPlayer
>>> from pathlib import Path
>>> vid = Path("x:/cyan uwu.mp4")
>>> player = MediaPlayer(vid.as_posix())
# Can't delete while MediaPlayer is open
>>> vid.unlink()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "...\Python39\lib\pathlib.py", line 1343, in unlink
self._accessor.unlink(self)
PermissionError: [WinError 32] "<error message in your language>" : 'x:\cyan uwu.mp4'
# After closing you're free to remove
>>> player.close_player()
>>> vid.unlink()
>>>
在我的项目中,我喜欢用 cv2 播放视频,用 ffpyplayer 播放声音。
除了最后一行外,下面显示的代码有效:
os.remove("testinn.mp4")
报错:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'testinn.mp4'.
我可以断定文件没有正确关闭,所以无法删除。问题不在 cv2(如果删除 ffpyplayer 部分,它确实会删除)。但是在 ffpyplayer.
如何关闭此文件以便在播放后将其删除?
import os
import cv2
import numpy as np
from ffpyplayer.player import MediaPlayer
video_name = "testinn.mp4"
window_name = "window"
interframe_wait_ms = 30
cap = cv2.VideoCapture(video_name)
player = MediaPlayer(video_name)
if not cap.isOpened():
print("Error: Could not open video.")
exit()
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
audio_frame, val = player.get_frame()
while (True):
ret, frame = cap.read()
if not ret:
print("Reached end of video, exiting.")
break
cv2.imshow(window_name, frame)
if cv2.waitKey(interframe_wait_ms) & 0x7F == ord('~'):
print("Exit requested.")
break
cap.release()
cv2.destroyAllWindows()
os.remove("testinn.mp4")
您忘记关闭 MediaPlayer
。
根据document:
close_player(self)
Closes the player and all resources.
我使用 pathlib.Path.unlink()
删除文件,但应该与 os.remove
相同。查看 pathlib.Path
,因为它提供了方便而强大的高级接口。
>>> from ffpyplayer.player import MediaPlayer
>>> from pathlib import Path
>>> vid = Path("x:/cyan uwu.mp4")
>>> player = MediaPlayer(vid.as_posix())
# Can't delete while MediaPlayer is open
>>> vid.unlink()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "...\Python39\lib\pathlib.py", line 1343, in unlink
self._accessor.unlink(self)
PermissionError: [WinError 32] "<error message in your language>" : 'x:\cyan uwu.mp4'
# After closing you're free to remove
>>> player.close_player()
>>> vid.unlink()
>>>