Permalink
Browse files

reorder functions

  • Loading branch information...
sdpython committed Jan 2, 2018
1 parent 62ebc42 commit abfeb615ccda5a0d03da02427bdd62c7f6d2dec1
Showing with 104 additions and 92 deletions.
  1. +104 −92 src/code_beatrix/faq/faq_video.py
@@ -13,6 +13,10 @@
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
from .moviepy_context import AudioContext, VideoContext
##########
# youtube
##########
def download_youtube_video(tag, output_path=None, res='720p', mime_type="video/mp4", **kwargs):
"""
@@ -49,35 +53,122 @@ def download_youtube_video(tag, output_path=None, res='720p', mime_type="video/m
fi.download(output_path=output_path)
return fi.default_filename
########
# audio
########
def video_extract_video(video_or_file, ta=0, tb=None):
def audio_extract_audio(audio_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>`_.
Extracts a part of an audio.
Extrait une partie du son.
Uses `subclip <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=audioclip#moviepy.audio.AudioClip.AudioClip.subclip>`_.
@param video_or_file string or :epkg:`VideoClip`
@param audio_or_file string or :epkg:`AudioClip`
@param ta beginning
@param tb end
@return :epkg:`VideoClip`
"""
with VideoContext(video_or_file) as video:
return video.subclip(ta, tb)
with AudioContext(audio_or_file) as audio:
return audio.subclip(ta, tb)
def audio_extract_audio(audio_or_file, ta=0, tb=None):
def audio_save(audio_or_file, filename, verbose=False, **kwargs):
"""
Extracts a part of an audio.
Extrait une partie du son.
Uses `subclip <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=audioclip#moviepy.audio.AudioClip.AudioClip.subclip>`_.
Saves as a sound.
Enregistre un son dans un fichier.
Uses `write_audiofile <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=audioclip#moviepy.audio.AudioClip.AudioClip.write_audiofile>`_.
@param audio_or_file string or :epkg:`AudioClip`
@param verbose logging or not
@param kwargs see `write_audiofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_
"""
with AudioContext(audio_or_file) as audio:
if verbose:
audio.write_audiofile(filename, verbose=verbose, **kwargs)
else:
f = io.StringIO()
with redirect_stdout(f):
with redirect_stderr(f):
audio.write_audiofile(filename, verbose=verbose, **kwargs)
def audio_modification(new_sound, loop_duration=None, volumex=1.,
fadein=False, fadeout=False, t_start=0, t_end=None,
speed=1.):
"""
Modifies a sound.
Modifie un son.
@param loop_duration loops sound
@param volumex multiplies the sound
@param fadein decreases the volume of the first seconds
@param fadeout decreases the volume of the last seconds
@param t_start shorten the audio
@param t_end shorten the audio
@param speed speed of the sound
@return new sound
"""
with AudioContext(new_sound) as audio:
if speed:
audio = audio.fl_time(lambda t: t * speed, keep_duration=True)
wav = audio.to_soundarray(fps=audio.fps)
audio = AudioArrayClip(wav, audio.fps)
if volumex != 1.:
audio = audio.fx(afx.volumex, volumex)
if loop_duration:
audio = afx.audio_loop(audio, duration=loop_duration)
if fadein:
audio = audio.fx(afx.audio_fadein, 1.0)
if fadeout:
audio = audio.fx(afx.audio_fadeout, 1.0)
if t_start != 0 or t_end is not None:
audio = audio.subclip(t_start=t_start, t_end=t_end)
return audio
def audio_compose(audio_or_file1, audio_or_file2, t1=0, t2=None):
"""
Concatenates or superposes two sounds.
Ajoute ou superpose deux sons.
@param audio_or_file1 son 1
@param audio_or_file2 son 2
@param t1 start of the first sound
@param t2 start of the second sound (or None to add it ad
@return new sound
"""
with AudioContext(audio_or_file1) as audio1:
with AudioContext(audio_or_file2) as audio2:
add = []
if t1 != 0:
add.append(audio1.set_start(t1))
else:
add.append(audio1)
if t2 is None:
add.append(audio2.set_start(audio1.duration + t1))
else:
add.append(audio2.set_start(t2))
return CompositeAudioClip(add)
########
# vidéo
########
def video_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`
"""
with AudioContext(audio_or_file) as audio:
return audio.subclip(ta, tb)
with VideoContext(video_or_file) as video:
return video.subclip(ta, tb)
def video_save(video_or_file, filename, verbose=False, **kwargs):
@@ -111,26 +202,6 @@ def video_save(video_or_file, filename, verbose=False, **kwargs):
filename, verbose=verbose, **kwargs)
def audio_save(audio_or_file, filename, verbose=False, **kwargs):
"""
Saves as a sound.
Enregistre un son dans un fichier.
Uses `write_audiofile <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=audioclip#moviepy.audio.AudioClip.AudioClip.write_audiofile>`_.
@param audio_or_file string or :epkg:`AudioClip`
@param verbose logging or not
@param kwargs see `write_audiofile <https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=videofileclip#moviepy.video.io.VideoFileClip.VideoFileClip.write_videofile>`_
"""
with AudioContext(audio_or_file) as audio:
if verbose:
audio.write_audiofile(filename, verbose=verbose, **kwargs)
else:
f = io.StringIO()
with redirect_stdout(f):
with redirect_stderr(f):
audio.write_audiofile(filename, verbose=verbose, **kwargs)
def video_enumerate_frames(video_or_file, folder=None, fps=10, pattern='images_%04d.jpg', **kwargs):
"""
Enumerates frames from a video.
@@ -161,40 +232,6 @@ def video_enumerate_frames(video_or_file, folder=None, fps=10, pattern='images_%
yield name
def audio_modification(new_sound, loop_duration=None, volumex=1.,
fadein=False, fadeout=False, t_start=0, t_end=None,
speed=1.):
"""
Modifies a sound.
Modifie un son.
@param loop_duration loops sound
@param volumex multiplies the sound
@param fadein decreases the volume of the first seconds
@param fadeout decreases the volume of the last seconds
@param t_start shorten the audio
@param t_end shorten the audio
@param speed speed of the sound
@return new sound
"""
with AudioContext(new_sound) as audio:
if speed:
audio = audio.fl_time(lambda t: t * speed, keep_duration=True)
wav = audio.to_soundarray(fps=audio.fps)
audio = AudioArrayClip(wav, audio.fps)
if volumex != 1.:
audio = audio.fx(afx.volumex, volumex)
if loop_duration:
audio = afx.audio_loop(audio, duration=loop_duration)
if fadein:
audio = audio.fx(afx.audio_fadein, 1.0)
if fadeout:
audio = audio.fx(afx.audio_fadeout, 1.0)
if t_start != 0 or t_end is not None:
audio = audio.subclip(t_start=t_start, t_end=t_end)
return audio
def video_replace_sound(video_or_file, new_sound, **kwargs):
"""
Replaces the sound of a video.
@@ -230,31 +267,6 @@ def video_extract_audio(video_or_file):
return video.audio
def audio_compose(audio_or_file1, audio_or_file2, t1=0, t2=None):
"""
Concatenates or superposes two sounds.
Ajoute ou superpose deux sons.
@param audio_or_file1 son 1
@param audio_or_file2 son 2
@param t1 start of the first sound
@param t2 start of the second sound (or None to add it ad
@return new sound
"""
with AudioContext(audio_or_file1) as audio1:
with AudioContext(audio_or_file2) as audio2:
add = []
if t1 != 0:
add.append(audio1.set_start(t1))
else:
add.append(audio1)
if t2 is None:
add.append(audio2.set_start(audio1.duration + t1))
else:
add.append(audio2.set_start(t2))
return CompositeAudioClip(add)
def video_compose(video_or_file1, video_or_file2, t1=0, t2=None, **kwargs):
"""
Concatenates or superposes two videos.

0 comments on commit abfeb61

Please sign in to comment.