19日に更新してた

アフィリエイトはないよ

Android の SD カードにファイルを入れるのは FTP 使って無線 LAN で入れるのが楽。

今どきは音楽聞くのにサブスクリプションやダウンロードするのが当たり前で、エンコードして移動するとかはあまり流行らないのかもしれませんが、ダウンロードしかないものならともかく、やっぱり現物ないと心配なんですよ。

なので、ファイル移動は必須作業。
手動でシコシコやるのもいい歳になると目が辛いし面倒になってくるので、クライアント書いて ftp でファイル移動するのが好みだったりします。

まず、Android にファイルマネージャーを入れておくと捗ります。
play.google.com

sd カードの path が理解できるようにでてくる ftp サーバー立ち上げてくれるアプリはこれくらいしか見つけられなかったので、とても重宝しております。*1

で、以下は Windows10Home の Jupiter notebook 上で動かしているちょっと使うための FTP クライアント、道具なので細かいところは適当です。

見たとおり、ポート2221で /sdcard/music の下にディレクトリごとに入れてくれるようにしてあります。3-4層くらいまでは試しておりますが、再帰で書いているのでディレクトリ構造が深いと動作するかどうか?メモリーとファイル数次第かと。テキストファイルは上げないようにしています。

tqdm 周りで”将来的にこの書き方はアカン”とうるさいので、警告を切ってあります。

from ftplib import *
import os
import posixpath
from tqdm import tqdm_notebook as tqdm
import warnings
warnings.filterwarnings('ignore')


def dir_upload_to_server(UPLOAD_DIR,url,server_path="/sdcard/music",port=2221,username="", password=""):
    def ftp_contents_upload(UPLOAD_DIR,server_path_inner):
        dir_name = os.path.basename(UPLOAD_DIR)
        try:
            ftp.nlst(dir_name)
        except:
            ftp.mkd(dir_name)
        if not posixpath.basename(server_path_inner) == dir_name:
            server_path_inner = posixpath.join(server_path_inner,dir_name)
        ftp.cwd(dir_name)
        mother_dir_contents_list = os.listdir(UPLOAD_DIR)
        flist = ftp.nlst()
        for upload_contents in tqdm(mother_dir_contents_list, leave=False):
            mother_dir_contents = os.path.join(UPLOAD_DIR,upload_contents)
            if os.path.isfile(mother_dir_contents):
                if upload_contents not in flist:
                    if not upload_contents.endswith('.txt'):
                        with open(mother_dir_contents, 'rb') as fi:
                            ftp.storbinary('STOR ' + upload_contents, fi)
            elif os.path.isdir(mother_dir_contents):
                ftp_contents_upload(mother_dir_contents, posixpath.join(server_path_inner, upload_contents))
                ftp.cwd('..')
                flist = ftp.nlst()

    FTP.encoding = "utf-8"
    ftp = FTP()
    ftp.connect(url,port)
    ftp.set_pasv("true")
    ftp.login(username,password)
    ftp.cwd(server_path)
    ftp_contents_upload(UPLOAD_DIR,server_path)

UPLOAD_DIRs=[r'D:\\radiko',r'D:\NHK2021\中高生の基礎英語inEnglish',r'D:\NHK2021\ラジオ英会話']

URL='192.168.1.3'

for UPLOAD_dir in UPLOAD_DIRs:
    try:
        dir_upload_to_server(UPLOAD_dir,URL)
    except:
        URL=input('ftp ip ?:')
        dir_upload_to_server(UPLOAD_dir,URL)
print("\n更新終了")

令和3年7月12日 追記
そういえばフォーマットは外部ストレージでしてくださいね。

令和4年3月12日 追記2
Windows のミュージックホルダをそのまま "/sdcard/music" に上げる場合は

target_dir=r"C:\\Users\\username\\Music\\"
UPLOAD_DIRs=[os.path.join(target_dir,d) for d in os.listdir(target_dir)]

とか

import glob
UPLOAD_DIRs=glob.glob(r"C:\\Users\\username\\Music\\*")

とやると "/sdcard/music/music" となりません。

令和4年3月25日 追記3: 追記2に追記

音源しか移動しないので忘れてましたが、

server_path="/sdcard/

でもいいはずです。

*1:意味がわからないかもしれませんが、ルートディレクトリがよくわからないディレクトリ構造が出てくるアプリもあるのですよ。