使用 Python 脚本将长视频剪切成 FFMPEG 中的块
Using Python script to cut long videos into chunks in FFMPEG
首先说我不是程序员,但我确实需要应用程序这个 Python 我发现的脚本说它可以做到。
Auto-Splitting Script by Antarctic Nest of Icephoenix
基本上我有一个长的 .MP4 目录,需要根据 运行3 小时 15 分钟的总时间将其分成相等的部分。例如,我有一个 8 小时的视频需要在 3:15:00.
下切割成更小的部分
我们一直在手动创建 FFMPEG 代码来执行此操作,但我发现上面的 Python 脚本似乎可以满足我们的需要。问题是我没有 Python 经验。我不知道在脚本的哪个位置输入视频的文件夹路径,或者在哪里指定我的编解码器,或者在哪里告诉程序每个视频块的最大时间是 3:15:00.
我使用的是 64 位 windows 系统,在命令提示符下工作
这是我所做的:
- 已安装 python 3
- 下载脚本
- 我可以点击脚本看到cmd window flash 说明是运行ning
- 我在cmd里输入"C:\Python34\python.exe V:\ffmpeg\ffmpeg-split.py"
输出为
文件 "V:\ffmpeg\ffmpeg-split.py",第 16 行
打印 "Split length can't be 0"
SyntaxError: Missing parentheses in call to 'print'
我不知道从这里去哪里。脚本似乎正在正确加载,但我还没有输入我的变量。任何有关将信息放在哪里的帮助将不胜感激。
下面是我们常用的FFMPEG代码:
ffmpeg -i V:\ffmpeg518_63c392af.mp4 -vcodec libx264 -acodec copy -vf fps=fps=30000/1001 -ss 00:05:01.000 -t 02:43:49.000 V:\events518.mp4
我们使用的ffmpeg代码:
-i 是.mp4
-vcodec h.264 编解码器
-acodec 应该是“copy”或者可以是“libvo_aacenc”
-vf fps=30000/1000 强制 fps 为 29.97
-ss 是开始时间(我们将使用它与-t 一起手动切割成部分)
-t 是持续时间(我们将每个部分的持续时间计算为总 运行 时间除以 3:15:00 下的相等时间,无论是两个、三个还是四个部分)
谢谢你一百万美元
python脚本好像是用Python写的 2.很遗憾,运行用Python不行 3.需要卸载Python 3 并安装 Python 2.7.
您需要在打印中添加括号并将异常语法更改为 except Exception as e
:
import subprocess
import re
import math
from optparse import OptionParser
length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)
def main():
(filename, split_length) = parse_options()
if split_length <= 0:
print("Split length can't be 0")
raise SystemExit
output = subprocess.Popen("ffmpeg -i '"+filename+"' 2>&1 | grep 'Duration'",
stdout = subprocess.PIPE
).stdout.read()
print(output)
matches = re_length.search(output)
if matches:
video_length = int(matches.group(1)) * 3600 + \
int(matches.group(2)) * 60 + \
int(matches.group(3))
print("Video length in seconds: " + str(video_length))
else:
print("Can't determine video length.")
raise SystemExit
split_count = math.ceil(video_length/float(split_length))
if split_count == 1:
print("Video length is less then the target split length.")
raise SystemExit
split_cmd = "ffmpeg -i '"+filename+"' -vcodec copy "
for n in range(0, split_count):
split_str = ""
if n == 0:
split_start = 0
else:
split_start = split_length * n
split_str += " -ss "+str(split_start)+" -t "+str(split_length) + \
" '"+filename[:-4] + "-" + str(n) + "." + filename[-3:] + \
"'"
print("About to run: " + split_cmd + split_str)
output = subprocess.Popen(split_cmd+split_str, shell = True, stdout =
subprocess.PIPE).stdout.read()
def parse_options():
parser = OptionParser()
parser.add_option("-f", "--file",
dest = "filename",
help = "file to split, for example sample.avi",
type = "string",
action = "store"
)
parser.add_option("-s", "--split-size",
dest = "split_size",
help = "split or chunk size in seconds, for example 10",
type = "int",
action = "store"
)
(options, args) = parser.parse_args()
if options.filename and options.split_size:
return (options.filename, options.split_size)
else:
parser.print_help()
raise SystemExit
if __name__ == '__main__':
try:
main()
except Exception as e:
print("Exception occured running main():")
print(e)
经过这些更改后,脚本将 运行 正常,还可以改进。
更新版本应与 python2 和 3:
兼容
import re
import math
from optparse import OptionParser
length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)
from subprocess import check_call, PIPE, Popen
import shlex
def main():
filename, split_length = parse_options()
if split_length <= 0:
print("Split length can't be 0")
raise SystemExit
p1 = Popen(["ffmpeg", "-i", filename], stdout=PIPE, stderr=PIPE, universal_newlines=True)
# get p1.stderr as input
output = Popen(["grep", 'Duration'], stdin=p1.stderr, stdout=PIPE, universal_newlines=True)
p1.stdout.close()
matches = re_length.search(output.stdout.read())
if matches:
video_length = int(matches.group(1)) * 3600 + \
int(matches.group(2)) * 60 + \
int(matches.group(3))
print("Video length in seconds: {}".format(video_length))
else:
print("Can't determine video length.")
raise SystemExit
split_count = math.ceil(video_length / split_length)
if split_count == 1:
print("Video length is less than the target split length.")
raise SystemExit
for n in range(split_count):
split_start = split_length * n
pth, ext = filename.rsplit(".", 1)
cmd = "ffmpeg -i {} -vcodec copy -strict -2 -ss {} -t {} {}-{}.{}".\
format(filename, split_start, split_length, pth, n, ext)
print("About to run: {}".format(cmd))
check_call(shlex.split(cmd), universal_newlines=True)
def parse_options():
parser = OptionParser()
parser.add_option("-f", "--file",
dest="filename",
help="file to split, for example sample.avi",
type="string",
action="store"
)
parser.add_option("-s", "--split-size",
dest="split_size",
help="split or chunk size in seconds, for example 10",
type="int",
action="store"
)
(options, args) = parser.parse_args()
if options.filename and options.split_size:
return options.filename, options.split_size
else:
parser.print_help()
raise SystemExit
if __name__ == '__main__':
try:
main()
except Exception as e:
print(e)
你可以从 here
安装 FFMPEG
这里是 python 代码,用于将长视频剪切成 5 分钟的块,即(600 秒)
import os
import re
import sys
import subprocess
from decimal import Decimal
process = subprocess.Popen(['ffmpeg', '-i', path_of_video_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
matches = re.search(r"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict()
total_sec=(Decimal(matches['hours'])*60*60)+(Decimal(matches['minutes'])*60)+(Decimal(matches['seconds'])) #duration of the media file
dir_name=path_of_video_file.split('.')[0]+"_chunks"
if not os.path.exists(dir_name):
os.makedirs(dir_name) #creating directory to save the chunks of the media file
from_time=0
file_no=1
file_names=path_of_video_file.split('.')[0]+'_'
while total_sec>0:
os.system('ffmpeg -ss '+str(from_time)+' -t 600 -i '+path_of_video_file+' '+dir_name+'/'+file_names+str(file_no)+'.mp4') #In case if you want the chunks of the file to be in other formats then you can change 'mp4' to the required audio or video format.
file_no=file_no+1
from_time=from_time+600
total_sec=total_sec-600
首先说我不是程序员,但我确实需要应用程序这个 Python 我发现的脚本说它可以做到。
Auto-Splitting Script by Antarctic Nest of Icephoenix
基本上我有一个长的 .MP4 目录,需要根据 运行3 小时 15 分钟的总时间将其分成相等的部分。例如,我有一个 8 小时的视频需要在 3:15:00.
下切割成更小的部分我们一直在手动创建 FFMPEG 代码来执行此操作,但我发现上面的 Python 脚本似乎可以满足我们的需要。问题是我没有 Python 经验。我不知道在脚本的哪个位置输入视频的文件夹路径,或者在哪里指定我的编解码器,或者在哪里告诉程序每个视频块的最大时间是 3:15:00.
我使用的是 64 位 windows 系统,在命令提示符下工作
这是我所做的:
- 已安装 python 3
- 下载脚本
- 我可以点击脚本看到cmd window flash 说明是运行ning
- 我在cmd里输入"C:\Python34\python.exe V:\ffmpeg\ffmpeg-split.py"
输出为
文件 "V:\ffmpeg\ffmpeg-split.py",第 16 行 打印 "Split length can't be 0"
SyntaxError: Missing parentheses in call to 'print'
我不知道从这里去哪里。脚本似乎正在正确加载,但我还没有输入我的变量。任何有关将信息放在哪里的帮助将不胜感激。
下面是我们常用的FFMPEG代码:
ffmpeg -i V:\ffmpeg518_63c392af.mp4 -vcodec libx264 -acodec copy -vf fps=fps=30000/1001 -ss 00:05:01.000 -t 02:43:49.000 V:\events518.mp4
我们使用的ffmpeg代码:
-i 是.mp4
-vcodec h.264 编解码器
-acodec 应该是“copy”或者可以是“libvo_aacenc”
-vf fps=30000/1000 强制 fps 为 29.97
-ss 是开始时间(我们将使用它与-t 一起手动切割成部分)
-t 是持续时间(我们将每个部分的持续时间计算为总 运行 时间除以 3:15:00 下的相等时间,无论是两个、三个还是四个部分)
谢谢你一百万美元
python脚本好像是用Python写的 2.很遗憾,运行用Python不行 3.需要卸载Python 3 并安装 Python 2.7.
您需要在打印中添加括号并将异常语法更改为 except Exception as e
:
import subprocess
import re
import math
from optparse import OptionParser
length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)
def main():
(filename, split_length) = parse_options()
if split_length <= 0:
print("Split length can't be 0")
raise SystemExit
output = subprocess.Popen("ffmpeg -i '"+filename+"' 2>&1 | grep 'Duration'",
stdout = subprocess.PIPE
).stdout.read()
print(output)
matches = re_length.search(output)
if matches:
video_length = int(matches.group(1)) * 3600 + \
int(matches.group(2)) * 60 + \
int(matches.group(3))
print("Video length in seconds: " + str(video_length))
else:
print("Can't determine video length.")
raise SystemExit
split_count = math.ceil(video_length/float(split_length))
if split_count == 1:
print("Video length is less then the target split length.")
raise SystemExit
split_cmd = "ffmpeg -i '"+filename+"' -vcodec copy "
for n in range(0, split_count):
split_str = ""
if n == 0:
split_start = 0
else:
split_start = split_length * n
split_str += " -ss "+str(split_start)+" -t "+str(split_length) + \
" '"+filename[:-4] + "-" + str(n) + "." + filename[-3:] + \
"'"
print("About to run: " + split_cmd + split_str)
output = subprocess.Popen(split_cmd+split_str, shell = True, stdout =
subprocess.PIPE).stdout.read()
def parse_options():
parser = OptionParser()
parser.add_option("-f", "--file",
dest = "filename",
help = "file to split, for example sample.avi",
type = "string",
action = "store"
)
parser.add_option("-s", "--split-size",
dest = "split_size",
help = "split or chunk size in seconds, for example 10",
type = "int",
action = "store"
)
(options, args) = parser.parse_args()
if options.filename and options.split_size:
return (options.filename, options.split_size)
else:
parser.print_help()
raise SystemExit
if __name__ == '__main__':
try:
main()
except Exception as e:
print("Exception occured running main():")
print(e)
经过这些更改后,脚本将 运行 正常,还可以改进。
更新版本应与 python2 和 3:
兼容import re
import math
from optparse import OptionParser
length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
re_length = re.compile(length_regexp)
from subprocess import check_call, PIPE, Popen
import shlex
def main():
filename, split_length = parse_options()
if split_length <= 0:
print("Split length can't be 0")
raise SystemExit
p1 = Popen(["ffmpeg", "-i", filename], stdout=PIPE, stderr=PIPE, universal_newlines=True)
# get p1.stderr as input
output = Popen(["grep", 'Duration'], stdin=p1.stderr, stdout=PIPE, universal_newlines=True)
p1.stdout.close()
matches = re_length.search(output.stdout.read())
if matches:
video_length = int(matches.group(1)) * 3600 + \
int(matches.group(2)) * 60 + \
int(matches.group(3))
print("Video length in seconds: {}".format(video_length))
else:
print("Can't determine video length.")
raise SystemExit
split_count = math.ceil(video_length / split_length)
if split_count == 1:
print("Video length is less than the target split length.")
raise SystemExit
for n in range(split_count):
split_start = split_length * n
pth, ext = filename.rsplit(".", 1)
cmd = "ffmpeg -i {} -vcodec copy -strict -2 -ss {} -t {} {}-{}.{}".\
format(filename, split_start, split_length, pth, n, ext)
print("About to run: {}".format(cmd))
check_call(shlex.split(cmd), universal_newlines=True)
def parse_options():
parser = OptionParser()
parser.add_option("-f", "--file",
dest="filename",
help="file to split, for example sample.avi",
type="string",
action="store"
)
parser.add_option("-s", "--split-size",
dest="split_size",
help="split or chunk size in seconds, for example 10",
type="int",
action="store"
)
(options, args) = parser.parse_args()
if options.filename and options.split_size:
return options.filename, options.split_size
else:
parser.print_help()
raise SystemExit
if __name__ == '__main__':
try:
main()
except Exception as e:
print(e)
你可以从 here
安装 FFMPEG这里是 python 代码,用于将长视频剪切成 5 分钟的块,即(600 秒)
import os
import re
import sys
import subprocess
from decimal import Decimal
process = subprocess.Popen(['ffmpeg', '-i', path_of_video_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
matches = re.search(r"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict()
total_sec=(Decimal(matches['hours'])*60*60)+(Decimal(matches['minutes'])*60)+(Decimal(matches['seconds'])) #duration of the media file
dir_name=path_of_video_file.split('.')[0]+"_chunks"
if not os.path.exists(dir_name):
os.makedirs(dir_name) #creating directory to save the chunks of the media file
from_time=0
file_no=1
file_names=path_of_video_file.split('.')[0]+'_'
while total_sec>0:
os.system('ffmpeg -ss '+str(from_time)+' -t 600 -i '+path_of_video_file+' '+dir_name+'/'+file_names+str(file_no)+'.mp4') #In case if you want the chunks of the file to be in other formats then you can change 'mp4' to the required audio or video format.
file_no=file_no+1
from_time=from_time+600
total_sec=total_sec-600