如何使用FFMPEG,Celery和RabbitMQ在django中转码MP3文件?

时间:2017-01-05 06:25:36

标签: ffmpeg rabbitmq subprocess celery django-celery

我正在尝试使用django,celery,rabbitMQ和FFMPEG将用户上传的MP3音频文件转码为ogg,ac3 wav或其他格式。但我收到[WinError 10042]的错误在getsockopt或setsockopt调用中指定了未知,无效或不支持的选项或级别

OS-Window 10-64bit Python 3.0 Django - 1.10

这是我遵循的代码:

models.py
import uuid
from django.db import models

   # Create your models here.

    def unique_file_path(instance, filename):
    new_file_name = uuid.uuid4()
    return str(new_file_name)

    class AudioFile(models.Model):
    name = models.CharField(max_length=100, blank=True)
    mp3_file = models.FileField(upload_to=unique_file_path)
    ogg_file = models.FileField(blank=True, upload_to=unique_file_path)
    wav_file = models.FileField(blank=True, upload_to=unique_file_path)
    ac3_file = models.FileField(blank=True, upload_to=unique_file_path)

    def __str__(self):
        return self.name

    views.py
    from django.shortcuts import render

    # Create your views here.

    from django.views.generic.edit import FormView
    from django.http import HttpResponseRedirect
    from django.core.urlresolvers import reverse

    from django.views.generic import FormView
    from audio_transcoder.taskapp.tasks import transcode_mp3

    from .forms import AudioFileFrom
    from .models import AudioFile

    class UploadAudioFileView(FormView):
    template_name = 'upload/upload.html'
    form_class = AudioFileFrom


    def form_valid(self, form):
        audio_file = AudioFile(
            name=self.get_form_kwargs().get('data')['name'],
            mp3_file=self.get_form_kwargs().get('files')['mp3_file']
        )
        audio_file.save()
        transcode_mp3.delay(audio_file.id)

        return HttpResponseRedirect(self.get_success_url())

    def get_success_url(self):
        return reverse('/')


    tasks.py
    import os
    import os.path
    import subprocess

    from audio_transcoder.taskapp.celery import app




    from celery import Celery

    app = Celery('fftest',
             broker='amqp://guest@localhost//',
             include=['taskapp.tasks'])

    if __name__ == '__main__':
    app.start()

    from audio_transcoder.models import AudioFile
    import fftest.settings as settings

    @app.task
    def transcode_mp3(mp3_id):
    audio_file = AudioFile.objects.get(id=mp3_id)
    input_file_path = audio_file.mp3_file.path
    filename = os.path.basename(input_file_path)

    ogg_output_file_name = os.path.join('transcoded', '{}.ogg'.format(filename))
    ogg_output_file_path = os.path.join(settings.MEDIA_ROOT, ogg_`enter code     
    here`output_file_name)
    enter code here
    ac3_output_file_name = os.path.join('transcoded', '{}.ac3'.format(filename))
    ac3_output_file_path = os.path.join(settings.MEDIA_ROOT,  
    ac3_output_file_name)

    wav_output_file_name = os.path.join('transcoded', '{}.wav'.format(filename))
    wav_output_file_path = os.path.join(settings.MEDIA_ROOT, 
    wav_output_file_name)

    if not os.path.isdir(os.path.dirname(ogg_output_file_path)):
        os.makedirs(os.path.dirname(ogg_output_file_path))

    subprocess.call([
            settings.FFMPEG_PATH,
            '-i',
            input_file_path,
            ogg_output_file_path,
            ac3_output_file_path,
            wav_output_file_path
        ]
    )

    audio_file.ogg_file = ogg_output_file_name
    audio_file.ac3_file = ac3_output_file_name
    audio_file.wav_file = wav_output_file_name
    audio_file.save()

不确定错误发生在哪里。上传视频时,显示如下:

OperationalError at /new/
[WinError 10042] An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call
Request Method: POST
Request URL:  http://127.0.0.1:8000/new/
Django Version: 1.10.4
Exception Type: OperationalError`enter code here`
Exception Value:  
[WinError 10042] An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call
Exception Location: C:\Users\RAMa2r3e4s5h6\fftest\lib\site-packages\amqp\transport.py in _set_socket_options, line 204

1 个答案:

答案 0 :(得分:0)

如果您使用的是Celery版本4+,则会完全删除Windows支持。

我建议安装celery v.3.1.25

pip install celery==3.1.25

来源:Removed features in Celery 4.0