0

I am writing a Python script to convert a video (.MP4) into an audio file (.MP3) on a Django server. To achieve this, I am using the Moviepy library but when I run the script, I get the following error:

Internal Server Error: /test/
Traceback (most recent call last):
  File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\shoe\musicsite\main\views.py", line 29, in test
    video = VideoFileClip(os.path.join(basePath + ".mp4"))
  File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
    self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
  File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 34, in __init__
    self.fps = infos['video_fps']
KeyError: 'video_fps'
[15/Nov/2019 23:49:43] "POST /test/ HTTP/1.1" 500 80909

There's practically no information about this error or how to solve it that I could find, so any help or insight would be much appreciated.

Here is my Python script (views.py):

import pyodbc, json, pytube
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import parsers
import os
from moviepy.editor import *

@csrf_exempt
def test(request):
if request.method == 'POST':
    filePath = 'C:\\Users\\etsho\\Music\\'

    #retrieve url from app
    body_unicode = request.body.decode('utf-8')
    body = json.loads(body_unicode)
    videoURL = body['url']

    print("before download")

    #download youtube video
    youtube = pytube.YouTube(videoURL)
    videoTitle = youtube.title
    video = youtube.streams.filter(only_audio=True).first()
    freshDownload = video.download(filePath)

    print("after download")

    basePath, extension = os.path.splitext(freshDownload)
    video = VideoFileClip(os.path.join(basePath + ".mp4"))
    video.audio.write_audiofile(os.path.join(basePath + ".mp3"))

    print("video converted")



return HttpResponse("")
furas
  • 134,197
  • 12
  • 106
  • 148
Ethan Shoe
  • 466
  • 1
  • 7
  • 20
  • 1
    if you use `youtube.streams.filter(only_audio=True)` then probably you download only audio, not video - so it can't have `video_fps`. Maybe you should use `AudioFileClip` instead of `VideoFileClip`? – furas Nov 16 '19 at 07:13
  • maybe first you should check downloaded file to see if it is video or audio file. – furas Nov 16 '19 at 07:15
  • @furas I can verify that the files I'm trying to import are .MP4 files, but maybe the `only_audio=True` is doing something to that "video." I'll try to just download a regular video and see what happens – Ethan Shoe Nov 17 '19 at 06:27
  • Yep, that solved it. Somehow, the `only_audio=True` downloads a .MP4, but has some weird property making it unable to be converted. Thanks for your help. – Ethan Shoe Nov 17 '19 at 06:31
  • you add extension `.mp4` to filename but it doesn't mean that file has really format `.mp4`. Maybe it has totally different format - ie. `.opus` or `.webm` ? Why don't you use `freshDownload` ? – furas Nov 17 '19 at 12:48

2 Answers2

4

So @furas was on to something. What ended up being the issue was the only_audio=True part of the line. The video still downloaded and the file was still a .MP4 but the video had no picture (as per only_audio) but to me, it just appeared to be a black video. What actually happened was there were no actual frames to the video, hence video_fps being the error name. After removing the only_audio=True input, a normal video was downloaded and then the conversion happened as it should have.

Ethan Shoe
  • 466
  • 1
  • 7
  • 20
0

The error occurs when the video doesn't contain any visual content, ensure you are uploading a video which is not blank.