Permalink
Browse files

extract a video

  • Loading branch information...
sdpython committed Dec 28, 2017
1 parent 5d0de6b commit 4130b960eb67049c323fa45c66db41f76bb8e573
@@ -77,4 +77,5 @@
epkg_dictionary['pytube'] = 'https://github.com/nficano/pytube'
epkg_dictionary['SciTe'] = 'http://www.scintilla.org/SciTE.html'
epkg_dictionary['scratch'] = "https://scratch.mit.edu/"
epkg_dictionary['VideoClip'] = 'https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html'
epkg_dictionary['youtube'] = "https://www.youtube.com/"
Binary file not shown.
@@ -42,19 +42,23 @@
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.faq.faq_video import download_youtube_video
from src.code_beatrix.faq.faq_video import extract_video, save_video
class TestVideo(ExtTestCase):
def test_video_download(self):
def test_extract_video(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_video_download")
download_youtube_video('vHcfbOqYztU', output_path=temp)
exp = os.path.join(temp, "vidéo tres courte.mp4")
temp = get_temp_folder(__file__, "temp_video_extract")
vid = os.path.join(temp, '..', 'data', 'videxa.mp4')
fLOG('extract')
vid2 = extract_video(vid, '00:00:01', '00:00:04')
fLOG('save')
exp = os.path.join(temp, "courte.mp4")
save_video(vid2, exp)
self.assertExists(exp)
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
"""
@brief test log(time=1s)
"""
import sys
import os
import unittest
try:
import pyquickhelper
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper
try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, ExtTestCase
from src.code_beatrix.faq.faq_video import download_youtube_video
class TestVideoDownload(ExtTestCase):
def test_video_download(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
temp = get_temp_folder(__file__, "temp_video_download")
download_youtube_video('vHcfbOqYztU', output_path=temp)
exp = os.path.join(temp, "vidéo tres courte.mp4")
self.assertExists(exp)
if __name__ == "__main__":
unittest.main()
View
@@ -33,6 +33,7 @@ install:
- "%PYTHON%\\Scripts\\pip install pyrsslocal"
- "%PYTHON%\\Scripts\\pip install ensae_projects"
- "%PYTHON%\\Scripts\\pip install pygame"
- "%PYTHON%\\Scripts\\pip install moviepy"
- "%PYTHON%\\Scripts\\pip install pytube"
- "%PYTHON%\\Scripts\\pymy_install3 opencv_python cupy"
- "%PYTHON%\\Scripts\\pymy_install3 yaml pyproj scikit-image opencv-python"
View
@@ -10,6 +10,7 @@ folium
gdown
hachibee_sphinx_theme
jyquickhelper
moviepy
mpld3
mutagen
opencv-python
@@ -3,7 +3,12 @@
@file
@brief Quelques questions d'ordre général autour du langage Python.
"""
from contextlib import redirect_stdout, redirect_stderr
import io
import os
from pytube import YouTube
from moviepy.video.VideoClip import VideoClip
from moviepy.editor import VideoFileClip
def download_youtube_video(tag, output_path=None, res='720p', mime_type="video/mp4", **kwargs):
@@ -40,3 +45,57 @@ def download_youtube_video(tag, output_path=None, res='720p', mime_type="video/m
fi = st.first()
fi.download(output_path=output_path)
return fi.default_filename
def get_video(video_or_file):
"""
Returns a :epkg:`VideoClip`.
Retourne un objet de type :epkg:`VideoClip`.
@param video_or_file string or :epkg:`VideoClip`
@return :epkg:`VideoClip`
"""
if isinstance(video_or_file, str):
if not os.path.exists(video_or_file):
raise FileNotFoundError(video_or_file)
video = VideoFileClip(video_or_file)
elif isinstance(video_or_file, VideoClip):
video = video_or_file
else:
raise TypeError('Unable to use type {0}'.format(type(video_or_file)))
return video
def extract_video(video_or_file, ta=0, tb=None):
"""
Extracts a part of a video.
Extrait une partie de la vidéo.
Uses `subclip <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.VideoClip.VideoClip.subclip>`_.
@param video_or_file string or :epkg:`VideoClip`
@param ta beginning
@param tb end
@return :epkg:`VideoClip`
"""
video = get_video(video_or_file)
return video.subclip(ta, tb)
def save_video(video_or_file, filename, verbose=False, **kwargs):
"""
Saves as a video.
Enregistre une vidéo dans un fichier.
Uses `write_videofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_.
@param video_or_file string or :epkg:`VideoClip`
@param verbose logging or not
@param kwargs see `write_videofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_
"""
video = get_video(video_or_file)
if verbose:
video.write_videofile(filename, verbose=verbose, **kwargs)
else:
f = io.StringIO()
with redirect_stdout(f):
with redirect_stderr(f):
video.write_videofile(filename, verbose=verbose, **kwargs)

0 comments on commit 4130b96

Please sign in to comment.