質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1216閲覧

google colabratoryでpythonライブラリのmoviepyのメソッドTextClipを実行したい。(Windows)

sasa__tomo

総合スコア1

Google Colaboratory

Google Colaboratoryとは、無償のJupyterノートブック環境。教育や研究機関の機械学習の普及のためのGoogleの研究プロジェクトです。PythonやNumpyといった機械学習で要する大方の環境がすでに構築されており、コードの記述・実行、解析の保存・共有などが可能です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/03/26 12:56

実現したいこと

google colabratoryでpythonライブラリのmoviepyのメソッドTextClipを実行したい。

前提

google colab上で動画編集ライブラリmoviepyのTextClipを使って、動画にテロップをつけようと思っています。しかしWindowsで実現するにはImageMagickをインストールしてから、以下のコードを記述する必要があるそうです。

python

1#change_settings({"IMAGEMAGICK_BINARY": r"drive/MyDrive/whisper/content/ImageMagick-7.1.1-Q16-HDRI/magick.exe"}) 2IMAGEMAGICK_BINARY = os.getenv('IMAGEMAGICK_BINARY', r'drive/MyDrive/whisper/content/ImageMagick-7.1.1-Q16-HDRI/magick.exe')

実際に記述してVScodeで実行したところうまくいきました。
しかしGoogle colab上ではエラーが出てしまいます。(個人的に環境変数にはローカルのパスを使ってないからかなとか思ってます)

発生している問題・エラーメッセージ

--------------------------------------------------------------------------- PermissionError Traceback (most recent call last) /usr/local/lib/python3.9/dist-packages/moviepy/video/VideoClip.py in __init__(self, txt, filename, size, color, bg_color, fontsize, font, stroke_color, stroke_width, method, kerning, align, interline, tempfilename, temptxt, transparent, remove_temp, print_cmd) 1136 try: -> 1137 subprocess_call(cmd, logger=None) 1138 except (IOError, OSError) as err: 4 frames PermissionError: [Errno 13] Permission denied: '/content/drive/MyDrive/whisper/content/ImageMagick-7.1.1-Q16-HDRI/magick.exe' During handling of the above exception, another exception occurred: OSError Traceback (most recent call last) /usr/local/lib/python3.9/dist-packages/moviepy/video/VideoClip.py in __init__(self, txt, filename, size, color, bg_color, fontsize, font, stroke_color, stroke_width, method, kerning, align, interline, tempfilename, temptxt, transparent, remove_temp, print_cmd) 1144 "ImageMagick binary in file conf.py, or that the path " 1145 "you specified is incorrect")) -> 1146 raise IOError(error) 1147 1148 ImageClip.__init__(self, tempfilename, transparent=transparent) OSError: MoviePy Error: creation of None failed because of the following error: [Errno 13] Permission denied: '/content/drive/MyDrive/whisper/content/ImageMagick-7.1.1-Q16-HDRI/magick.exe'. .This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect

該当のソースコード

python

1#Google colaboratoryでの記述 2from moviepy.editor import * 3from moviepy.config import change_settings 4import os 5 6#7,8行目どちらでもいいそうです。 7change_settings({"IMAGEMAGICK_BINARY": r'/content/drive/MyDrive/whisper/content/ImageMagick-7.1.1-Q16-HDRI/magick.exe'}) 8#IMAGEMAGICK_BINARY = os.getenv('IMAGEMAGICK_BINARY', r'/content/drive/MyDrive/whisper/content/ImageMagick-7.1.1-Q16-HDRI/magick.exe') 9 10path = "/content/drive/MyDrive/whisper/content/kaze_short.mp4" 11clip = VideoFileClip(path).subclip("00:00:20", "00:00:24") 12 13 14txtclip = TextClip('Fujii Kaze', fontsize=80, font='/content/drive/MyDrive/whisper/content/ipaexm.ttf',color='white') 15 16cvc = CompositeVideoClip([clip, txtclip.set_pos(('center', 'center'))]) 17final_clip = cvc.set_duration(clip.duration) 18 19final_clip.write_videofile("telop.mp4")

試したこと

VScodeで以下を実行したらうまくいきました。

Python

1#VScodeでの記述 2from moviepy.editor import * 3from moviepy.config import change_settings 4import os 5 6#パスがローカルのパスに代わってます 7change_settings({"IMAGEMAGICK_BINARY":r"C:\\Program Files\\ImageMagick-7.1.1-Q16-HDRI\\magick.exe"}) 8#IMAGEMAGICK_BINARY = os.getenv('IMAGEMAGICK_BINARY', r"C:\\Program Files\\ImageMagick-7.1.1-Q16-HDRI\\magick.exe") 9 10path = "./movie/kaze_short.mp4" 11clip = VideoFileClip(path).subclip("00:00:20", "00:00:24") 12 13 14txtclip = TextClip('Fujii Kaze', fontsize=80, font="./fonts/ipaexm.ttf",color='white') 15 16cvc = CompositeVideoClip([clip, txtclip.set_pos(('center', 'center'))]) 17final_clip = cvc.set_duration(clip.duration) 18 19final_clip.write_videofile("telop.mp4")

補足情報(FW/ツールのバージョンなど)

OS:Windows
python3.9

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2023/03/26 13:19

Google Colaboratory が稼働している OS は Linux ですので Windows の EXE ファイルを実行することはできません。なので、magic command で ffmpeg パッケージをインストールしてみてください。 !apt install ffmpeg インストールが正常に完了したら、 IMAGEMAGICK_BINARY = os.getenv('...') を削除するかコメントアウトしてください。
sasa__tomo

2023/03/26 13:49

コメントありがとうございます。なぜエラーが出てしまうのか理解できました。 やってみましたが以下のようなエラーが出てしまいました。 [Errno 2] No such file or directory: 'unset'. .This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect
melian

2023/03/26 13:53

ごめんなさい、書き間違えてしまいました。ffmpeg ではなく imagemagick でした。 !apt install imagemagick
sasa__tomo

2023/03/26 14:01 編集

ありがとうございます。 やはりエラーがでてしまうようです。 convert-im6.q16: attempt to perform an operation not allowed by the security policy `@/tmp/tmpo5jqa9my.txt' @ error/property.c/InterpretImageProperties/3666. convert-im6.q16: no images defined `PNG32:/tmp/tmpna4sqgon.png' @ error/convert.c/ConvertImageCommand/3258. . .This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect 質問から逸れてしまうのですが、google colaboratoryがLinuxで動いているのに、for Windows usersのようにWindowsのエラーになるのか疑問に思ったので分かれば教えていただきたいです。
melian

2023/03/26 14:54

> ... or (for Windows users) that you didn't specify the path to ... or で接続されているので、「Windows ユーザの場合は〜」程度の意味かと思います。 > attempt to perform an operation not allowed by the security policy security policy を書き換える必要がありますので、以下の magic command で実行して下さい。 !sed -i 's:<policy domain="path" rights="none" pattern="@\*"/>:<!-- $& -->:' /etc/ImageMagick-6/policy.xml また、ipaexfont をインストールしてください。 !apt install fonts-ipaexfont-mincho そして、TextClip の部分を以下の様に変更してみて下さい。 txtclip = TextClip('Fujii Kaze', fontsize=80, font='/content/drive/MyDrive/whisper/content/ipaexm.ttf',color='white') => txtclip = TextClip('Fujii Kaze', fontsize=80, font='/usr/share/fonts/opentype/ipaexfont-mincho/ipaexm.ttf',color='white')
sasa__tomo

2023/03/28 03:14

返信遅くなり申し訳ありません。 実行できました。本当にありがとうございます!
sasa__tomo

2023/03/28 04:45

google coraboサーバー上のpolicy.xmlの場所などはfindコマンドなどを使って探しているのでしょうか? それとも!apt install pkg でのインストール先は決まりがあるのでしょうか? 試しに!find / -name "ImageMagick*"と実行してみたのですが複数出てきてしまいました。 何度も申し訳ないですが、余裕がありましたら教えていただきたいです。
melian

2023/03/28 04:54

はい、実際には手元に Ubuntu Linux をインストールした PC があって、locate コマンドを使ってパスを調べました。 $ locate -r policy.xml /etc/ImageMagick-6/policy.xml ただ、Google Colab ですとこの方法は使えないと思います。なので、dpkg コマンドの方がよいかと思います。 $ dpkg --listfiles imagemagick-6-common | grep policy /etc/ImageMagick-6/policy.xml
sasa__tomo

2023/03/28 13:39

理解しました。 丁寧にありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問