Skip to content

Commit

Permalink
Use mp4 for movies; requires ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
dsblank committed Jan 12, 2018
1 parent 4577c51 commit 34859e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export VERSION=`python3 setup.py --version 2>/dev/null`
all:
rm -rf dist
pip install wheel -U
python setup.py register
python setup.py bdist_wheel
python setup.py sdist --formats=zip
python3 setup.py register
python3 setup.py bdist_wheel
python3 setup.py sdist --formats=zip
twine upload dist/*

tag:
Expand Down
25 changes: 23 additions & 2 deletions jyro/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import html
import sys
import io
import os
import threading

from jyro.simulator.color import colorMap, colorCode
Expand Down Expand Up @@ -672,7 +673,7 @@ def image_to_uri(self, img_src):
return "data:image/gif;base64,%s" % html.escape(data)

def movie(self, poses, function, movie_name=None, start=0, stop=None, step=1,
loop=0, optimize=True, duration=100, embed=False):
loop=0, optimize=True, duration=100, embed=False, mp4=True):
"""
Make a movie from a list of poses and a function.
Expand Down Expand Up @@ -726,7 +727,10 @@ def movie(self, poses, function, movie_name=None, start=0, stop=None, step=1,
if frames:
frames[0].save(movie_name, save_all=True, append_images=frames[1:],
optimize=optimize, loop=loop, duration=duration)
return Image(url=movie_name, embed=embed)
if mp4 is False:
return Image(url=movie_name, embed=embed)
else:
return gif2mp4(movie_name)

def playback(self, poses, function, play_rate=0.0):
"""
Expand Down Expand Up @@ -1043,3 +1047,20 @@ def update_slider_control(self, change):
results = [results]
for i in range(len(self.displayers)):
self.displayers[i].update(results[i])

## Utilities

def gif2mp4(filename):
"""
Convert an animated gif into a mp4, to show with controls.
"""
from IPython.display import HTML
if filename.endswith(".gif"):
filename = filename[:-4]
if os.path.exists(filename + ".mp4"):
os.remove(filename + ".mp4")
retval = os.system("""ffmpeg -i {0}.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" {0}.mp4""".format(filename))
if retval == 0:
return HTML("""<video src='{0}.mp4' controls></video>""".format(filename))
else:
print("error running ffmpeg; see console log message or use mp4=False")

0 comments on commit 34859e4

Please sign in to comment.