[PYTHON] I stumbled upon using MoviePy, so make a note

I wanted to edit a video, so I thought I would use moviepy, but I stumbled upon it, so I made a note.

environment

mac Mojave python 3.6.8

Conclusion

The following is easy for the use case of "adding text or images using a video file as Input, and exporting the video file as Output".

pip install moviepy==1.0.0

##* Since we have confirmed the operation only with QuickTime Player on mac, it may not work unless it is here.
##moviepy version 1.0.A little tinkering with 0
# pip install -U git+https://github.com/mynkit/moviepy.git@mynkit/dev

sample_moviepy.py

import moviepy.editor as mp


AUDIOPATH = 'hogehoge.mp4'
IMGPATH = 'hogehoge.png'


video = mp.VideoFileClip(AUDIOPATH)

img = (mp.ImageClip(IMGPATH)
          .set_start(1) #How many seconds in the video to display the image
          .set_duration(10) #How many seconds to display the image
          .resize(height=250) #Image height
          .margin(right=0, top=0, opacity=0) #Margin size setting(No margin this time)
          .set_pos(('right','bottom'))) #This time the image is displayed in the lower right

final = mp.CompositeVideoClip([video, img])
# mynkit/If you have pip install from dev`final.subclip(0,10).write_videofile('test.mp4')`Only need.
final.subclip(0,10).write_videofile(
    "test.mp4",
    codec='libx264', 
    audio_codec='aac', 
    temp_audiofile='temp-audio.m4a', 
    remove_temp=True
) # 0~Test up to 10 seconds.Output to mp4

Stumble point

Apparently, write_videofile doesn't seem to be various.

Write_videofile doesn't work in the first place

At the moment (2020/03/07), the latest version of MoviePy is 1.1.0, but if you execute write_videofile as it is, the following error will occur.

AttributeError: 'NoneType' object has no attribute 'stdout'

Looking at issue938, it says that the version should be lowered to 1.0.0. For the time being, lower it as told.

There is no sound from the output file this time

There is certainly audio in the input file, and on jupyter

final.subclip(0,10).ipython_display(width=400)

Then I was able to preview the sound in the presence. However, there is no sound in the output file. ..

This was also described in issue876.

'-i','-i', described in moviepy / video / io / ffmpeg_writer.py The order of'-','-an'is wrong, and the correct order is'-an','-i','-'`. This was supported by forked on my github.

By the way, even if this correspondence is not taken, it was played by QuickTime Player if only the following correspondence was made.

Can't play on Mac QuickTime Player

I thought that this was safe, but when I checked it with QuickTime Player on mac, there was still no sound ...

As mentioned in issue51 and issue820, with QuickTime Player In the argument of write_videofile to play

    codec='libx264', 
    audio_codec='aac', 
    temp_audiofile='temp-audio.m4a', 
    remove_temp=True

Seems to be necessary. When I exported the video in this state, it worked.

Recommended Posts

I stumbled upon using MoviePy, so make a note
I touched "Orator" so I made a note
[Python] I tried to implement stable sorting, so make a note
I tried using pipenv, so a memo
What I stumbled upon when using CodeIgniter on a Linux server
I was addicted to trying Cython with PyCharm, so make a note
I tried to make a ○ ✕ game using TensorFlow
[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.
[Linux] Let's talk about when I stumbled upon a symbolic link I was using.
I set up TensowFlow and was addicted to it, so make a note
PyTorch Learning Note 2 (I tried using a pre-trained model)
I stumbled upon trying Pylearn 2
I stumbled when I tried to install Basemap, so a memorandum
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
I made a Line-bot using Python!
I tried to make a regular expression of "amount" using Python
Make a face recognizer using TensorFlow
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I tried to make a todo application using bottle with python
Postgres environment construction with Docker I struggled a little, so note
I got stuck trying to install various things with Mac El captain pip, so make a note
I learned scraping using selenium to make a horse racing prediction model.
I tried to make a calculator with Tkinter so I will write it
[Streamlit] I hate JavaScript, so I make a web application only with Python
A note I looked up to make a command line tool in Python
I want to make a web application using React and Python flask
A story that stumbled upon installing matplotlib
I tried playing a ○ ✕ game using TensorFlow
A story that stumbled upon a comparison operation
Beginner: I made a launcher using dictionary
I stumbled upon installing sentencepiece on ubuntu
Let's make a multilingual site using flask-babel
I tried to make a Web API
I wanted to use the find module of Ansible2, but it took some time, so make a note
Suddenly I needed to work on a project using Python and Pyramid, so a note of how I'm studying
Work memorandum (pymongo) Part 3. I don't want to look it up again, so make a note of it (aggregate)
A memo that I stumbled upon when doing a quote RT on Twitter Bot
I tried to make a translation BOT that works on Discord using googletrans
I tried to make a suspicious person MAP quickly using Geolonia address data
I want to make matplotlib a dark theme
I want to make a game with Python
I tried using Pythonect, a dataflow programming language.
I tried reading a CSV file using Python
I read "How to make a hacking lab"
[Chat De Tornado] Make a chat using WebSocket with Tornado
I wrote a Japanese parser in Japanese using pyparsing.
Let's make a module for Python using SWIG
I tried using a database (sqlite3) with kivy
I want to collect a lot of images, so I tried using "google image download"
A Python beginner made a chat bot, so I tried to summarize how to make it.