-
Notifications
You must be signed in to change notification settings - Fork 0
/
videokit.py
67 lines (41 loc) · 1.7 KB
/
videokit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from utils import runCommand
class VideoMaker:
def __init__(self):
self.videoList = None
def appendVideo(self, srcVideoPath):
if self.videoList is None:
self.videoList = list()
self.videoList.append(srcVideoPath)
return self
def merge(self, dstVideoPath):
if self.videoList is None:
return
configPath = '{}.txt'.format(dstVideoPath)
with open(configPath, 'w') as fp:
for video in self.videoList:
fp.write('file \'{}\'\n'.format(video))
print('Merge all to', dstVideoPath, 'from', configPath)
cmd = 'ffmpeg -y -f concat -safe 0 -i {} -c copy {}'.format(configPath, dstVideoPath)
runCommand(cmd)
class VideoKit:
@staticmethod
def createLoopVideo(dstVideoPath, srcImagePath, videoLength):
print('Create video to', dstVideoPath, 'from', srcImagePath, 'with length', videoLength)
cmd = 'ffmpeg -y -loop 1 -i {} -c:v libx264 -t {:.2f} -pix_fmt yuv420p {}'.format(srcImagePath,
videoLength, dstVideoPath)
runCommand(cmd)
return dstVideoPath
@staticmethod
def appendVideo(srcVideoPath, videoMaker=None):
if videoMaker is None:
videoMaker = VideoMaker()
videoMaker.appendVideo(srcVideoPath)
return videoMaker
@staticmethod
def merge(dstVideoPath, srcVideoPath, srcAudioPath):
print('Merge', srcVideoPath, 'and', srcAudioPath, 'to', dstVideoPath)
cmd = 'ffmpeg -y -i {} -i {} -c copy -map \'0:v:0\' -map \'1:a:0\' {}'.format(srcVideoPath,
srcAudioPath, dstVideoPath)
runCommand(cmd)