質問編集履歴

1

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

2020/04/26 06:46

投稿

tumutumu
tumutumu

スコア13

test CHANGED
File without changes
test CHANGED
@@ -193,3 +193,207 @@
193
193
 
194
194
 
195
195
  その他ライブラリ最新バージョンです。
196
+
197
+
198
+
199
+ 以下追記になります。
200
+
201
+ 先ほどは失礼しました。
202
+
203
+
204
+
205
+ 教えて頂き有難うございます。
206
+
207
+
208
+
209
+ ご指摘頂いた部分を含めてコードを以下の様に訂正しました。
210
+
211
+ ```
212
+
213
+ import re, sys, os, time, subprocess
214
+
215
+ from bs4 import BeautifulSoup
216
+
217
+ from urllib.request import urlopen, urlretrieve
218
+
219
+ import tkinter as tk
220
+
221
+ from tkinter import *
222
+
223
+ from tkinter import ttk,filedialog,messagebox
224
+
225
+ from tkinter.ttk import Combobox
226
+
227
+
228
+
229
+ #URL
230
+
231
+ main_links = [
232
+
233
+ "https://www.youtube.com/",
234
+
235
+ "https://soundcloud.com/",
236
+
237
+ "https://www.nicovideo.jp/"
238
+
239
+ ]
240
+
241
+ m_window=tk.Tk()
242
+
243
+ m_window.title("動画 DLソフト")
244
+
245
+ m_window.geometry("640x360")
246
+
247
+
248
+
249
+ m_frame=ttk.Frame(m_window)
250
+
251
+ URL_Label=ttk.Label(m_frame,text="URL")
252
+
253
+ URL=str()
254
+
255
+ URL_box=ttk.Entry(m_frame,textvariable=URL)
256
+
257
+
258
+
259
+ #mainframe
260
+
261
+ m_frame.grid(column=0,row=0,sticky=tk.NSEW,padx=5,pady=10)
262
+
263
+
264
+
265
+ def dl_click():
266
+
267
+ URL=str(URL_box.get())
268
+
269
+ dl_extension=str(file_extension.get())
270
+
271
+
272
+
273
+ if any(s in URL for s in (main_links)):
274
+
275
+ ans = dl_extension
276
+
277
+ if "https://www.youtube.com/" in URL and "list=" in URL:
278
+
279
+ th_list = str(re.sub("list=","",URL))
280
+
281
+ else:
282
+
283
+ th_list = URL
284
+
285
+ if ans=="mp3":
286
+
287
+ cmd = 'youtube-dl -o ./%(playlist)s/%(title)s.%(ext)s -ci --extract-audio --audio-format mp3 --add-metadata ' + th_list
288
+
289
+ if ans=="mp4":
290
+
291
+ cmd = 'youtube-dl -o ./%(playlist)s/%(title)s.%(ext)s -i -f mp4 --add-metadata ' + th_list
292
+
293
+ subprocess.check_call(cmd.split())#ここでエラーがでます
294
+
295
+
296
+
297
+ sys.exit()
298
+
299
+ #widget
300
+
301
+ dl_extension=str()
302
+
303
+ file_extension=ttk.Combobox(m_frame,textvariable=dl_extension,width=10)
304
+
305
+ file_extension["values"]=["mp4","mp3"]
306
+
307
+ dl_button=ttk.Button(m_frame,text="ダウンロードする",command=dl_click)
308
+
309
+
310
+
311
+ #widget_position
312
+
313
+ URL_Label.grid(column=0,row=0,pady=10)
314
+
315
+ URL_box.grid(column=1,row=1,sticky=tk.EW,padx=5)
316
+
317
+
318
+
319
+ file_extension.grid(column=3,row=2,sticky=tk.EW,padx=5)
320
+
321
+
322
+
323
+ m_window.columnconfigure(0,weight=1)
324
+
325
+ m_window.rowconfigure(0,weight=1)
326
+
327
+
328
+
329
+ m_frame.columnconfigure(1,weight=1)
330
+
331
+
332
+
333
+ dl_button.grid(column=2,row=1)
334
+
335
+
336
+
337
+ m_window.mainloop()
338
+
339
+ ```
340
+
341
+ そうしますとウィンドウが開き→URLを入力、拡張子を選択→ダウンロードする
342
+
343
+ とクリックして目的の動画がダウンロードできたのですが
344
+
345
+ 以下の様にエラーがでてしまいました。
346
+
347
+ ```
348
+
349
+ Exception in Tkinter callback
350
+
351
+ Traceback (most recent call last):
352
+
353
+ File "C:\Users\kotar\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py",
354
+
355
+ line 1705, in __call__
356
+
357
+ return self.func(*args)
358
+
359
+ File "dl-movie.py", line 41, in dl_click
360
+
361
+ subprocess.check_call(cmd.split())#ここでエラーがでます
362
+
363
+ File "C:\Users\kotar\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 347, in check_call
364
+
365
+ raise CalledProcessError(retcode, cmd)
366
+
367
+ 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.
368
+
369
+ Traceback (most recent call last):
370
+
371
+ File "dl-movie.py", line 63, in <module>
372
+
373
+ m_window.mainloop()
374
+
375
+ File "C:\Users\kotar\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py",
376
+
377
+ line 1283, in mainloop
378
+
379
+ self.tk.mainloop(n)
380
+
381
+ ```
382
+
383
+ 原因が subprocess.check_call(cmd.split())であり、変数cmdの中に入れた文字列に問題があるだろうとは思っているのですが対処法がわかりません。
384
+
385
+
386
+
387
+ print(cmd.split())で確認してみましたが、特に問題ありませんでした。
388
+
389
+ 以下print(cmd.split())で表示されたリストになります。
390
+
391
+ ```
392
+
393
+ ['youtube-dl', '-o', './%(playlist)s/%(title)s.%(ext)s', '-i', '-f', 'mp4', '--add-metadata', 'ダウンロードした動画のURL']
394
+
395
+ ```
396
+
397
+ cmdの型宣言も同じように確認してみましたが間違いが見つけられません。
398
+
399
+ どうかよろしくお願いします。