首页 > 解决方案 > 每当尝试使用以下代码下载视频时文件损坏

问题描述

此函数接受 URL 、目标和文件名,并很好地下载文件。但是,当我使用 Google Photos API 尝试视频时,视频文件会出现此错误:

OSError: MoviePy error: failed to read the duration of file .\ImageBin\yolo.mp4.

这是下载部分的代码:

def download_file(url: str, destination_folder: str, file_name: str):
    response = requests.get(url)
    if response.status_code == 200:
        with open(os.path.join(destination_folder, file_name), 'wb') as f:
            f.write(response.content)

编辑:我在运行时遇到的所有错误:

  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 286, in ffmpeg_parse_infos
    match = re.findall("([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])", line)[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/defaultuser100000/Google Meets Intagram/Downloading_random_image_from_Google_Photos.py", line 122, in <module>
    upload_video_to_instagram(r'.\ImageBin\yolo.mp4', cap)
  File "C:\Users\defaultuser100000\Google Meets Intagram\Uploading_Photo_to_Instagram.py", line 14, in upload_video_to_instagram
    bot.upload_video(video, caption=caption)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\instabot\bot\bot.py", line 822, in upload_video
    return upload_video(self, video, caption, thumbnail, options)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\instabot\bot\bot_video.py", line 20, in upload_video
    video, caption=caption, thumbnail=thumbnail, options=options
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\instabot\api\api.py", line 865, in upload_video
    return upload_video(self, video, caption, upload_id, thumbnail, options)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\instabot\api\api_video.py", line 110, in upload_video
    video, thumbnail, width, height, duration = resize_video(video, thumbnail)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\instabot\api\api_video.py", line 248, in resize_video
    vid = mp.VideoFileClip(fname)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 91, in __init__
    fps_source=fps_source)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 36, in __init__
    fps_source)
  File "C:\Users\Aryan Garg\PycharmProjects\Google Meets Intagram\venv\lib\site-packages\moviepy\video\io\ffmpeg_reader.py", line 291, in ffmpeg_parse_infos
    filename, infos))
OSError: MoviePy error: failed to read the duration of file .\ImageBin\yolo.mp4.
Here are the file infos returned by ffmpeg:

ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers

  built with gcc 9.2.1 (GCC) 20200122

  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --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 --enable-libopenmpt

  libavutil      56. 31.100 / 56. 31.100

  libavcodec     58. 54.100 / 58. 54.100

  libavformat    58. 29.100 / 58. 29.100

  libavdevice    58.  8.100 / 58.  8.100

  libavfilter     7. 57.100 /  7. 57.100

  libswscale      5.  5.100 /  5.  5.100

  libswresample   3.  5.100 /  3.  5.100

  libpostproc    55.  5.100 / 55.  5.100

Input #0, jpeg_pipe, from '.\ImageBin\yolo.mp4':

  Duration: N/A, bitrate: N/A

    Stream #0:0: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 262x512 [SAR 1:1 DAR 131:256], 25 tbr, 25 tbn, 25 tbc

At least one output file must be specified

调用函数:

holy_image = choice(Base_URL_list)

尽管它的名字 Holy_image 实际上只是一个 URL 列表,可以是视频和图像

标签: pythonpython-3.xdownload

解决方案


推荐阅读