MoviePy ใน Django: ไม่สามารถอ่านระยะเวลาของไฟล์ได้

ฉันกำลังพยายามส่งข้อมูลวิดีโอที่ดึงมาจาก ModelsForm ไปยังวิธี moviepy แต่ฟังก์ชัน VideoFileClip() ไม่สามารถอ่านระยะเวลาของไฟล์ได้

ฉันได้ตรวจสอบข้อมูลที่ดึงเข้ามาแล้วและเป็นชื่อที่ถูกต้อง ขณะนี้ปัญหาอาจเป็นได้ว่าฉันกำลังส่งผ่านเป็นสตริง อย่างไรก็ตาม หากไม่มีสิ่งนี้ จะส่งกลับข้อผิดพลาด: วัตถุ 'Splitter' ไม่มีแอตทริบิวต์ 'endswith'

โปรดดูข้อผิดพลาดและรหัสของฉันด้านล่าง ขอบคุณสำหรับความช่วยเหลือของคุณ!

โค้ด Models.py

ข้อความแสดงข้อผิดพลาด

OSError at /highlights/
MoviePy error: failed to read the duration of file <highlights.splitter.Splitter object at 0x000001A82CE553D0>.
Here are the file infos returned by ffmpeg:

ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181017
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
<highlights.splitter.Splitter object at 0x000001A82CE553D0>: Invalid argument
Request Method: POST
Request URL:    http://127.0.0.1:8000/highlights/
Django Version: 3.0.1
Exception Type: OSError
Exception Value:    
MoviePy error: failed to read the duration of file <highlights.splitter.Splitter object at 0x000001A82CE553D0>.
Here are the file infos returned by ffmpeg:

ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181017
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
<highlights.splitter.Splitter object at 0x000001A82CE553D0>: Invalid argument
Exception Location: C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py in ffmpeg_parse_infos, line 289
Python Executable:  C:\Program Files\Python38\python.exe
Python Version: 3.8.0
Python Path:    
['C:\\Users\\User\\Documents\\Codes\\highlightreel',
 'C:\\Program Files\\Python38\\python38.zip',
 'C:\\Program Files\\Python38\\DLLs',
 'C:\\Program Files\\Python38\\lib',
 'C:\\Program Files\\Python38',
 'C:\\Program Files\\Python38\\lib\\site-packages',
 'C:\\Program Files\\Python38\\lib\\site-packages\\IPython\\extensions']
Server time:    Wed, 1 Apr 2020 20:04:23 +0000

ย้อนกลับ

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/highlights/

Django Version: 3.0.1
Python Version: 3.8.0
Installed Applications:
['highlights.apps.HighlightsConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 285, in ffmpeg_parse_infos
    line = [l for l in lines if keyword in l][index]

During handling of the above exception (list index out of range), another exception occurred:
  File "C:\Program Files\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Program Files\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\Documents\Codes\highlightreel\highlights\views.py", line 17, in video_form_upload
    Splitter(val)
  File "C:\Users\User\Documents\Codes\highlightreel\highlights\splitter.py", line 18, in __init__
    clip = mp.VideoFileClip(str(file))
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
    self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 35, in __init__
    infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 289, in ffmpeg_parse_infos
    raise IOError(("MoviePy error: failed to read the duration of file %s.\n"

Exception Type: OSError at /highlights/
Exception Value: MoviePy error: failed to read the duration of file <highlights.splitter.Splitter object at 0x000001A82CE553D0>.
Here are the file infos returned by ffmpeg:

ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181017
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
<highlights.splitter.Splitter object at 0x000001A82CE553D0>: Invalid argument

อัปเดต

โค้ด

ข้อผิดพลาด

AttributeError at /highlights/
'Splitter' object has no attribute 'endswith'
Request Method: POST
Request URL:    http://127.0.0.1:8000/highlights/
Django Version: 3.0.1
Exception Type: AttributeError
Exception Value:    
'Splitter' object has no attribute 'endswith'
Exception Location: C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py in ffmpeg_parse_infos, line 244
Python Executable:  C:\Program Files\Python38\python.exe
Python Version: 3.8.0
Python Path:    
['C:\\Users\\User\\Documents\\Codes\\highlightreel',
 'C:\\Program Files\\Python38\\python38.zip',
 'C:\\Program Files\\Python38\\DLLs',
 'C:\\Program Files\\Python38\\lib',
 'C:\\Program Files\\Python38',
 'C:\\Program Files\\Python38\\lib\\site-packages',
 'C:\\Program Files\\Python38\\lib\\site-packages\\IPython\\extensions']
Server time:    Thu, 2 Apr 2020 00:57:52 +0000

ย้อนกลับ

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/highlights/

Django Version: 3.0.1
Python Version: 3.8.0
Installed Applications:
['highlights.apps.HighlightsConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Program Files\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\Documents\Codes\highlightreel\highlights\views.py", line 16, in video_form_upload
    splitter = Splitter(instance.video.path)
  File "C:\Users\User\Documents\Codes\highlightreel\highlights\splitter.py", line 20, in __init__
    clip = mp.VideoFileClip(file)
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
    self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 35, in __init__
    infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
  File "C:\Program Files\Python38\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 244, in ffmpeg_parse_infos
    is_GIF = filename.endswith('.gif')

Exception Type: AttributeError at /highlights/
Exception Value: 'Splitter' object has no attribute 'endswith'

person Silreth    schedule 01.04.2020    source แหล่งที่มา
comment
ฉันตรวจสอบซอร์สโค้ดของแพ็คเกจที่คุณใช้แล้ว และการส่งชื่อไฟล์ดูเหมือนจะเป็นวิธีที่ถูกต้องในการเรียก VideoFileClip   -  person Grand Phuba    schedule 01.04.2020


คำตอบ (1)


ดูเหมือนว่าคุณจะส่งเพียงชื่อไฟล์ของวิดีโอที่อัปโหลด ซึ่งคุณควรส่งผ่านเส้นทางทั้งหมดไปยังไฟล์

ลองดำเนินการอย่างใดอย่างหนึ่งต่อไปนี้ แต่ไม่ว่าจะเป็นเช่นไร ดูเหมือนว่าปัญหาจะเกิดขึ้นว่าไลบรารีไม่สามารถโหลดไฟล์ที่ถูกต้องได้ ตรวจสอบให้แน่ใจว่าก่อนอื่นวิดีโอกำลังถูกอัปโหลดในโฟลเดอร์สื่อของคุณ หากคุณสามารถตรวจสอบสิ่งนี้ได้ คุณจะรู้ว่าตอนนี้เป็นเพียงเรื่องของการโหลดเท่านั้น

ตัวเลือกที่ 1 - เปลี่ยนมุมมอง

ในมุมมอง video_form_upload ของคุณ ให้ลองทำดังนี้

def video_form_upload(request):
    if request.method == 'POST':
        form = VideoForm(request.POST, request.FILES)
        if form.is_valid():
             instance = form.save()  # get the saved model
             splitter = Splitter(instance.video.path)  # get the video file from the saved model
             return redirect(`highlights`)
    ...

ตัวเลือก 2 - เปลี่ยนตัวแยก

ในคลาส Splitter ของคุณ บรรทัดที่ 18 ให้ลองเปลี่ยนจาก

clip = mp.VideoFileClip(str(file))

to

clip = mp.VideoFileClip(file.path)
person Grand Phuba    schedule 01.04.2020
comment
นั่นคงจะสมเหตุสมผล! แม้ว่าดูเหมือนจะไม่มีเส้นทางอยู่ก็ตาม: AttributeError ที่ /highlights/ วัตถุ 'Splitter' ไม่มีแอตทริบิวต์ 'เส้นทาง' มีวิธีที่ดีกว่าในการใช้งานวิดีโอที่อัปโหลดหรือไม่ - person Silreth; 02.04.2020
comment
ลองตัวเลือกที่หนึ่งแล้วและผ่านไปอย่างถูกต้อง แต่กลับไปสู่ข้อผิดพลาดด้านระยะเวลา ฉันได้ตรวจสอบแล้วว่าวิดีโอกำลังอัปโหลดไปยังตำแหน่งที่ถูกต้อง ดูเหมือนว่าจะมีปัญหาในการโหลด:/ มีแนวคิดอื่นๆ อีกไหม - person Silreth; 02.04.2020
comment
คุณสามารถยืนยันได้ว่าคุณกำลังส่งผ่านเส้นทางสัมบูรณ์ของไฟล์หรือไม่ - person Grand Phuba; 02.04.2020
comment
จริงๆ แล้ว ฉันแสดงผลสิ่งที่มันผ่านไปและมันกำลังส่งผ่าน: ‹highlights.splitter.Splitter object at 0x000002313C68A490› แทนที่จะเป็นเส้นทาง - person Silreth; 02.04.2020
comment
คุณสามารถส่งภาพหน้าจอของรหัสปัจจุบันของคุณพร้อมกับคำสั่ง print/log และผลลัพธ์ได้หรือไม่ - person Grand Phuba; 02.04.2020