Permalink
Browse files

clean videos when it can be done to reduce the number of open processes

  • Loading branch information...
sdpython committed Jan 19, 2018
1 parent 0739052 commit 373ea04b86a654aab222f188605ded910c0cceee
@@ -59,7 +59,7 @@ def test_extract_frames_img(self):
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_video_extract_frames_img")
vid = os.path.join(temp, '..', 'data', 'videxa.mp4')
fns = list(video_enumerate_frames(vid, folder=temp))
fns = list(video_enumerate_frames(vid, folder=temp, clean=True))
self.assertEqual(len(fns), 78)
subprocess._cleanup()
@@ -2,7 +2,7 @@
"""
@brief test log(time=1000s)
Duraction is around a few seconds but the test needs to be run
Duration is around a few seconds but the test needs to be run
at the end of the series of unitests as it interferes
with the notebook unittesting (it uses Popen too).
"""
@@ -57,7 +57,7 @@ def test_extract_frames_check_open(self):
self._testMethodName,
OutputPrint=__name__ == "__main__")
vid = os.path.join(os.path.dirname(__file__), 'data', 'videxa.mp4')
fra = list(video_enumerate_frames(vid))
fra = list(video_enumerate_frames(vid, clean=True))
self.assertEqual(len(fra), 78)
self.assertEqual(fra[0].shape, (720, 404, 3))
@@ -46,7 +46,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_text, video_position, video_compose, video_image
from src.code_beatrix.art.video import video_save, video_text, video_position, video_compose, video_image, clean_video
class TestVideoText(ExtTestCase):
@@ -61,11 +61,13 @@ def test_video_text(self):
exp = os.path.join(temp, "courte.mp4")
video_save(vid, exp, fps=20, duration=3)
self.assertExists(exp)
clean_video(vid)
exp = os.path.join(temp, "courtefct.mp4")
vid = video_position(img, lambda t: (t * 0.1, t * 0.2), relative=True)
video_save(vid, exp, fps=20, duration=4)
self.assertExists(exp)
clean_video(vid)
def test_video_text_image(self):
fLOG(
@@ -90,6 +92,8 @@ def test_video_text_image(self):
video_save(comb, exp2, fps=20, duration=5)
self.assertExists(exp1)
self.assertExists(exp2)
clean_video(text)
clean_video(comb)
if __name__ == "__main__":
@@ -46,7 +46,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_frame
from src.code_beatrix.art.video import video_save, video_frame, clean_video
class TestVideoImage(ExtTestCase):
@@ -71,6 +71,7 @@ def make_frame(t):
exp = os.path.join(temp, "courte.gif")
video_save(vid, exp, fps=20, duration=2)
self.assertExists(exp)
clean_video(vid)
def test_video_frame_sequence(self):
fLOG(
@@ -83,6 +84,7 @@ def test_video_frame_sequence(self):
exp = os.path.join(temp, "courte.gif")
video_save(vid, exp)
self.assertExists(exp)
clean_video(vid)
if __name__ == "__main__":
@@ -46,7 +46,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_image, video_position, video_resize
from src.code_beatrix.art.video import video_save, video_image, video_position, video_resize, clean_video
class TestVideoImage(ExtTestCase):
@@ -62,21 +62,25 @@ def test_video_image_position(self):
exp = os.path.join(temp, "courte.mp4")
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
exp = os.path.join(temp, "courte05.mp4")
vid = video_position(img, (0.5, 0.5), relative=True)
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
exp = os.path.join(temp, "courtecb.mp4")
vid = video_position(img, ('center', 'bottom'), relative=True)
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
exp = os.path.join(temp, "courtefct.mp4")
vid = video_position(img, lambda t: (t * 0.1, t * 0.2), relative=True)
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
def test_video_image_resize(self):
fLOG(
@@ -89,16 +93,19 @@ def test_video_image_resize(self):
exp = os.path.join(temp, "courte.mp4")
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
exp = os.path.join(temp, "courtecb.mp4")
vid = video_resize(img, 0.5)
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
exp = os.path.join(temp, "courtefct.mp4")
vid = video_resize(img, lambda t: max(0.1, 1 - 0.1 * t))
video_save(vid, exp, fps=20)
self.assertExists(exp)
clean_video(vid)
if __name__ == "__main__":
@@ -46,7 +46,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_compose
from src.code_beatrix.art.video import video_save, video_compose, clean_video
class TestVideoPlacement(ExtTestCase):
@@ -73,6 +73,7 @@ def test_modify_placement(self):
exp = os.path.join(temp, "br.mp4")
video_save(vid2, exp)
self.assertExists(exp)
clean_video(vid2)
if __name__ == "__main__":
@@ -46,7 +46,8 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_modification, video_replace_audio, video_extract_audio, video_concatenate, audio_concatenate
from src.code_beatrix.art.video import video_save, video_modification, video_replace_audio, video_extract_audio
from src.code_beatrix.art.video import video_concatenate, audio_concatenate, clean_video
class TestVideoAudioBug(ExtTestCase):
@@ -67,6 +68,7 @@ def test_video_audio_bug(self):
exp = os.path.join(temp, "courte2x.mp4")
video_save(vid4, exp)
self.assertExists(exp)
clean_video(vid4)
if __name__ == "__main__":
@@ -46,7 +46,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_extract_video, video_compose, video_concatenate
from src.code_beatrix.art.video import video_save, video_extract_video, video_compose, video_concatenate, clean_video
class TestVideo(ExtTestCase):
@@ -65,6 +65,7 @@ def test_extract_avideo(self):
exp = os.path.join(temp, "courte.gif")
video_save(vid2, exp)
self.assertExists(exp)
clean_video(vid2)
def test_compose_avideo(self):
fLOG(
@@ -82,6 +83,7 @@ def test_compose_avideo(self):
vid3 = video_concatenate([vid, vid])
video_save(vid3, exp)
self.assertExists(exp)
clean_video(vid3)
if __name__ == "__main__":
@@ -46,7 +46,7 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.art.video import video_save, video_modification
from src.code_beatrix.art.video import video_save, video_modification, clean_video
class TestVideoModif(ExtTestCase):
@@ -62,6 +62,7 @@ def test_modify_avideo(self):
exp = os.path.join(temp, "courte2x.mp4")
video_save(vid2, exp)
self.assertExists(exp)
clean_video(vid2)
if __name__ == "__main__":
@@ -52,7 +52,7 @@
class TestVideoDLFace(ExtTestCase):
def test_modify_avideo_blur(self):
def _test_modify_avideo_blur(self):
fLOG(
__file__,
self._testMethodName,
@@ -65,6 +65,7 @@ def test_modify_avideo_blur(self):
exp = os.path.join(temp, "face.mp4")
video_save(vid2, exp)
self.assertExists(exp)
vid2.cbclean()
def test_modify_avideo(self):
fLOG(
@@ -83,6 +84,7 @@ def test_modify_avideo(self):
im.save(os.path.join(temp, "img2.png"))
video_save(vid2, exp, verbose=__name__ == "__main__")
self.assertExists(exp)
# vid2.cbclean()
if __name__ == "__main__":
@@ -65,6 +65,7 @@ def test_modify_avideo(self):
exp = os.path.join(temp, "people.mp4")
video_save(vid2, exp)
self.assertExists(exp)
vid2.cbclean()
if __name__ == "__main__":
View
@@ -20,7 +20,7 @@ pydocstyle
pyensae
pymmails
pymyinstall
pyquickhelper>=1.6.2365
pyquickhelper>=1.6.2369
pyshp
pytube>=8.0.0
qgrid
View
@@ -36,7 +36,6 @@
packages = find_packages('src', exclude='src')
package_dir = {k: "src/" + k.replace(".", "/") for k in packages}
package_data = {project_var_name + ".algorithm.data": ["*.txt"],
project_var_name + ".art.data": ["*.xml"],
project_var_name + ".scratchs.example_echiquier": ["*.sb2"],
project_var_name + ".scratchs.example_tri": ["*.sb2"],
project_var_name + ".scratchs.example_pyramide": ["*.sb2"],
@@ -21,7 +21,10 @@ def __init__(self, video_or_file):
"""
@param video_or_file string or :epkg:`VideoClip`
"""
self.video_or_file = video_or_file
if isinstance(video_or_file, VideoContext):
self.video_or_file = video_or_file.video
else:
self.video_or_file = video_or_file
def __enter__(self):
"""
@@ -35,6 +38,8 @@ def __enter__(self):
elif isinstance(self.video_or_file, VideoClip):
video = self.video_or_file
self.close = False
elif isinstance(self.video_or_file, VideoContext):
raise TypeError("Video cannot be a VideoContext")
else:
raise TypeError(
'Unable to use type {0}'.format(type(self.video_or_file)))
@@ -60,7 +65,7 @@ def __getattr__(self, fct):
"""
if not hasattr(self.video, fct):
raise AttributeError(
"Unable to find function '{0}' in {1}".format(fct, type(self.video)))
"Unable to find method '{0}' in {1}".format(fct, type(self.video)))
return getattr(self.video, fct)
@@ -75,7 +80,10 @@ def __init__(self, audio_or_file):
"""
@param audio_or_file string or :epkg:`AudioClip`
"""
self.audio_or_file = audio_or_file
if isinstance(audio_or_file, AudioContext):
self.audio_or_file = audio_or_file.audio
else:
self.audio_or_file = audio_or_file
def __enter__(self):
"""
@@ -89,6 +97,8 @@ def __enter__(self):
elif isinstance(self.audio_or_file, AudioClip):
audio = self.audio_or_file
self.close = False
elif isinstance(self.audio_or_file, AudioContext):
raise TypeError("Audio cannot be a VideoContext")
else:
raise TypeError(
'Unable to use type {0}'.format(type(self.audio_or_file)))
@@ -112,5 +122,33 @@ def __getattr__(self, fct):
"""
if not hasattr(self.audio, fct):
raise AttributeError(
"Unable to find function '{0}' in {1}".format(fct, type(self.audio)))
"Unable to find method '{0}' in {1}".format(fct, type(self.audio)))
return getattr(self.audio, fct)
def get_wrapped(obj):
"""
Retrives the video or the audio wrapped or not wrapped into obj.
@param obj @see cl WrappedObject, @see cl AudioContext, @see cl VideoContext
@return wrapped object
"""
if isinstance(obj, VideoContext):
return obj.video
elif isinstance(obj, AudioContext):
return obj.audio
else:
return obj
def clean_video(video):
"""
Cleans residual open streams.
It is related to the following issues:
* `The handle is invalid - Windows Only <https://github.com/Zulko/moviepy/issues/697>`_
"""
if hasattr(video, 'reader'):
video.reader.close()
if hasattr(video.audio, 'reader'):
video.audio.reader.close_proc()
Oops, something went wrong.

0 comments on commit 373ea04

Please sign in to comment.