teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

教えて頂いた修正によるコード変更

2020/04/26 06:46

投稿

tumutumu
tumutumu

スコア13

title CHANGED
File without changes
body CHANGED
@@ -95,4 +95,106 @@
95
95
  python 3.7.4
96
96
  (VScodeの拡張機能を使用しておらず、別々でダウンロードしてpython側を環境変数に入れて使用してます)
97
97
 
98
- その他ライブラリ最新バージョンです。
98
+ その他ライブラリ最新バージョンです。
99
+
100
+ 以下追記になります。
101
+ 先ほどは失礼しました。
102
+
103
+ 教えて頂き有難うございます。
104
+
105
+ ご指摘頂いた部分を含めてコードを以下の様に訂正しました。
106
+ ```
107
+ import re, sys, os, time, subprocess
108
+ from bs4 import BeautifulSoup
109
+ from urllib.request import urlopen, urlretrieve
110
+ import tkinter as tk
111
+ from tkinter import *
112
+ from tkinter import ttk,filedialog,messagebox
113
+ from tkinter.ttk import Combobox
114
+
115
+ #URL
116
+ main_links = [
117
+ "https://www.youtube.com/",
118
+ "https://soundcloud.com/",
119
+ "https://www.nicovideo.jp/"
120
+ ]
121
+ m_window=tk.Tk()
122
+ m_window.title("動画 DLソフト")
123
+ m_window.geometry("640x360")
124
+
125
+ m_frame=ttk.Frame(m_window)
126
+ URL_Label=ttk.Label(m_frame,text="URL")
127
+ URL=str()
128
+ URL_box=ttk.Entry(m_frame,textvariable=URL)
129
+
130
+ #mainframe
131
+ m_frame.grid(column=0,row=0,sticky=tk.NSEW,padx=5,pady=10)
132
+
133
+ def dl_click():
134
+ URL=str(URL_box.get())
135
+ dl_extension=str(file_extension.get())
136
+
137
+ if any(s in URL for s in (main_links)):
138
+ ans = dl_extension
139
+ if "https://www.youtube.com/" in URL and "list=" in URL:
140
+ th_list = str(re.sub("list=","",URL))
141
+ else:
142
+ th_list = URL
143
+ if ans=="mp3":
144
+ cmd = 'youtube-dl -o ./%(playlist)s/%(title)s.%(ext)s -ci --extract-audio --audio-format mp3 --add-metadata ' + th_list
145
+ if ans=="mp4":
146
+ cmd = 'youtube-dl -o ./%(playlist)s/%(title)s.%(ext)s -i -f mp4 --add-metadata ' + th_list
147
+ subprocess.check_call(cmd.split())#ここでエラーがでます
148
+
149
+ sys.exit()
150
+ #widget
151
+ dl_extension=str()
152
+ file_extension=ttk.Combobox(m_frame,textvariable=dl_extension,width=10)
153
+ file_extension["values"]=["mp4","mp3"]
154
+ dl_button=ttk.Button(m_frame,text="ダウンロードする",command=dl_click)
155
+
156
+ #widget_position
157
+ URL_Label.grid(column=0,row=0,pady=10)
158
+ URL_box.grid(column=1,row=1,sticky=tk.EW,padx=5)
159
+
160
+ file_extension.grid(column=3,row=2,sticky=tk.EW,padx=5)
161
+
162
+ m_window.columnconfigure(0,weight=1)
163
+ m_window.rowconfigure(0,weight=1)
164
+
165
+ m_frame.columnconfigure(1,weight=1)
166
+
167
+ dl_button.grid(column=2,row=1)
168
+
169
+ m_window.mainloop()
170
+ ```
171
+ そうしますとウィンドウが開き→URLを入力、拡張子を選択→ダウンロードする
172
+ とクリックして目的の動画がダウンロードできたのですが
173
+ 以下の様にエラーがでてしまいました。
174
+ ```
175
+ Exception in Tkinter callback
176
+ Traceback (most recent call last):
177
+ File "C:\Users\kotar\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py",
178
+ line 1705, in __call__
179
+ return self.func(*args)
180
+ File "dl-movie.py", line 41, in dl_click
181
+ subprocess.check_call(cmd.split())#ここでエラーがでます
182
+ File "C:\Users\kotar\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 347, in check_call
183
+ raise CalledProcessError(retcode, cmd)
184
+ subprocess.CalledProcessError: Command '['youtube-dl', '-o', './%(playlist)s/%(title)s.%(ext)s', '-i', '-f', 'mp4', '--add-metadata', 'ダウンロードしたYouTube動画のURL']' returned non-zero exit status 1.
185
+ Traceback (most recent call last):
186
+ File "dl-movie.py", line 63, in <module>
187
+ m_window.mainloop()
188
+ File "C:\Users\kotar\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py",
189
+ line 1283, in mainloop
190
+ self.tk.mainloop(n)
191
+ ```
192
+ 原因が subprocess.check_call(cmd.split())であり、変数cmdの中に入れた文字列に問題があるだろうとは思っているのですが対処法がわかりません。
193
+
194
+ print(cmd.split())で確認してみましたが、特に問題ありませんでした。
195
+ 以下print(cmd.split())で表示されたリストになります。
196
+ ```
197
+ ['youtube-dl', '-o', './%(playlist)s/%(title)s.%(ext)s', '-i', '-f', 'mp4', '--add-metadata', 'ダウンロードした動画のURL']
198
+ ```
199
+ cmdの型宣言も同じように確認してみましたが間違いが見つけられません。
200
+ どうかよろしくお願いします。