版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ucsheep/article/details/82772980
使用Docker镜像快速搭建moviepy环境
前言:之前有不少同学说搭建moviepy环境失败了,大部分是卡在ffmpeg自动下载的步骤了,原因在于从国外下载ffmpeg。当然有办法解决:可以手动下载,然后放在~/.imageio/ffmpeg目录下。
ucsheep@ucsheep-B250M-D2V:~/.imageio/ffmpeg$ ll
总用量 44864
drwxrwxr-x 2 ucsheep ucsheep 4096 8月 21 10:44 ./
drwxrwxr-x 3 ucsheep ucsheep 4096 8月 14 08:54 ../
-rwxrw-r-- 1 ucsheep ucsheep 45929032 8月 21 10:44 ffmpeg-linux64-v3.3.1*
但是,其实我这边最开始就直接使用的是moviepy的docker镜像,很方便。也不会遇到上述的情况。那么,在那里可以买的到呢?
有两种办法,一种是通过Dockerfile构建,一种是直接在Docker Hub寻找有么有现成的镜像。
注意:进行以下操作的首要前提是,您的电脑已经安装Docker
1.Docker Hub拉取镜像
首先,查找镜像
ucsheep@ucsheep-B250M-D2V:~$ docker search moviepy
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
dkarchmervue/moviepy Image for moviepy, with latest Python 3.4, l… 1 [OK]
jdauphant/moviepy 0 [OK]
mentlsve/anaconda3-opencv-moviepy Anaconda3 (Python 3.5) with opencv and movie… 0
srid/moviepy 0 [OK]
mbeacom/moviepy 0
jacquesvdm/moviepy Image that contains PHP and MoviePy 0
hongyusir/python-moviepy docker image with python, pil and moviepy in… 0
ib5k/moviepy 0
screencom/moviepy 0
verynb/moviepy 0
hongyusir/mnpp mongo, nodejs and python together, and with … 0
lucasrodesg/deepo Modified from ufoym/deepo. Incorporates pyth… 0
然后,选择第一个,拉取镜像,创建一个容器
ucsheep@ucsheep-B250M-D2V:~$ docker run -idt --name moviepy-test dkarchmervue/moviepy
4e4afde5a3702171ac345f924f4a40596fb081f20cf6984544c0b7855d5585b7
查看容器运行情况
ucsheep@ucsheep-B250M-D2V:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e4afde5a370 dkarchmervue/moviepy "/bin/bash" About a minute ago Up About a minute moviepy-test
进入容器,看看moviepy环境是否可用
ucsheep@ucsheep-B250M-D2V:~$ docker attach moviepy-test
root@4e4afde5a370:/work#
root@4e4afde5a370:/work# python3
Python 3.4.4 (default, May 25 2016, 06:34:52)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import moviepy
>>>
没有报错,大功告成。可以将开发目录挂载到容器中的work目录,方便运行。
Dockerfile构建
首先,clone MoviePy源码
ucsheep@ucsheep-B250M-D2V:~/git-projects$ git clone https://github.com/Zulko/moviepy.git
查看,看到了Dockerfile
ucsheep@ucsheep-B250M-D2V:~/git-projects$ cd moviepy/
ucsheep@ucsheep-B250M-D2V:~/git-projects/moviepy$ ls
appveyor CONTRIBUTING.md examples MANIFEST.in setup.py
appveyor.yml Dockerfile ez_setup.py moviepy tests
CHANGELOG.md docs LICENCE.txt README.rst
Dockerfile的内容如下
FROM python:3
# Install numpy using system package manager
RUN apt-get -y update && apt-get -y install libav-tools imagemagick libopencv-dev python-opencv
# Install some special fonts we use in testing, etc..
RUN apt-get -y install fonts-liberation
RUN apt-get install -y locales && \
locale-gen C.UTF-8 && \
/usr/sbin/update-locale LANG=C.UTF-8
ENV LC_ALL C.UTF-8
# do we need all of these, maybe remove some of them?
RUN pip install imageio numpy scipy matplotlib pandas sympy nose decorator tqdm pillow pytest requests
# install scikit-image after the other deps, it doesn't cause errors this way.
RUN pip install scikit-image sklearn
ADD . /var/src/moviepy/
#RUN git clone https://github.com/Zulko/moviepy.git /var/src/moviepy
RUN cd /var/src/moviepy/ && python setup.py install
# install ffmpeg from imageio.
RUN python -c "import imageio; imageio.plugins.ffmpeg.download()"
#add soft link so that ffmpeg can executed (like usual) from command line
RUN ln -s /root/.imageio/ffmpeg/ffmpeg.linux64 /usr/bin/ffmpeg
# modify ImageMagick policy file so that Textclips work correctly.
RUN cat /etc/ImageMagick-6/policy.xml | sed 's/none/read,write/g'> /etc/ImageMagick-6/policy.xml
就是在Python3的基础上,搭建量MoviePy的运行环境
那么,接下来,开始构建
ucsheep@ucsheep-B250M-D2V:~/git-projects/moviepy$ docker build -t moviepy -f Dockerfile .
Sending build context to Docker daemon 7.115MB
Step 1/12 : FROM python:3
---> 825141134528
Step 2/12 : RUN apt-get -y update && apt-get -y install libav-tools imagemagick libopencv-dev python-opencv
---> Running in b09eb178cc0a
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:3 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:4 http://deb.debian.org/debian stretch Release [118 kB]
Get:5 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [437 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [5148 B]
Get:7 http://deb.debian.org/debian stretch Release.gpg [2434 B]
Get:8 http://deb.debian.org/debian stretch/main amd64 Packages [7099 kB]
最终执行到Step 12/12后会构建完毕
接下来,可以运行容器,测试moviepy给出的示例代码:
ucsheep@ucsheep-B250M-D2V:~/git-projects/moviepy$ cd tests/
ucsheep@ucsheep-B250M-D2V:~/git-projects/moviepy/tests$ ls
download_media.py test_fx.py test_resourcerelease.py
__init__.py test_helper.py test_TextClip.py
README.rst test_ImageSequenceClip.py test_tools.py
test_AudioClips.py test_issues.py test_VideoClip.py
test_compositing.py test_misc.py test_VideoFileClip.py
test_examples.py test_PR.py test_Videos.py
test_ffmpeg_reader.py test_resourcereleasedemo.py test_videotools.py
ucsheep@ucsheep-B250M-D2V:~/git-projects/moviepy/tests$ docker run -it -v `pwd`:/tests moviepy bash