Linux Containers Authors: Yeshim Deniz, Liz McMillan, Zakia Bouachraoui, Elizabeth White, Pat Romanski

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
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...