Linux Containers Authors: Liz McMillan, Elizabeth White, Zakia Bouachraoui, Pat Romanski, Stefana Muller

Related Topics: Linux Containers

Linux Containers: Article

Build a CD Ripper with Python

Programming can be fun again

This article shows you how to develop a straightforward, multiplatform application for ripping CDs into the Ogg Vorbis format. Follow along to see how elegant and quick it is to develop using PyQt.

BlackAdder is an IDE for developing Python and PyQt based applications. Python is a very popular multiplatform, object-oriented scripting language that has been around for some time; you can download it for free from www.python.org. PyQt is the Qt language bindings for Python developed by Riverbank Computing some years ago; you can find out more at www.riverbankcomputing.co.uk. And if you aren't familiar with Qt, it is a multiplatform windowing toolkit created by Trolltech (www.trolltech.com) and is the foundation of KDE (www.kde.org), a popular open source windowing desktop available on Linux.

BlackAdder brings all these elements together, including the Qt Designer for creating user interfaces, and allows you to create projects to organize your files, a syntax-highlighting text editor, a Python debugger, and many other useful features. BlackAdder runs on both Linux and Windows and allows you to create applications that will run anywhere that Python and PyQt run. You can even make Python applications that don't require PyQt. BlackAdder comes in two flavors, the Personal Edition, which allows you to create GPL applications, and the Business Edition, which includes commercial licenses of Qt for PyQt, and PyQt, so you can create closed source, commercial applications.

This article shows how you can use BlackAdder to develop a straightforward, multiplatform application for ripping CDs into the popular open source Ogg Vorbis format (www.vorbis.com) (see Figure 1). What you'll notice is how elegant and quick it is to develop using PyQt as opposed to C++.

This project assumes you are working on Linux; the entire project can be downloaded from www.smga3000.com/thekompany/BAdemo/tkcoggripper.tar.gz, and a demo of BlackAdder can be downloaded from www.smga3000.com/thekompany/BAdemo. In short, you'll:

  • Build an application to rip CDs and encode it as an Ogg Vorbis file with ID tags
  • Query the CDDB server for information about the CD
  • Produce a clean and easy user interface
To do this you'll need: An attractive user interface is always important, so we've put together a collection of attractive icons, listed below. They're included in the project archive.
  • disk.png
  • drive.png
  • eject.png
  • older.png
  • folder_home.png
  • folder_open.png
  • logo_home.png
  • oggripper16.png
  • oggripper32.png
  • options.png
  • query.png
  • select.png
  • select_folder.png
  • stop.png
  • track.png
Creating the user interface before the program logic is a good way to start - function follows form. We'll use Qt Designer (BlackAdder's form editor) to create the main window form. It helps if you have some experience with Qt Designer; however, it's pretty straightforward. The main thing you need to know is that you're grouping widgets in layouts so that your form will be resizable and will work with any font.

We have a group of action buttons on the top. Below, a tab widget contains a "Directories" tab so you can select a destination directory for the ripped files and a "CD" tab for working with the track information. Below that is the main button to stop the ripping process and progress bars that show information about the progress per track and per disk. Figures 2, 3, and 4 show the user interface in different stages of development.

You can quickly create this form for yourself (see Figure 5). First define slots that the form will use; these are the actions you'll need:

  • query cddb server
  • eject
  • stop/quit
  • options
  • select tracks
  • select destination folder
  • rip
So you'll create these slots:
  • query()
  • eject()
  • stop()
  • options()
  • selectFolder()
  • folderClicked(item) for QListView.clicked(QListViewItem)
  • ripTracks()
First look at init(), shown in Listing 1.

FolderItem is a subclass of QListViewItem for showing a directory tree. It's easy to implement the class - just 70 lines. You can write it yourself or find an example on the Net from other projects.

The init() call sets column widths, the CD-ROM device name, and the tab to switch to the page with the directories.

For the call eject() you just run eject command:

    def eject(self, *args):

Now look at query() call, shown in Listing 2. It is a bit complex.

You first query the CD device to get track information (DiscID), then fill the ListView with tracknames Track1... and query the CDDB server for more information about the disc. You then change track names to those names you retrieved. (We've omitted code for showing track length and left it as an exercise for the reader.)

Now it's time to implement the ripping process. The call to ripTracks() just creates a list of tracks that will be ripped. If the user doesn't select any tracks, then just rip them all. We then run ripTrack() to begin ripping the first track.

Look at ripTrack():. It is a long but simple function. It forms arguments for cdparanoia and for oggenc and then runs these two processes using QProcess() (see Listing 3).

You may be wondering how it works. You first start a system process for ripping and encoding. You use QProcess, so work with these processes is asynchronous; this is important so that multiple actions can take place at the same time, such as the actual ripping and showing the progress of the ripping. You can do this in one of two ways: using Qt's QProcess or using Python's Threads.

After you start to process your function's self.ripStdout self.ripStderr, you'll receive all the output of cdparanoia. (We passed the argument "-", so all raw output will send to stdout.).Oggenc will wait for the raw input to stdin, so our function ripStdout() will be very short:

     def ripStdout(self, *args):

Notice that we've implemented a pipe between the two processes.

Next you need to grab the verbose output of cdparanoia from stderr and according to that information show the progress of the rip. CDparanoise will provide a string like "from sector NNN" and "to sector NNN" so you can parse it out pretty easily with regexps. Now you just grab the current sector and count progress as 100% * (sector - fromsector)/(tosector - fromsector).

Next you need to look at the encFinished function. It has code for disconnecting from the processes; set the progress and prepare to rip the next track if one exists.

We've only touched on core aspects of the application. We've created a pretty sophisticated application here, and it's only about 400 lines of code plus the work of creating the form in the designer, which is trivial. We've left out the dialog that allows you to set ogg encoding options such as quality, bitrate, etc., but this is a good exercise for you to test your skills. This same application written in C++ had more than two times the amount of code (it can be downloaded from www.smga3000.com/thekompany/OggVorbis). Another advantage is that Python is interpreted, so you can quickly prototype, test, and debug your application without having to wait for compiles, making the development process even quicker.

These powerful and easy-to-use tools can make you extremely productive, and dare I say it, can even make programming fun again.

More Stories By Shawn Gordon

Shawn Gordon started programming in 1978 and went on to work in all aspects of IT. In 1990 he founded S.M.Gordon & Associates, whichspecialized in software products on the HP 3000. In 2000 he founded theKompany.com to bring developer and desktop software to the Linux operating system.

More Stories By Oleksandr Yakovlyev

Oleksandr Yakovlyev has been working for theKompany.com since September of 2001
on products such as QuantaGold, Kobol, BlackAdder, and tkcOggRipper. Oleksandr graduated with a master's degree in 2002 from the Radio Physics Department T. Shevchenko University of Kiev, Ukraine.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.
The Jevons Paradox suggests that when technological advances increase efficiency of a resource, it results in an overall increase in consumption. Writing on the increased use of coal as a result of technological improvements, 19th-century economist William Stanley Jevons found that these improvements led to the development of new ways to utilize coal. In his session at 19th Cloud Expo, Mark Thiele, Chief Strategy Officer for Apcera, compared the Jevons Paradox to modern-day enterprise IT, examin...
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DSR is a supplier of project management, consultancy services and IT solutions that increase effectiveness of a company's operations in the production sector. The company combines in-depth knowledge of international companies with expert knowledge utilising IT tools that support manufacturing and distribution processes. DSR ensures optimization and integration of internal processes which is necessary for companies to grow rapidly. The rapid growth is possible thanks, to specialized services an...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
Scala Hosting is trusted by 50 000 customers from 120 countries and hosting 700 000+ websites. The company has local presence in the United States and Europe and runs an internal R&D department which focuses on changing the status quo in the web hosting industry. Imagine every website owner running their online business on a fully managed cloud VPS platform at an affordable price that's very close to the price of shared hosting. The efforts of the R&D department in the last 3 years made that pos...