21
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iPhoneでyoutubeをダウンロードするアプリ作ってみた【pythonista3】

Last updated at Posted at 2018-10-20

pythonista3というiPhone上でpythonができるアプリがあります。
Pythonista3:https://itunes.apple.com/jp/app/pythonista-3/id1085978097?mt=8
エディタとしても優秀でなぜappleが許可したかわからないすごい代物です。
今回はこのpythonista単体でyoutubeからmp4とm4aをダウンロードするアプリを作ってみたいと思います。

環境

pythonista3 == 3.2
のみ

StaShとyoutube_dlのダウンロード

このアプリではyoutube_dlというものをダウンロードするためにpipコマンドを使いますが、pythonista3を入れたばかりでは使うことができません。
そこでStashというシェルを追加します。
名前はPythoni__sta Sh__ellを略したそうです。
まずはpythonistaのコンソールで以下を入力します。

import requests as r; exec(r.get('http://bit.ly/get-stash').text)

するとlaunch_stash.pyが作成されます。

image.png
そして右上のRUNボタン▶を押すとシェルが開始されます。
image.png

ここでpip install youtube_dlと打つとモジュールにyoutube_dlのダウンロードが開始されます。

スクリプト作成

アプリのプログラムを作成します。
ここではファイル名をyoutube_dler.py、UIファイル名をyoutube_dler.pyuiとします。

youtube_dler.py
import youtube_dl,os
import ui


curdir = os.getcwd() + '/'

def dl(sender, path, format):
	global curdir
	if not os.path.isdir(curdir + path):
		os.mkdir(curdir + path)
	os.chdir(curdir + path)
	urlform = sender.superview['url']
	url = urlform.text
	if format == 'm4a' :
		dlformat = 'bestaudio[ext=m4a]'
	elif format == 'mp4' :
		dlformat = 'bestvideo[ext=mp4]'
	ydl_opts = {'format':dlformat}
	with youtube_dl.YoutubeDL(ydl_opts) as ydl:
		ydl.download([url])

def music_dl(sender):
	dl(sender, 'music', 'm4a')

def movie_dl(sender):
	dl(sender, 'video', 'mp4')

v = ui.load_view('youtube_dler.pyui')
v.present('sheet')

つぎはUIを作成します。
UIは基本的に自由です。もっとかっこよくしてください。
image.png
ただしURLフォームのnameをurl、2つのボタンのactionをそれぞれmusic_dl,movie_dlにする必要があります。
image.png
image.png
image.png

使い方

URLフォームにyoutubeのURLをいれてボタンを押すと、m4aはmusic、mp4はvideoファイルに保存されます。

参考

iPhone単体で動画をダウンロードする【pythonista】
https://qiita.com/0x0/items/91fef12fa8e231bc49e9
Pythonista 3 にStaSh をインストールする。
https://qiita.com/maboy/items/cef5dee13d5b2e9ac843
iPhoneでiPhoneアプリを作ろう
https://qiita.com/ttsutchi/items/34fb3395f085e5e11f6e

21
36
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?