我网站的一部分包含一个允许用户上传视频的表单。我使用 ffmpeg 将视频转换为 flv。我的媒体和静态文件存储在 Amazon S3 上。我可以让一切在本地完美运行,但是我似乎无法弄清楚如何使用工作人员在生产中运行视频转换子流程。我的应用程序中安装了 dj-celery 和 rq。在我看来,我能够在本地工作的代码是:
#views.py
def upload_broadcast(request):
if request.method == 'POST':
form = VideoUploadForm(request.POST, request.FILES)
if form.is_valid():
new_video=form.save()
def convert_to_flv(video):
filename = video.video_upload
sourcefile = "%s%s" % (settings.MEDIA_ROOT, filename)
flvfilename = "%s.flv" % video.id
imagefilename = "%s.png" % video.id
thumbnailfilename = "%svideos/flv/%s" % (settings.MEDIA_ROOT, imagefilename)
targetfile = "%svideos/flv/%s" % (settings.MEDIA_ROOT, flvfilename)
ffmpeg = "ffmpeg -i %s -acodec mp3 -ar 22050 -f flv -s 320x240 %s" % (sourcefile, targetfile)
grabimage = "ffmpeg -y -i %s -vframes 1 -ss 00:00:02 -an -vcodec png -f rawvideo -s 320x240 %s" % (sourcefile, thumbnailfilename)
print ("SOURCE: %s" % sourcefile)
print ("TARGET: %s" % targetfile)
print ("TARGET IMAGE: %s" % thumbnailfilename)
print ("FFMPEG TASK CODE: %s" % ffmpeg)
print ("IMAGE TASK CODE: %s" % grabimage)
try:
ffmpegresult = subprocess.call(ffmpeg)
print "---------------FFMPEG---------------"
print ffmpegresult
except:
print "Not working."
try:
videothumbnail = subprocess.call(grabimage)
print "---------------IMAGE---------------"
print videothumbnail
except:
print "Not working."
video.flvfilename = flvfilename
video.videothumbnail = imagefilename
video.save()
convert_to_flv(new_video)
return HttpResponseRedirect('/video_list/')
else:
...
这是我第一次尝试使用工人(或曾经将项目推向生产),所以即使有文档,我仍然不清楚我需要做什么。我尝试了几种不同的方法,但似乎没有任何效果。是否有一种简单的方法可以告诉 celery 运行 ffmpegresult = subprocess.call(ffmpeg)?提前感谢您的任何帮助或见解。
编辑-添加了heroku日志
2013-01-10T20:58:57+00:00 app[web.1]: TARGET: /media/videos/flv/8.flv
2013-01-10T20:58:57+00:00 app[web.1]: IMAGE TASK CODE: ffmpeg -y -i /media/videos/practice.wmv -vframes 1 -ss 00:00:02 - an -vcodec png -f rawvideo -s 320x240 /media/videos/flv/8.png
2013-01-10T20:58:57+00:00 app[web.1]: SOURCE: /media/videos/practice.wmv
2013-01-10T20:58:57+00:00 app[web.1]: FFMPEG TASK CODE: ffmpeg -i /media/videos/practice.wmv -acodec mp3 -ar 22050 -f fl v -s 320x240 /media/videos/flv/8.flv
2013-01-10T20:58:57+00:00 app[web.1]: TARGET IMAGE: /media/videos/flv/8.png
2013-01-10T20:58:57+00:00 app[web.1]: Not working.
2013-01-10T20:58:57+00:00 app[web.1]: Not working.
较新的编辑
我尝试添加一个tasks.py并添加了任务:
celery = Celery('tasks', broker='redis://guest@localhost//')
@celery.task
def ffmpeg_task(video):
converted_file = subprocess.call(video)
return converted_file
然后我将视图的相关部分更改为:
...
try:
ffmpeg_task.delay(ffmpeg)
print "---------------FFMPEG---------------"
print ffmpegresult
except:
print "Not working."
...
我的新日志是:
2013-01-15T13:19:52+00:00 app[web.1]: TARGET IMAGE: /media/videos/flv/12.png
2013-01-15T13:19:52+00:00 app[web.1]: SOURCE: /media/videos/practice.wmv
2013-01-15T13:19:52+00:00 app[web.1]: FFMPEG TASK CODE: ffmpeg -i /media/videos/practice.wmv -acodec mp3 -ar 22050 -f fl v -s 320x240 /media/videos/flv/12.flv
2013-01-15T13:19:52+00:00 app[web.1]: IMAGE TASK CODE: ffmpeg -y -i /media/videos/practice.wmv -vframes 1 -ss 00:00:02 -an -vcodec png -f rawvideo -s 320x240 /media/videos/flv/12.png
2013-01-15T13:19:52+00:00 app[web.1]: TARGET: /media/videos/flv/12.flv
2013-01-15T13:20:17+00:00 app[web.1]: 2013-01-15 13:20:17 [2] [CRITICAL] WORKER TIMEOUT (pid:12)
2013-01-15T13:20:17+00:00 app[web.1]: 2013-01-15 13:20:17 [2] [CRITICAL] WORKER TIMEOUT (pid:12)
2013-01-15T13:20:17+00:00 app[web.1]: 2013-01-15 13:20:17 [19] [INFO] Booting worker with pid: 19
我完全错过了什么吗?我会继续努力,但会非常感谢任何方向或帮助。