instance = Video.objects.get(pk=pk)
dirt = {'test':'test'}
vfile = instance.fileuri.url
vpath = settings.BASE_DIR + vfile
dirt['vfile'] = vfile
dirt['vpath'] = vpath
result = os.path.exists(vpath)
dirt['vpath_flag'] = result
if result:
realname = os.path.basename(vfile)
image_name = 'uploads/video_images/' + os.path.splitext(realname)[0] + '.jpg'
image_path = settings.BASE_DIR + '/' + image_name
dirt['image_name'] = image_name
dirt['image_path'] = image_path
dirt['realname'] = realname
result2 = os.path.exists(image_path)
dirt['result2'] = result2
if not result2:
clip = VideoFileClip(vpath)
dirt['duration'] = clip.duration
clip.save_frame(image_name, t=2)
size = os.path.getsize(vpath)
dirt['size'] = size
2.很确定视频文件是存在的。
在本地跑运行,生成截图也没有问题,但是放到测试服务器上就一直报错。
IndexError at /admin/gp/video/9/change/
list index out of range
Request Method: POST
Request URL: http://gp.test.com/admin/gp/video/9/change/
Django Version: 1.11.6
Exception Type: IndexError
Exception Value:
list index out of range
Exception Location: /www/video/env/lib/python2.7/site-packages/moviepy/video/io/ffmpeg_reader.py in ffmpeg_parse_infos, line 269
Python Executable: /www/video/env/bin/python2.7
Python Version: 2.7.11
Python Path:
['/www/video/gpSource',
'/www/video/env/bin',
'/www/video/env/lib/python27.zip',
'/www/video/env/lib/python2.7',
'/www/video/env/lib/python2.7/plat-linux2',
'/www/video/env/lib/python2.7/lib-tk',
'/www/video/env/lib/python2.7/lib-old',
'/www/video/env/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/www/video/env/lib/python2.7/site-packages']
Server time: 星期五, 18 一月 2019 10:49:30 +0800
应该是文件路径的问题,到文件目录下pwd检查路径。以下是我在截取封面的时候所用到的方法,因为我的项目是python3写的,在程序中调用的该文件。希望对你有所帮助。
import os
import sys
import time
from ffvideo import VideoStream
import Image
#ffmpeg -i WSWDA1768844088.mp4 -b 800k -strict -2 output.mp4
BASE_DIR =os.path.dirname( os.path.dirname(os.path.abspath(__file__)))
print sys.argv[1]
path = BASE_DIR+sys.argv[1]
pil_image = VideoStream( path).get_frame_at_sec(0.01).image()
picturepath = path[:path.rindex('.')]
pil_image.save(picturepath+'.jpeg')
im = Image.open(picturepath+'.jpeg')
(x,y) = im.size
if x < 400 and y < 400:
x_s=s
y_s=y
else:
x_s = 400 # define standard widt
y_s = y
while x_s > 400 or y_s > 400:
x_s = x_s - 1
if x_s < 400:
print
x_s, y_s, x, y
y_s = int(y * x_s * 1.0 / x)
print
y_s
if x_s > 400:
print
x_s, y_s, x, y
y_s = int(y * x * 1.0 / x_s)
out = im.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-quality
out.save(picturepath+'.jpeg')