libmtpやってみる -2-

 やっと動いたので、続き。

 mtp-filetree:

 MTPデバイス上のファイルとフォルダをツリー形式で表示。

 各ファイル/フォルダにはID (ハンドル) が付与される。

kinneko@pi3b:~ $ mtp-filetree
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Attempting to connect device(s)
Android device detected, assigning default bug flags
Error 1: Get Storage information failed.
Device:
LIBMTP_Get_Storage(): Resource temporarily unavailable
OK.

 あかん... これって実装されてないのかも...

 pixel6aのPTPモードでもダメなの同じだわ...

kinneko@pi3b:~ $ sudo mtp-filetree
Device 0 (VID=18d1 and PID=4ee5) is a Google Inc Nexus/Pixel (PTP).
Attempting to connect device(s)
PTP_ERROR_IO: failed to open session, trying again after resetting USB interface
LIBMTP libusb: Attempt to reset device
LIBMTP PANIC: failed to open session on second attempt
Unable to open raw device 0
OK.

 これは、USBの設定でPTPになっていないとき。

 たぶん、LINKLETもこれと同じだな。

 LINKLETは都合によりadb接続を開放していない。つまりGUIでの操作はできないのであった。

kinneko@pi3b:~ $ sudo mtp-filetree
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Attempting to connect device(s)
Android device detected, assigning default bug flags
Error 1: Get Storage information failed.
Device:
LIBMTP_Get_Storage(): Resource temporarily unavailable
OK.

 特定のサービス入れていないとMTP使えないのだった... Web画面からリモートでできるのだけど、グループの管理者権限でないと入れられない。入れてもらう。しかし、ユーザー権限では入っていることが画面からはわからない。ダメじゃんw

 MTPには応答するようになったので、アプリがインストールされて、USBの設定がMTPになったのはわかった。

kinneko@pi3b:~ $ sudo mtp-filetree
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Attempting to connect device(s)
Android device detected, assigning default bug flags
Device:
Storage: SanDisk製SDカード
99 DCIM
100 Pictures
Storage: 内部共有ストレージ
6 Pictures
9 DCIM
OK.

 撮影した動画データは以下にあるそうな。

 THINKLET LC01/SanDisk製SDカード/DCIM/

 mtp-files:

 MTPデバイス上のファイルとフォルダを、より詳細な情報(サイズ、日付など)とともにリスト表示

 なんかベタっと出てくるのいやね。日付とかのデータは出ない。めんどくさい。

kinneko@pi3b:~ $ mtp-files
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
mtp-files: Successfully connected
Android device detected, assigning default bug flags
Listing File Information on Device with name:
File ID: 112
Filename: VID_20250702_182933.mp4
File size 154752518 (0x0000000009395606) bytes
Parent ID: 99
Storage ID: 0xF5030001
Filetype: MPEG video stream
File ID: 113
Filename: VID_20250702_183215.mp4
File size 5022762 (0x00000000004CA42A) bytes
Parent ID: 99
Storage ID: 0xF5030001
Filetype: MPEG video stream
File ID: 106
Filename: PIC_20250702_182937.jpg
File size 1132163 (0x0000000000114683) bytes
Parent ID: 100
Storage ID: 0xF5030001
Filetype: JPEG file
File ID: 107
Filename: PIC_20250702_182944.jpg
File size 1063254 (0x0000000000103956) bytes
Parent ID: 100
Storage ID: 0xF5030001
Filetype: JPEG file
File ID: 108
Filename: PIC_20250702_182955.jpg
File size 1135111 (0x0000000000115207) bytes
Parent ID: 100
Storage ID: 0xF5030001
Filetype: JPEG file
File ID: 109
Filename: PIC_20250702_183031.jpg
File size 1137191 (0x0000000000115A27) bytes
Parent ID: 100
Storage ID: 0xF5030001
Filetype: JPEG file
File ID: 110
Filename: PIC_20250702_183050.jpg
File size 918349 (0x00000000000E034D) bytes
Parent ID: 100
Storage ID: 0xF5030001
Filetype: JPEG file
File ID: 111
Filename: PIC_20250702_183149.jpg
File size 930388 (0x00000000000E3254) bytes
Parent ID: 100
Storage ID: 0xF5030001
Filetype: JPEG file
OK.
mtp-getfile:
MTPデバイスからファイルをPCにコピー
mtp-getfile <デバイス上のファイルID> <PC上の保存先パス>

 ファイル名じゃなくて、IDで操作するの面倒ね... これでもいいらしい。

 mtp-connect --getfile <デバイス上のファイル名> <PC上の保存先パス>

kinneko@pi3b:~ $ mtp-getfile 111 /home/kinneko/
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Getting file/track 111 to local file /home/kinneko/

 Error getting file from MTP device.

 なるほど、保存するファイル名を決めてやらないとダメらしい。

kinneko@pi3b:~ $ mtp-getfile 111 /home/kinneko/test.jpg
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Getting file/track 111 to local file /home/kinneko/test.jpg
Progress: 930404 of 930404 (100%)

 こっちは落とすファイル名はつけたらダメらしい。

kinneko@pi3b:~ $ mtp-connect --getfile PIC_20250702_183050.jpg /home/kinneko/test.jpg
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Get file PIC_20250702_183050.jpg
Getting PIC_20250702_183050.jpg to /home/kinneko/test.jpg
Unknown options: /home/kinneko/test.jpg

 え? パスもだめ?

kinneko@pi3b:~ $ mtp-connect --getfile PIC_20250702_183050.jpg /home/kinneko/
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Get file PIC_20250702_183050.jpg
Getting PIC_20250702_183050.jpg to /home/kinneko/

 Error getting file from MTP device.

 Error 1: LIBMTP_Get_File_To_File(): Could not create file.

 Unknown options: /home/kinneko/

 第二引数に何を指定しても「Unknown options」が返る感じ。

 2019年に報告されている。応答はないね。

 example mtp-connect getfile doesn't take two arguments · Issue #26 · libmtp/libmtp

https://github.com/libmtp/libmtp/issues/26

 mtp-connectは保守されていないのかもしれないな... やめとくか。

 mtp-sendfile:

 PCからMTPデバイスにファイルをコピー

 mtp-sendfile <PC上のファイルパス> <デバイス上の保存先パス>

 mtp-connect --sendfile <PC上のファイルパス> <デバイス上のファイル名>

kinneko@pi3b:~ $ mtp-sendfile ./desktop_480x800.jpg "THINKLET LC01/SanDisk製SDカード/DCIM/"
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Sending ./desktop_480x800.jpg to THINKLET LC01/SanDisk製SDカード/DCIM/
type: jpg, 14
Sending file...

 Error sending file.

 Error 2: PTP Layer error 2002: get_suggested_storage_id(): could not get storage id from parent id.

 Error 2: Error 2002: PTP General Error

 Error 2: PTP Layer error 2002: send_file_object_info(): Could not send object info.

 Error 2: Error 2002: PTP General Error

 なんかめんどくさ...

kinneko@pi3b:~ $ mtp-sendfile ./desktop_480x800.jpg "/SanDisk製SDカード/DCIM/"
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Sending ./desktop_480x800.jpg to /SanDisk製SDカード/DCIM/
Parent folder could not be found, skipping

 うーん。

kinneko@pi3b:~ $ mtp-sendfile ./desktop_480x800.jpg "SanDisk製SDカード/DCIM/"
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Sending ./desktop_480x800.jpg to SanDisk製SDカード/DCIM/
type: jpg, 14
Sending file...

 Error sending file.

 Error 2: PTP Layer error 2002: get_suggested_storage_id(): could not get storage id from parent id.

 Error 2: Error 2002: PTP General Error

 Error 2: PTP Layer error 2002: send_file_object_info(): Could not send object info.

 Error 2: Error 2002: PTP General Error

 今回は取得と削除しかしないからいいか...

 たぶん、Storageに日本語が含まれるのを想定していない感じがするな。

 locale的にはUTF-8だし、デフォルトで問題ない気がする。

kinneko@pi3b:~ $ locale
LANG=en_GB.UTF-8
LANGUAGE=
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=

 mtp-connect --sendfileもやってみとくか。ダメだけど...

kinneko@pi3b:~ $ mtp-connect --sendfile ./test.jpg "SanDisk製SDカード/DCIM/test.jpg"
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Send file ./test.jpg
Sending ./test.jpg to SanDisk製SDカード/DCIM/test.jpg
type: jpg, 14
Sending file...

 Error sending file.

 Error 2: PTP Layer error 2002: get_suggested_storage_id(): could not get storage id from parent id.

 Error 2: Error 2002: PTP General Error

 Error 2: PTP Layer error 2002: send_file_object_info(): Could not send object info.

 Error 2: Error 2002: PTP General Error

 Unknown options: SanDisk製SDカード/DCIM/test.jpg

kinneko@pi3b:~ $ mtp-connect --sendfile ./test.jpg "/SanDisk製SDカード/DCIM/test.jpg"
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Send file ./test.jpg
Sending ./test.jpg to /SanDisk製SDカード/DCIM/test.jpg
Parent folder could not be found, skipping
Unknown options: /SanDisk製SDカード/DCIM/test.jpg
kinneko@pi3b:~ $ mtp-connect --sendfile ./test.jpg 99
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Send file ./test.jpg
Sending ./test.jpg to 99
type: jpg, 14
Sending file...

 Error sending file.

 Error 2: PTP Layer error 2002: send_file_object_info(): Could not send object info.

 Error 2: Error 2002: PTP General Error

 Unknown options: 99

 mtp-delfile:

 MTPデバイス上のファイルを削除

 mtp-delfile -n <デバイス上のファイルID>

 mtp-delfile -f <デバイス上のファイル名>

 mtp-connect --delete <デバイス上のファイル名>

kinneko@pi3b:~ $ mtp-delfile -n 106
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Deleting 106
kinneko@pi3b:~ $ mtp-filetree | grep 106

 消えてるね。

kinneko@pi3b:~ $ mtp-delfile -f PIC_20250702_182944.jpg
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Deleting PIC_20250702_182944.jpg
kinneko@pi3b:~ $ mtp-filetree | grep PIC_20250702_182944.jpg

 消えてる。特にパスは出す必要ないのがミソ?

kinneko@pi3b:~ $ mtp-connect --delete PIC_20250702_182955.jpg
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Delete PIC_20250702_182955.jpg
Deleting PIC_20250702_182955.jpg which has item_id:108

 これもいける。

 mtp-newfolder:

 MTPデバイス上に新しいフォルダを作成

 mtp-newfolder <新しいフォルダ名> <親フォルダのID> <ストレージID>

 ※ ルートディレクトリに作成する場合は親フォルダIDを0、ストレージIDも0にする

 mtp-connect --newfolder <新しいフォルダ名> <親フォルダのパス>

 まぁ、rootには作れないよね。

kinneko@pi3b:~ $ mtp-newfolder newfolder 0 0
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Folder creation failed.

 ストレージIDって番号では出ないのだよね。

kinneko@pi3b:~ $ mtp-newfolder newfolder 99 "SanDisk製SDカード"
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Folder creation failed.

 こんなん出たけど。

 Usage: newfolder name

parent = parent folder or 0 to create the new folder in the root dir
storage = storage id or 0 to create the new folder on the primary storage
kinneko@pi3b:~ $ mtp-detect
(snip)
Storage Devices:
StorageID: 0xf5030001
StorageType: 0x0004 removable RAM storage
FilesystemType: 0x0002 generic hierarchical
AccessCapability: 0x0000 read/write
MaxCapacity: 127833178112
FreeSpaceInBytes: 127669567488
FreeSpaceInObjects: 1073741824
StorageDescription: SanDisk製SDカード
VolumeIdentifier:
StorageID: 0x00010001
StorageType: 0x0003 fixed RAM storage
FilesystemType: 0x0002 generic hierarchical
AccessCapability: 0x0000 read/write
MaxCapacity: 55605719040
FreeSpaceInBytes: 53268713472
FreeSpaceInObjects: 1073741824
StorageDescription: 内部共有ストレージ
VolumeIdentifier:
(snip)
kinneko@pi3b:~ $ mtp-newfolder newfolder 99 0xf5030001
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Folder creation failed.
kinneko@pi3b:~ $ mtp-newfolder newfolder 9 0x00010001
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Folder creation failed.

 権限的な問題か?

kinneko@pi3b:~ $ mtp-newfolder newfolder 0 0x00010001
libmtp version: 1.1.20
Device 0 (VID=18d1 and PID=4ee1) is a Google Inc Nexus/Pixel (MTP).
Android device detected, assigning default bug flags
Folder creation failed.

 わからん。ギブ。

 mtp-delfolder:

 あまり使われていない感じ

 mtp-connect --delfolder <削除するフォルダのパス>

 削除できるフォルダを作れないので、これもパスだな...

 というわけで、ファイルはコピれるし消せるけど、フォルダ作ったりファイル置いたりはできないということで。うまくいかない部分は、デバイス側の実装に依存している感じかも。

 100円投げ銭  https://kinneko.booth.pm/items/1963720


オリジナル投稿:
libmtpやってみる -2-|kinneko|pixivFANBOX
https://kinneko.fanbox.cc/posts/10159857