実現したいこと
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

あなたの回答
tips
プレビュー