Permalink
Browse files

extends video helpers

  • Loading branch information...
sdpython committed Dec 28, 2017
1 parent 0e2f176 commit d0853046529b10483b9bedf6ce5fd89d1e002f2c
View
@@ -264,3 +264,5 @@ _unittests/ut_documentation/test_notebook_algorithm.py.err
_unittests/run_unittests.py.out
_doc/sphinxdoc/source/phdoc_static/style_notebook_snippet.css
_doc/sphinxdoc/source/nbcov.png
_doc/notebooks/exemples/mpoppins.mp4
_doc/notebooks/exemples/Supercalifragilisticexpialidocious (from Mary Poppins) - Julie Andrews Dick Van Dyke.mp4
@@ -0,0 +1,246 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quelques images de Mary Poppins\n",
"\n",
"Le notebook t\u00e9l\u00e9charge une petite vid\u00e9o de Mary Poppins, extrait une dizaine de secondes, convertit la vid\u00e9o sous forme d'images, enl\u00e8ve le fond de chaque image et recompose une vid\u00e9o."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div id=\"my_id_menu_nb\">run previous cell, wait for 2 seconds</div>\n",
"<script>\n",
"function repeat_indent_string(n){\n",
" var a = \"\" ;\n",
" for ( ; n > 0 ; --n)\n",
" a += \" \";\n",
" return a;\n",
"}\n",
"var update_menu_string = function(begin, lfirst, llast, sformat, send, keep_item, begin_format, end_format) {\n",
" var anchors = document.getElementsByClassName(\"section\");\n",
" if (anchors.length == 0) {\n",
" anchors = document.getElementsByClassName(\"text_cell_render rendered_html\");\n",
" }\n",
" var i,t;\n",
" var text_menu = begin;\n",
" var text_memo = \"<pre>\\nlength:\" + anchors.length + \"\\n\";\n",
" var ind = \"\";\n",
" var memo_level = 1;\n",
" var href;\n",
" var tags = [];\n",
" var main_item = 0;\n",
" var format_open = 0;\n",
" for (i = 0; i <= llast; i++)\n",
" tags.push(\"h\" + i);\n",
"\n",
" for (i = 0; i < anchors.length; i++) {\n",
" text_memo += \"**\" + anchors[i].id + \"--\\n\";\n",
"\n",
" var child = null;\n",
" for(t = 0; t < tags.length; t++) {\n",
" var r = anchors[i].getElementsByTagName(tags[t]);\n",
" if (r.length > 0) {\n",
"child = r[0];\n",
"break;\n",
" }\n",
" }\n",
" if (child == null) {\n",
" text_memo += \"null\\n\";\n",
" continue;\n",
" }\n",
" if (anchors[i].hasAttribute(\"id\")) {\n",
" // when converted in RST\n",
" href = anchors[i].id;\n",
" text_memo += \"#1-\" + href;\n",
" // passer \u00e0 child suivant (le chercher)\n",
" }\n",
" else if (child.hasAttribute(\"id\")) {\n",
" // in a notebook\n",
" href = child.id;\n",
" text_memo += \"#2-\" + href;\n",
" }\n",
" else {\n",
" text_memo += \"#3-\" + \"*\" + \"\\n\";\n",
" continue;\n",
" }\n",
" var title = child.textContent;\n",
" var level = parseInt(child.tagName.substring(1,2));\n",
"\n",
" text_memo += \"--\" + level + \"?\" + lfirst + \"--\" + title + \"\\n\";\n",
"\n",
" if ((level < lfirst) || (level > llast)) {\n",
" continue ;\n",
" }\n",
" if (title.endsWith('\u00b6')) {\n",
" title = title.substring(0,title.length-1).replace(\"<\", \"&lt;\")\n",
" .replace(\">\", \"&gt;\").replace(\"&\", \"&amp;\");\n",
" }\n",
" if (title.length == 0) {\n",
" continue;\n",
" }\n",
"\n",
" while (level < memo_level) {\n",
" text_menu += end_format + \"</ul>\\n\";\n",
" format_open -= 1;\n",
" memo_level -= 1;\n",
" }\n",
" if (level == lfirst) {\n",
" main_item += 1;\n",
" }\n",
" if (keep_item != -1 && main_item != keep_item + 1) {\n",
" // alert(main_item + \" - \" + level + \" - \" + keep_item);\n",
" continue;\n",
" }\n",
" while (level > memo_level) {\n",
" text_menu += \"<ul>\\n\";\n",
" memo_level += 1;\n",
" }\n",
" text_menu += repeat_indent_string(level-2);\n",
" text_menu += begin_format + sformat.replace(\"__HREF__\", href).replace(\"__TITLE__\", title);\n",
" format_open += 1;\n",
" }\n",
" while (1 < memo_level) {\n",
" text_menu += end_format + \"</ul>\\n\";\n",
" memo_level -= 1;\n",
" format_open -= 1;\n",
" }\n",
" text_menu += send;\n",
" //text_menu += \"\\n\" + text_memo;\n",
"\n",
" while (format_open > 0) {\n",
" text_menu += end_format;\n",
" format_open -= 1;\n",
" }\n",
" return text_menu;\n",
"};\n",
"var update_menu = function() {\n",
" var sbegin = \"\";\n",
" var sformat = '<a href=\"#__HREF__\">__TITLE__</a>';\n",
" var send = \"\";\n",
" var begin_format = '<li>';\n",
" var end_format = '</li>';\n",
" var keep_item = -1;\n",
" var text_menu = update_menu_string(sbegin, 2, 4, sformat, send, keep_item,\n",
" begin_format, end_format);\n",
" var menu = document.getElementById(\"my_id_menu_nb\");\n",
" menu.innerHTML=text_menu;\n",
"};\n",
"window.setTimeout(update_menu,2000);\n",
" </script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from jyquickhelper import add_notebook_menu\n",
"add_notebook_menu()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Le module utilise [moviepy](https://zulko.github.io/moviepy/) et [pytube](https://github.com/nficano/pytube)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## T\u00e9l\u00e9charger une vid\u00e9o et extraire une partie\n",
"\n",
"On choisit une vid\u00e9o de Mary Poppins : [Supercalifragilisticexpialidocious](https://www.youtube.com/watch?v=tRFHXMQP-QU)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Supercalifragilisticexpialidocious (from Mary Poppins) - Julie Andrews Dick Van Dyke.mp4'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from code_beatrix.faq.faq_video import download_youtube_video\n",
"name = download_youtube_video(tag='tRFHXMQP-QU')\n",
"name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"On extraire une dizaine de secondes."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from code_beatrix.faq.faq_video import extract_video, save_video\n",
"vid = extract_video(name, '00:00:32', '00:00:45')\n",
"save_video(vid, \"mpoppins.mp4\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convertir la vid\u00e9o en images"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -9,3 +9,9 @@ Download
++++++++
.. autosignature:: code_beatrix.faq.faq_video.download_youtube_video
.. autosignature:: code_beatrix.faq.faq_video.extract_video
.. autosignature:: code_beatrix.faq.faq_video.video_enumerate_frames
.. autosignature:: code_beatrix.faq.faq_video.save_video
View
@@ -134,7 +134,7 @@ def verbose():
if is_local() and "--help" not in sys.argv and "--help-commands" not in sys.argv and \
'moviepy-setup' not in sys.argv:
'moviepy-setup' not in sys.argv:
def write_version():
pyquickhelper = import_pyquickhelper()
@@ -151,7 +151,8 @@ def write_version():
if subversion == ".0":
raise Exception("Subversion is wrong: '{0}'.".format(subversion))
else:
raise FileNotFoundError("Unable to find '{0}' argv={1}".format(versiontxt, sys.argv))
raise FileNotFoundError(
"Unable to find '{0}' argv={1}".format(versiontxt, sys.argv))
else:
# when the module is installed, no commit number is displayed
subversion = ""
@@ -99,3 +99,25 @@ def save_video(video_or_file, filename, verbose=False, **kwargs):
with redirect_stdout(f):
with redirect_stderr(f):
video.write_videofile(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.
Itère sur des images depuis une vidéo.
@param video_or_file string or :epkg:`VideoClip`
@param folder where to exports the images or returns arrays if None
@param pattern image names
@param fps frames per seconds
@param kwargs arguments to `iter_frames <https://zulko.github.io/moviepy/ref/AudioClip.html?highlight=frames#moviepy.audio.AudioClip.AudioClip.iter_frames>`_
@return iterator on arrays or files
"""
video = get_video(video_or_file)
if folder is None:
for frame in video.iter_frames(fps=fps, **kwargs):
yield frame
else:
for frame in video.iter_frames(fps=fps, **kwargs):
# saves as image
raise NotImplementedError()

0 comments on commit d085304

Please sign in to comment.