Python Forum
How to get a removable disc type in drive
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get a removable disc type in drive
#1
Question 
I am currently working on a program to automate ripping and backing my collection of CDs, DVDs and Blue-rays. I am currently running into an issue where I need to be able to find the type of disc in the drive but I have been unable to located a library that can do so. Has anymore ran into this issue before and if so what was your solution? The only janky way I'm seeing is by looking at Blue-rays and DVDs file structure. For DVD its TS_Video and or TS_Audio. For Blue-ray its AACS, BDMV and CERTIFICATE and go by that and for CD I have an bash command like abcde and catch if it fails or succeeds . Also if this help's I am writing this under Linux. If you have any suggestions or another way of looking at this please let me know.

Thanks,
Daring_T
Reply
#2
(Feb-06-2024, 05:45 AM)Daring_T Wrote: I am currently running into an issue where I need to be able to find the type of disc in the drive
I'd say first find how to do it with Linux on the command line. Then Python can do the same.
You could perhaps obtain something with pyudev
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Feb-06-2024, 05:45 AM)Daring_T Wrote: I am currently working on a program to automate ripping and backing my collection of CDs, DVDs and Blue-rays. I am currently running into an issue where I need to be able to find the type of disc in the drive but I have been unable to located a library that can do so. Has anymore ran into this issue before and if so what was your solution? The only janky way I'm seeing is by looking at Blue-rays and DVDs file structure. For DVD its TS_Video and or TS_Audio. For Blue-ray its AACS, BDMV and CERTIFICATE and go by that and for CD I have an bash command like abcde and catch if it fails or succeeds . Also if this help's I am writing this under Linux. If you have any suggestions or another way of looking at this please let me know.

Thanks,
Daring_T

It seems like you're on the right track with your approach, but there might be a more straightforward way to determine the type of disc in the drive programmatically, especially under Linux.

One common method is to use the blkid command, which is typically used to locate/block device attributes like UUIDs or filesystem types. When a disc is inserted into the drive, it should register as a block device, and blkid can provide information about it.

$blkid

This command should list all block devices, including optical drives and their respective filesystems. By parsing the output of this command, you can determine the type of disc inserted.

Best Regard
Danish Hafeez | QA Assistant
Gribouillis likes this post
Reply
#4
(Feb-06-2024, 09:01 AM)Gribouillis Wrote:
(Feb-06-2024, 05:45 AM)Daring_T Wrote: I am currently running into an issue where I need to be able to find the type of disc in the drive
I'd say first find how to do it with Linux on the command line. Then Python can do the same.
You could perhaps obtain something with pyudev

So I've tried using cd-tool but it just shows what the drive supports. I've tried blkid and lsblk they only show rom or unknown as the type. I tried using pyudev before I will take a further look at and see if I missed something. I will post if I find a solution.

Thank,
Daring_T
Reply
#5
(Feb-06-2024, 11:15 AM)Danishhafeez Wrote:
(Feb-06-2024, 05:45 AM)Daring_T Wrote: I am currently working on a program to automate ripping and backing my collection of CDs, DVDs and Blue-rays. I am currently running into an issue where I need to be able to find the type of disc in the drive but I have been unable to located a library that can do so. Has anymore ran into this issue before and if so what was your solution? The only janky way I'm seeing is by looking at Blue-rays and DVDs file structure. For DVD its TS_Video and or TS_Audio. For Blue-ray its AACS, BDMV and CERTIFICATE and go by that and for CD I have an bash command like abcde and catch if it fails or succeeds . Also if this help's I am writing this under Linux. If you have any suggestions or another way of looking at this please let me know.

Thanks,
Daring_T

It seems like you're on the right track with your approach, but there might be a more straightforward way to determine the type of disc in the drive programmatically, especially under Linux.

One common method is to use the blkid command, which is typically used to locate/block device attributes like UUIDs or filesystem types. When a disc is inserted into the drive, it should register as a block device, and blkid can provide information about it.

$blkid

This command should list all block devices, including optical drives and their respective filesystems. By parsing the output of this command, you can determine the type of disc inserted.

Best Regard
Danish Hafeez | QA Assistant
ictinnovations.com

So blkid was my first thought but the main problem with both blkid and lsblk is they show DVD, Blu-ray and Audio CD as only as "rom" or "unknown". If this was differing between hard drive and SSD that would be easy but unfortunately I am not seeing a way to see the disc's type.
Reply
#6
Looks like a job for dd!

Since you will have to place the discs in by hand, I presume, you know what it is and the title.

Have a look here for how to do this in Linux
Reply
#7
(Feb-07-2024, 03:34 AM)Daring_T Wrote: I am not seeing a way to see the disc's type.
I had some success on my linux computer with the command

Output:
udevadm info -q property /dev/cdrom
When a DVD is inserted, the output contains a line
Output:
ID_CDROM_MEDIA_DVD=1
When a CD is inserted, or an audio CD, there is a line
Output:
ID_CDROM_MEDIA_CD=1
When there is nothing in the CD drive, there is no ID_CDROM_MEDIA... line in the output. I don't have a blue ray disk to see what it does.

EDIT: after some tests, I was able to get the information with pyudev
import pyudev


def main():
    context = pyudev.Context()
    a = context.list_devices(sys_name="sr0")
    dev = next(iter(a))
    # print(dev)
    # print(dict(dev.properties))
    if dev.properties.get("ID_CDROM_MEDIA_CD", False):
        print("CD drive contains a CD")
    elif dev.properties.get("ID_CDROM_MEDIA_DVD", False):
        print("CD drive contains a DVD")


if __name__ == "__main__":
    main()
Output:
λ python paillasse/pf/cdrom.py CD drive contains a DVD
It seems that for blue ray, the property is ID_CDROM_MEDIA_BD
Daring_T likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#8
(Feb-07-2024, 07:48 AM)Pedroski55 Wrote: Looks like a job for dd!

Since you will have to place the discs in by hand, I presume, you know what it is and the title.

Have a look here for how to do this in Linux

Eventually no. I have a old CD duplicator that works over Serial that I have automatically inserting Discs into the Drive this is why this is really important. Also this be setup so that my family can rip Audio CD's, DVDs etc. I have ran out of time today but I will see if dd command has useful info for disc's.
Reply
#9
(Feb-07-2024, 01:32 PM)Gribouillis Wrote:
(Feb-07-2024, 03:34 AM)Daring_T Wrote: I am not seeing a way to see the disc's type.
I had some success on my linux computer with the command

Output:
udevadm info -q property /dev/cdrom
When a DVD is inserted, the output contains a line
Output:
ID_CDROM_MEDIA_DVD=1
When a CD is inserted, or an audio CD, there is a line
Output:
ID_CDROM_MEDIA_CD=1
When there is nothing in the CD drive, there is no ID_CDROM_MEDIA... line in the output. I don't have a blue ray disk to see what it does.

EDIT: after some tests, I was able to get the information with pyudev
import pyudev


def main():
    context = pyudev.Context()
    a = context.list_devices(sys_name="sr0")
    dev = next(iter(a))
    # print(dev)
    # print(dict(dev.properties))
    if dev.properties.get("ID_CDROM_MEDIA_CD", False):
        print("CD drive contains a CD")
    elif dev.properties.get("ID_CDROM_MEDIA_DVD", False):
        print("CD drive contains a DVD")


if __name__ == "__main__":
    main()
Output:
λ python paillasse/pf/cdrom.py CD drive contains a DVD
It seems that for blue ray, the property is ID_CDROM_MEDIA_BD

Thanks Gribouillis for the code snippet, worked like a charm. I just need to figure out blue-ray's. Unfortunately dict(dev.properties) does not show or add an different var for blue-rays from what I can see. I need to do some more digging with udev rules and dd but I have ran out of time today. I will post if I find a solution for blue-rays.

Thanks both for your help,
Daring_T

Also here is what a blue-ray looks like with running dict(dev.properties):

Output:
{'CURRENT_TAGS': ':uaccess:systemd:seat:', 'DEVLINKS': '/dev/disk/by-path/pci-0000:00:14.0-usb-0:4.4:1.0-scsi-0:0:0:0 ' '/dev/disk/by-id/usb-ASUS_BW-16D1HT_20201230-0:0 ' '/dev/disk/by-uuid/8cd94d0851963909 ' '/dev/disk/by-label/INCREDIBLES_D2', 'DEVNAME': '/dev/sr1', 'DEVPATH': '/devices/pci0000:00/0000:00:14.0/usb3/3-4/3-4.4/3-4.4:1.0/host5/target5:0:0/5:0:0:0/block/sr1', 'DEVTYPE': 'disk', 'DISKSEQ': '82', 'ID_BUS': 'usb', 'ID_CDROM': '1', 'ID_CDROM_BD': '1', 'ID_CDROM_BD_R': '1', 'ID_CDROM_BD_RE': '1', 'ID_CDROM_BD_R_RRM': '1', 'ID_CDROM_BD_R_SRM': '1', 'ID_CDROM_CD': '1', 'ID_CDROM_CD_R': '1', 'ID_CDROM_CD_RW': '1', 'ID_CDROM_DVD': '1', 'ID_CDROM_DVD_PLUS_R': '1', 'ID_CDROM_DVD_PLUS_RW': '1', 'ID_CDROM_DVD_PLUS_R_DL': '1', 'ID_CDROM_DVD_R': '1', 'ID_CDROM_DVD_RAM': '1', 'ID_CDROM_DVD_RW': '1', 'ID_CDROM_DVD_RW_RO': '1', 'ID_CDROM_DVD_RW_SEQ': '1', 'ID_CDROM_DVD_R_DL': '1', 'ID_CDROM_DVD_R_DL_JR': '1', 'ID_CDROM_DVD_R_DL_SEQ': '1', 'ID_CDROM_MEDIA': '1', 'ID_CDROM_MEDIA_BD': '1', 'ID_CDROM_MEDIA_SESSION_COUNT': '1', 'ID_CDROM_MEDIA_STATE': 'complete', 'ID_CDROM_MEDIA_TRACK_COUNT': '1', 'ID_CDROM_MEDIA_TRACK_COUNT_DATA': '1', 'ID_CDROM_MRW': '1', 'ID_CDROM_MRW_W': '1', 'ID_CDROM_RW_REMOVABLE': '1', 'ID_FOR_SEAT': 'block-pci-0000_00_14_0-usb-0_4_4_1_0-scsi-0_0_0_0', 'ID_FS_APPLICATION_ID': 'APPLICATION_ID', 'ID_FS_LABEL': 'INCREDIBLES_D2', 'ID_FS_LABEL_ENC': 'INCREDIBLES_D2', 'ID_FS_LOGICAL_VOLUME_ID': 'INCREDIBLES_D2', 'ID_FS_TYPE': 'udf', 'ID_FS_USAGE': 'filesystem', 'ID_FS_UUID': '8cd94d0851963909', 'ID_FS_UUID_ENC': '8cd94d0851963909', 'ID_FS_VERSION': '2.50', 'ID_FS_VOLUME_ID': 'INCREDIBLES_D2', 'ID_FS_VOLUME_SET_ID': '8CD94D0851963909_VOLUME_SET_ID', 'ID_INSTANCE': '0:0', 'ID_MODEL': 'BW-16D1HT', 'ID_MODEL_ENC': 'BW-16D1HT\\x20\\x20\\x20\\x20\\x20\\x20\\x20', 'ID_MODEL_ID': '0611', 'ID_PATH': 'pci-0000:00:14.0-usb-0:4.4:1.0-scsi-0:0:0:0', 'ID_PATH_TAG': 'pci-0000_00_14_0-usb-0_4_4_1_0-scsi-0_0_0_0', 'ID_REVISION': '3.00', 'ID_SERIAL': 'ASUS_BW-16D1HT_20201230-0:0', 'ID_SERIAL_SHORT': '01234567', 'ID_TYPE': 'cd', 'ID_USB_DRIVER': 'usb-storage', 'ID_USB_INTERFACES': ':080650:', 'ID_USB_INTERFACE_NUM': '00', 'ID_VENDOR': 'ASUS', 'ID_VENDOR_ENC': 'ASUS\\x20\\x20\\x20\\x20', 'ID_VENDOR_ID': '1f75', 'MAJOR': '11', 'MINOR': '1', 'SUBSYSTEM': 'block', 'SYSTEMD_MOUNT_DEVICE_BOUND': '1', 'TAGS': ':uaccess:systemd:seat:', 'USEC_INITIALIZED': '1989315471'}
Reply
#10
(Feb-08-2024, 05:56 AM)Daring_T Wrote: Also here is what a blue-ray looks like with running dict(dev.properties):
The property ID_CDROM_MEDIA_BD tells you that there is a blueray disk
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  open python files in other drive akbarza 1 715 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Integrating Google Drive in App Lahearle 0 480 Jul-19-2023, 05:51 PM
Last Post: Lahearle
  code to send attachments contained on the drive. stefanoste78 1 872 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,822 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Cant Access D: Drive kucingkembar 4 1,361 Jul-28-2022, 04:53 PM
Last Post: kucingkembar
  Calling exe on network drive GrahamL 4 2,566 Jan-21-2022, 12:01 PM
Last Post: GrahamL
  Failing to copy file from a network to a local drive tester_V 4 7,152 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Type hinting - return type based on parameter micseydel 2 2,510 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Nested Subdirectory within current sub on shared network drive Agregware 1 1,949 Jun-19-2019, 10:07 PM
Last Post: Gribouillis
  trouble with os.listdir on a network drive lconner 10 19,147 Jun-04-2019, 07:16 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020