抓YT音樂影片且轉成mp3 (Python) 基本是參考[ccs96307大所寫的:Python 當中使用pytube 下載Youtube 的影片](https://clay-atlas.co.
Published
LinkedwithGitHub
Like3
Bookmark
Subscribe
#抓YT音樂影片且轉成mp3(Python)
基本是參考[ccs96307大所寫的:Python當中使用pytube下載Youtube的影片](https://clay-atlas.com/blog/2019/11/08/python-chinese-packages-pytube-moviepy-download-youtube-convert/)
不過當中遇到一些問題,紀錄如何解決。
環境:Ubuntu18.04
使用pytube+moviepy
先安裝
```
sudopip3installpytube3
sudopip3installmoviepy
```
要做的事情:
1.抓YT影片
2.mp4轉檔mp3
##1.抓YT影片
```python=
#-*-coding:utf-8-*-
frompytubeimportYouTube
defprogress(chunk,file_handle,bytes_remaining):
contentSize=video.filesize
size=contentSize-bytes_remaining
print('\r'+'[Downloadprogress]:%.2f%%;\n'%(''*int(size*20/contentSize),''*(20-int(size*20/contentSize)),float(size/contentSize*100)),end='')
url='https://www.youtube.com/watch?v=9Sc-ir2UwGU'
yt=YouTube(url,on_progress_callback=progress)
video=yt.streams.first()
video.download()
```
url內就放你要抓的影片網址progress內只是顯示進度條,要簡單的話三行就能搞定
```python=
#-*-coding:utf-8-*-
frompytubeimportYouTube
url='https://www.youtube.com/watch?v=JwBXgJeqeOs'
YouTube(url).streams.first().download()
```
-配合txt存網址列表可一次抓多首
-可搭配音樂清單一次全抓
###遇到問題
```
Traceback(mostrecentcalllast):
File"/home/brad/.local/lib/python3.6/site-packages/pytube/extract.py",line297,inapply_descrambler
forformat_iteminformats
File"/home/brad/.local/lib/python3.6/site-packages/pytube/extract.py",line297,in
forformat_iteminformats
KeyError:'url'
Duringhandlingoftheaboveexception,anotherexceptionoccurred:
Traceback(mostrecentcalllast):
File"DlYT2mp3.py",line12,in
yt=YouTube(url,on_progress_callback=progress)
File"/home/brad/.local/lib/python3.6/site-packages/pytube/__main__.py",line92,in__init__
self.descramble()
File"/home/brad/.local/lib/python3.6/site-packages/pytube/__main__.py",line132,indescramble
apply_descrambler(self.player_config_args,fmt)
File"/home/brad/.local/lib/python3.6/site-packages/pytube/extract.py",line301,inapply_descrambler
parse_qs(formats[i]["cipher"])fori,datainenumerate(formats)
File"/home/brad/.local/lib/python3.6/site-packages/pytube/extract.py",line301,in
parse_qs(formats[i]["cipher"])fori,datainenumerate(formats)
KeyError:'cipher'
```
###如何解決?
爬文發現不少人有相同問題且有人給出解法
![](https://i.imgur.com/TMnwDXY.png)
簡單來說就是先看你的pytube裝在哪?去資料夾內打開extract.py檔找到函式apply_descrambler的301行,然後把
```python=
parse_qs(formats[i]["cipher"])fori,datainenumerate(formats)
```
改成
```python=
parse_qs(formats[i]["signatureCipher"])fori,datainenumerate(formats)
```
-c記得改大寫
###詳細步驟
1.輸入pip3showpytube3
![](https://i.imgur.com/KNxYSO7.png)
看location位置在哪,進該目錄
![](https://i.imgur.com/iuCfwac.png)
看到pytube資料夾,進去
![](https://i.imgur.com/7YsLUx6.png)
發現extract.py用你的IDE打開他,找到301行改掉
![](https://i.imgur.com/vWwLyBm.png)
解決
---
##2.mp4轉檔mp3
```python=
#-*-coding:utf-8-*-
frommoviepy.editorimport*
filename='Kygo-Firestone.mp4'
targetname='Kygo-Firestone.mp3'
video=VideoFileClip(filename)
video.audio.write_audiofile(targetname)
```
###遇到問題
```
Traceback(mostrecentcalllast):
File"mp4tomp3.py",line2,in
frommoviepy.editorimport*
File"/usr/local/lib/python3.6/dist-packages/moviepy/editor.py",line36,in
from.video.io.VideoFileClipimportVideoFileClip
File"/usr/local/lib/python3.6/dist-packages/moviepy/video/io/VideoFileClip.py",line3,in
frommoviepy.audio.io.AudioFileClipimportAudioFileClip
File"/usr/local/lib/python3.6/dist-packages/moviepy/audio/io/AudioFileClip.py",line3,in
frommoviepy.audio.AudioClipimportAudioClip
File"/usr/local/lib/python3.6/dist-packages/moviepy/audio/AudioClip.py",line7,in
frommoviepy.audio.io.ffmpeg_audiowriterimportffmpeg_audiowrite
File"/usr/local/lib/python3.6/dist-packages/moviepy/audio/io/ffmpeg_audiowriter.py",line7,in
frommoviepy.configimportget_setting
File"/usr/local/lib/python3.6/dist-packages/moviepy/config.py",line36,in
FFMPEG_BINARY=get_exe()
File"/usr/local/lib/python3.6/dist-packages/imageio/plugins/ffmpeg.py",line49,inget_exe
returnimageio_ffmpeg.get_ffmpeg_exe()
File"/usr/local/lib/python3.6/dist-packages/imageio_ffmpeg/_utils.py",line50,inget_ffmpeg_exe
"Noffmpegexecouldbefound.Installffmpegonyoursystem,"
RuntimeError:Noffmpegexecouldbefound.Installffmpegonyoursystem,orsettheIMAGEIO_FFMPEG_EXEenvironmentvariable.
```
找不到ffmpeg.exe?那就裝吧
###如何解決?
```
sudoaptupdate
sudoaptinstallffmpeg
```
解決
---
3
×
Signin
Email
Password
Forgotpassword
or
Byclickingbelow,youagreetoourtermsofservice.
SigninviaFacebook
SigninviaTwitter
SigninviaGitHub
SigninviaDropbox
SigninviaGoogle
NewtoHackMD?Signup