Python:将mp4转换为mp3或将youtube视频下载为mp3的最佳方法是什么?

时间:2019-05-24 20:03:49

标签: python audio youtube

我正在尝试通过python从youtube下载视频。我已经成功下载了mp4文件,但无法将mp4转换为mp3。我曾尝试使用ffmpeg,但从未成功。

我尝试了ffmpeg,youtube-dl和vlc。我正在尝试不使用VLC,因为以后无法在pi上使用它。

   import urllib.request
   import urllib.parse
   import pytube
   import re
   import os
   from moviepy.editor import *



   #Takes users input and searches youtube for request
   query_string = urllib.parse.urlencode({"search_query" : input('Search: 
   ')})
   html_content = 
   urllib.request.urlopen("http://www.youtube.com/results?" + 
   query_string)
   search_results= re.findall(r'href=\"\/watch\?v=(. 
   {11})',html_content.read().decode())
   url=("https://www.youtube.com/watch?v=" + search_results[0])
   #print("https://www.youtube.com/watch?v=" + search_results[0])
   print (url) #prints url

   #returns url for requested search
   video_url = 'https://www.youtube.com/watch?v=' + search_results[0]
   #print(video_url)
   youtube1 = pytube.YouTube(video_url)
   video = youtube1.streams.first()

  #video.download('\\Users\\dseni\\PycharmProjects\\SmartMirror1\\Music')
   video.download('\\Users\dseni\Music\Py-Music\\Music')
   print(video_url)
   print(youtube1)
   print()
   #path = 'C:\\Users\\dsenic\PycharmProjects\\SmartMirror1\\Music\\'
   path = 'C:\\Users\\dsenic\Music\\Py-Music\\Music'
   files = []

   #Is suppose to change all files in mp4 format to mp3
   video_mp4 = VideoFileClip(os.path.join("\\Users\\dseni\\Music\\Py- 
   Music\\Music"))

   video_mp4.audio.write_audiofile(os.path.join('\\User\\dseni\\Music\\
   Py-Music'))
   print('Complete')

   This is the error I get:

   Traceback (most recent call last):
     File "C:\Users\dseni\PycharmProjects\SmartMirror1\venv\lib\site- 
     packages\moviepy\video\io\ffmpeg_reader.py", line 287, in 
     ffmpeg_parse_infos
       line = [l for l in lines if keyword in l][index]
   IndexError: list index out of range

   During handling of the above exception, another exception occurred:

   Traceback (most recent call last):
     File 
     "C:/Users/dseni/PycharmProjects/SmartMirror1/Youtubemp3_Test1.py", 
      line 46, in <module>
        video_mp4 = 
        VideoFileClip(os.path.join("\\Users\\dseni\\Music\\Py- 
        Music\\Music"))
     File "C:\Users\dseni\PycharmProjects\SmartMirror1\venv\lib\site- 
     packages\moviepy\video\io\VideoFileClip.py", line 91, in __init__
        fps_source=fps_source)
     File "C:\Users\dseni\PycharmProjects\SmartMirror1\venv\lib\site- 
     packages\moviepy\video\io\ffmpeg_reader.py", line 33, in __init__
        fps_source)
     File "C:\Users\dseni\PycharmProjects\SmartMirror1\venv\lib\site- 
     packages\moviepy\video\io\ffmpeg_reader.py", line 293, in 
        ffmpeg_parse_infos
        filename, infos))
   OSError: MoviePy error: failed to read the duration of file 
   \Users\dseni\Music\Py-Music\Music.
   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
    \Users\dseni\Music\Py-Music\Music: Permission denied

预期结果是:

A)用户请求某种类型的歌曲,该程序将搜索youtube,找到链接,下载与该链接关联的文件并播放音频。

B)用户将下载多首歌曲,从而创建一个库。之后,用户将能够搜索他们的音乐库,选择特定的歌曲,然后按选择播放。

我希望能够暂停甚至播放整个文件(例如播放列表),但是现在我只希望播放实际的音频文件(mp3)。为此,我需要将mp4转换为mp3或将youtube文件下载为mp3。这是我的第一篇文章,所以请告知我是否需要提供其他信息。对不起,代码字符串和错误。

0 个答案:

没有答案