Synthesize video files with mp3 and images using ffmpeg in MacOS
First step you should install dependence files.
brew install ffmpeg
brew install libmagic
pip install eyed3
Then you can use the terminal to test the ffmpeg command. And you should see the ffmpeg help output in your terminal if you install ffmpeg success.
Use the python file to make your video files.
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import os
import urllib
import eyed3
import time
musicPath = '/Users/zhangyuqing/Desktop/tianshidechibang.mp3'
picturePath = '/Users/zhangyuqing/Desktop/timeradio2.jpg'
outFileName = 'tianshidechibang'
xx = eyed3.load(str(musicPath))
seconds = xx.info.time_secs # 获取音乐时长,为渲染一图流视频做准备
print seconds
os.system('ffmpeg -loop 1 -r 1 -t '+str(seconds)+' -f image2 -i '+ str(picturePath) + ' -vcodec libx264 -pix_fmt yuv420p -crf 24 -y SinglePictureVideo.mp4') #渲染一图流视频,参数都是我做完试出来的,尤其是-pix_fmt yuv420p这个参数,缺了它视频就是黑屏了
os.system('ffmpeg -i SinglePictureVideo.mp4 -i '+ str(musicPath) +' -c:v copy -c:a aac -y '+ str(outFileName) +'.flv') #合并视频与音频
os.remove('SinglePictureVideo.mp4')