[Mint 18-21] Backup DVD to ISO

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

[Mint 18-21] Backup DVD to ISO

Post by rene »

[EDIT] For Mint 21 / Python 3.10.6 this had to be adjusted a bit; see below at viewtopic.php?p=2300493#p2300493

Although there are many transcoding DVD rippers available and a few non-transcoding ones such as dvdbackup, there seems a somewhat surprising lack of simple tools to just copy a DVD unaltered from its source to an ISO image directly. DVD burner applications such as Brasero can do this but are somewhat overblown.

The below tiny Python script dvdcopy aims to be useful in that sense. It copies the DVD with plain old dd after getting name and size information from isoinfo. Both those as well as the for most commercial movie DVD's necessary libdvdcss2 package are installed by default on both Mint 18.3 and Mint 19; isoinfo is from the genisoimage package if you do not yet have it.

I can not speak to usefulness of this script for Blu-ray; I have discs nor hardware. CSS on DVD is partly drive-based and if this is also the case for the newer systems on Blu-ray then usefulness will depend on libdvdcss2. That is, for all I know Blu-ray may or may not be supported --- although I would assume not.

Save the below as say ~/bin/dvdcopy, make it executable as chmod +x ~/bin/dvdcopy and use dvdcopy -h for a small usage screen. Simply dvdcopy will have it copy /dev/dvd to an after the volume id named iso in the current directory, dvdcopy -i will only show disc information.

Reports of success, failure and/or perceived usefulness welcome...

Code: Select all

#!/usr/bin/env python3
#
# dvdcopy (public domain, 2018)

import argparse
import ctypes
import locale
import subprocess

IBS = 128 * 1024
OBS = 4 * 1024 ** 2


def isoinfo(device):
    def val(tag, line):
        i = line.find(':')
        return '' if i < 0 or line[:i].strip() != tag else line[i + 1:].strip()

    id, bs, vs = '', '', ''
    p = subprocess.run(['isoinfo', '-d', '-i', device], stdout=subprocess.PIPE, universal_newlines=True, check=True)
    for line in p.stdout.splitlines():
        if vs and bs and id:
            break
        if not id:
            id = val('Volume id', line)
            if id:
                continue
        if not bs:
            bs = val('Logical block size is', line)
            if bs:
                continue
        if not vs:
            vs = val('Volume size is', line)
    try:
        bs = int(bs)
        vs = int(vs)
    except ValueError:
        bs = 0
        vs = 0
    return id, bs, vs


locale.setlocale(locale.LC_ALL, '')
parser = argparse.ArgumentParser(description='Backup DVD to ISO.')
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', action='store_true', dest='copy', default=True, help='backup device to iso (default)')
group.add_argument('-i', action='store_false', dest='copy', help='show iso information')
parser.add_argument('-d', dest='device', default='/dev/dvd', help='device (default: /dev/dvd)')
args = parser.parse_args()

id, bs, vs = isoinfo(args.device)
if id and bs and vs:
    size = vs * bs
    i = 0 if size < 1024 ** 2 else 1 if size < 1024 ** 3 else 2
    unit = 'KMG'[i]
    print(locale.format_string('Volume %s, %d bytes (%.1f %cB, %.1f %ciB)',
                               (id, size, size / 1000 ** (i + 1), unit, size / 1024 ** (i + 1), unit)))
    if args.copy:
        libdvdcss = ctypes.CDLL('libdvdcss.so.2')
        libdvdcss.dvdcss_close(libdvdcss.dvdcss_open(args.device.encode()))
        i = IBS // bs
        while vs % i:
             i -= 1
        ibs = bs * i
        obs = OBS // ibs * ibs
        subprocess.run(['dd', 'if=%s' % args.device,
                              'iflag=direct',
                              'ibs=%d' % ibs,
                              'of=%s.iso' % id.lower(),
                              'obs=%d' % obs,
                              'count=%d' % (vs // i),
                              'status=progress'], check = True)
Last edited by rene on Sun Feb 26, 2023 3:19 pm, edited 1 time in total.
User avatar
all41
Level 19
Level 19
Posts: 9523
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: [Mint 18, 19] Backup DVD to ISO

Post by all41 »

rene wrote: Sun Nov 04, 2018 9:35 am
I just use k3b to store a dvd image file. Perhaps this will not always be an option
Last edited by all41 on Sun Nov 04, 2018 10:57 am, edited 1 time in total.
Everything in life was difficult before it became easy.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Mint 18, 19] Backup DVD to ISO

Post by rene »

Yes, sure; mentioned DVD burner applications as alternative possibilities. I'm not completely sure though if those will read at blockdevice or filesystem level and in the latter case just remake the ISO on the fly. Not that that matters much, but the script is by design minimal even if only for educational reasons (which it is not only; I'm using it to satisfaction :-)

Particularly felt educated by it being enough to open and immediately close the disc through libdvdcss2. But sure, feel free to have mileage vary.
User avatar
all41
Level 19
Level 19
Posts: 9523
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: [Mint 18, 19] Backup DVD to ISO

Post by all41 »

Rene,
Edit done.
Your bash instructions have been saved--thanks
Perhaps k3b and others are just gui to this code.
not certain re blocks, but an iso is after all--an iso
Everything in life was difficult before it became easy.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Mint 18, 19] Backup DVD to ISO

Post by rene »

The script goes out of its way somewhat to use dd rather than a simple read/write loop; I trust k3b will be more sensible than that :-)

And yes, certainly an iso is an iso is an iso. I'm from the audio universe accustomed to being really pedantic about getting verifiably original bits off of a disc though. Makes very significantly less sense for DVD, but still.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Mint 18-21] Backup DVD to ISO

Post by rene »

Python 3.9 and later ctypes seems broken on at least Ubuntu/Mint which is to say that things bombed on Mint 21 (Python 3.10.6). Being explicit about argument and return types seems to work around that though; the below works also with Python 3.10.6:

Code: Select all

#!/usr/bin/env python3
#
# dvdcopy (public domain, 2018, 2023)

import argparse
import ctypes
import locale
import subprocess

IBS = 128 * 1024
OBS = 4 * 1024 ** 2


def isoinfo(device):
    def val(tag, line):
        i = line.find(':')
        return '' if i < 0 or line[:i].strip() != tag else line[i + 1:].strip()

    id, bs, vs = '', '', ''
    p = subprocess.run(['isoinfo', '-d', '-i', device], stdout=subprocess.PIPE, universal_newlines=True, check=True)
    for line in p.stdout.splitlines():
        if vs and bs and id:
            break
        if not id:
            id = val('Volume id', line)
            if id:
                continue
        if not bs:
            bs = val('Logical block size is', line)
            if bs:
                continue
        if not vs:
            vs = val('Volume size is', line)
    try:
        bs = int(bs)
        vs = int(vs)
    except ValueError:
        bs = 0
        vs = 0
    return id, bs, vs


locale.setlocale(locale.LC_ALL, '')
parser = argparse.ArgumentParser(description='Backup DVD to ISO.')
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', action='store_true', dest='copy', default=True, help='backup device to iso (default)')
group.add_argument('-i', action='store_false', dest='copy', help='show iso information')
parser.add_argument('-o', dest='image', help='iso image (default: <volume>.iso)')
parser.add_argument('-d', dest='device', default='/dev/dvd', help='device (default: /dev/dvd)')
args = parser.parse_args();

id, bs, vs = isoinfo(args.device)
if id and bs and vs:
    size = vs * bs
    i = 0 if size < 1024 ** 2 else 1 if size < 1024 ** 3 else 2
    unit = 'KMG'[i]
    print(locale.format_string('Volume %s, %d bytes (%.1f %cB, %.1f %ciB)',
                               (id, size, size / 1000 ** (i + 1), unit, size / 1024 ** (i + 1), unit)))
    if args.copy:
        libdvdcss = ctypes.CDLL('libdvdcss.so.2');

        libdvdcss.dvdcss_open.argtypes = [ctypes.c_char_p]
        libdvdcss.dvdcss_open.restype  = ctypes.c_void_p

        libdvdcss.dvdcss_close.argtypes = [ctypes.c_void_p]
        libdvdcss.dvdcss_close.restype  = ctypes.c_int

        libdvdcss.dvdcss_close(libdvdcss.dvdcss_open(args.device.encode()))

        if args.image is None:
            args.image = id.lower() + '.iso'
        i = IBS // bs
        while vs % i:
             i -= 1
        ibs = bs * i
        obs = OBS // ibs * ibs
        subprocess.run(['dd', 'if=%s' % args.device,
                              'iflag=direct',
                              'ibs=%d' % ibs,
                              'of=%s' % args.image,
                              'obs=%d' % obs,
                              'count=%d' % (vs // i),
                              'status=progress'], check = True)
rickNS
Level 9
Level 9
Posts: 2977
Joined: Tue Jan 25, 2011 11:59 pm

Re: [Mint 18-21] Backup DVD to ISO

Post by rickNS »

Thanks for the effort, and the script.

This is handy stuff for a number of reasons.

As per your OP I used to use Brasero for this as it came preinstalled anyway, however since after Mint 19 (I think), the function of extracting .iso from DVD no longer works. I am not sure if any of the other burning software will do it "now" or not, and frankly I don't like 'borrowing' packages from other DE's.

Anyway, I have recently discovered though that Disks (gnome-disks) can do this quite easily, and also comes preinstalled on all DE's.

Open Disks, insert a DVD, click on the Unmount (square) button, click Menu (three dots), select Create Disk Image, file is already named with .iso extension, click start.
This takes about 2 minutes / GB.

I just tried your new script...
rene wrote: Sun Nov 04, 2018 9:35 am Reports of success, failure and/or perceived usefulness welcome...
This is what I got...(OK I did just change it's name to dvd-iso)

Code: Select all

rick@t420:~$ dvd-iso
isoinfo: No such file or directory. Cannot open '/dev/dvd'. Cannot open SCSI driver.
isoinfo: No such file or directory. Unable to open /dev/dvd
Traceback (most recent call last):
  File "/home/rick/bin/dvd-iso", line 52, in <module>
    id, bs, vs = isoinfo(args.device)
  File "/home/rick/bin/dvd-iso", line 20, in isoinfo
    p = subprocess.run(['isoinfo', '-d', '-i', device], stdout=subprocess.PIPE, universal_newlines=True, check=True)
  File "/usr/lib/python3.10/subprocess.py", line 524, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['isoinfo', '-d', '-i', '/dev/dvd']' returned non-zero exit status 2.
I wonder, please keep in mind, I am NO coder, but I see my disk listed as sr0, if I subbed that with /dev/DVD might it work then ?
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
rickNS
Level 9
Level 9
Posts: 2977
Joined: Tue Jan 25, 2011 11:59 pm

Re: [Mint 18-21] Backup DVD to ISO

Post by rickNS »

EDIT 3, I did make the substitution of /dev/DVD to /dev/sr0 in line 49, and the script did execute correctly with a 'clean' disk.
AND To remove some useless stuff from rene's thread.

Sorry rene, it apparently failed at about halfway done.

Code: Select all

rick@t420:~$ dvd-iso
Volume DUMB_AND_DUMBER_UNRATED_PE, 8412033024 bytes (8.4 GB, 7.8 GiB)
4206968832 bytes (4.2 GB, 3.9 GiB) copied, 713 s, 5.9 MB/s 
dd: error reading '/dev/sr0': Input/output error
Last edited by rickNS on Sat Mar 18, 2023 8:50 am, edited 2 times in total.
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Mint 18-21] Backup DVD to ISO

Post by rene »

rickNS wrote: Fri Mar 17, 2023 10:43 am

Code: Select all

dd: error reading '/dev/sr0': Input/output error
That's not an issue to do with the script or any software I'm afraid; disc is probably scratched or dirty and the hardware says it can't read the data at that spot. Doubt that "Disks" would do other, although yes, this tiny script is little other than a proof-of-concept as to using plain old dd, i.e.,, byte-for-byte copy, and doesn't e.g. retry failed reads; if "Disks" does maybe it'll work -- or maybe either would after a cleaning of disk or lens :)
rickNS
Level 9
Level 9
Posts: 2977
Joined: Tue Jan 25, 2011 11:59 pm

Re: [Mint 18-21] Backup DVD to ISO

Post by rickNS »

rene wrote: Fri Mar 17, 2023 11:17 am That's not an issue to do with the script or any software I'm afraid; disc is probably scratched or dirty and the hardware says it can't read the data at that spot. Doubt that "Disks" would do other, although yes, this tiny script is little other than a proof-of-concept as to using plain old dd, i.e.,, byte-for-byte copy, and doesn't e.g. retry failed reads; if "Disks" does maybe it'll work -- or maybe either would after a cleaning of disk or lens :)
Yes Disks also failed at 4.2GB, it still is trying to do something, but time to finish has gone up to hours.
There appears to be no scratch on the disk, but could be a speck of dust.
I'm glad I already copied that disk, and that is another very good reason to do so!

I tried another with your script, and it completed successfully.

Now should I get a moderator to remove some of my nonsense from your tut. ?
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: [Mint 18-21] Backup DVD to ISO

Post by rene »

No, that's perfectly fine. This thing was really only ever posted as a "look how neat it is to only have to open the DVD through libdvdcss to be able to use just anything to copy it directly". You say to not be a programmer but that's not really expected: I before I did this expected that I would need to read through a specific by libdvdcss supplied read() function, i.e., CSS-decrypting as I went along.

Then it turned out to be something I actually enjoyed also using quite a bit more than anything else -- I like me bits original! -- but, well, frankly, it's still just a tiny conceptual thing really...
User avatar
all41
Level 19
Level 19
Posts: 9523
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: [Mint 18-21] Backup DVD to ISO

Post by all41 »

rene wrote: Fri Mar 17, 2023 12:30 pm No, that's perfectly fine. This thing was really only ever posted as a "look how neat it is to only have to open the DVD through libdvdcss to be able to use just anything to copy it directly". You say to not be a programmer but that's not really expected: I before I did this expected that I would need to read through a specific by libdvdcss supplied read() function, i.e., CSS-decrypting as I went along.

Then it turned out to be something I actually enjoyed also using quite a bit more than anything else -- I like me bits original! -- but, well, frankly, it's still just a tiny conceptual thing really...
@rene
it's still good code regarding dvdcss2
Everything in life was difficult before it became easy.
Post Reply

Return to “Tutorials”