LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-07-2010, 01:34 AM   #1
wheeliee
Member
 
Registered: Aug 2008
Posts: 47

Rep: Reputation: 15
Thumbs up HOW TO cut an arbitrary portion of an mp3 file on command line


While this job is probably best-suited for a tool like Audacity (where you would just select a portion of what you want deleted with a cursor, and then go to Edit -> Cut to get it deleted), you may fail to impress that hot girl at the coffeeplace you go to in doing it with a cowardly GUI tool. That's right. Mitchell loves a man who knows his way around bash.

Suppose that you've an mp3 file, all of it is beautiful, except some arbitrary portion in the middle. You just don't like it. Mitchell doesn't like it either. Therefore, it has to be dealt with.

Let's work on a file foo.mp3, which is 6 minutes and 16 seconds long. We want to do away with the portion starting at 1:22 and ending at 1:47.

Step 1. We figure out how big the file is
Code:
$ ls -l foo.mp3
-rwxrwxrwx 1 kh kh 6022715 2010-05-04 23:55 foo.mp3
Step 2. We recall that the file was 6 minutes and 16 seconds long (that's 376 seconds in total, 6*60+16 is 376), so now we figure out how big each second of this file will be. We will impress Mitchell at this stage by demonstrating our perl-fu:
Code:
$  perl -e "print 6022715/376"
16017.8590425532
Alright. There's our answer. There are 16017 bytes to every second of this file.

Step 3. We will now figure out where the bad portion of the file is -- recall, it was from 1:22 (that's 82 seconds) to 1:47 (that's 107 seconds). Let's get to it, and show some more Perl-muscle.

Code:
$ perl -e "print 16017*82"
1313394
$ perl -e "print 16017*107"
1713819
Step 4. Now we'll slice it with the split tool. We'll slice it by 1313394 bytes each, as that is where the dirty portion of the mp3 files begins.
Code:
$  split -d -b 1313394 foo.mp3
As I have no doubt you immediately opened up split's man page to check what the -d -b arguments do, I have no reason to tell you what they are.

But nah, I'll tell you anyway, just because I love you so much. The -b argument lets you specify what size you want the splits to be, we followed that by 1313395. The -d argument will make the splits use numeric suffixes instead of alphabetic ones -- as you can imagine, numeric suffixes are easier to manage and use with other tools should the need arise. Anyway, here's an output of ls -l:
Code:
$ ls -l
total 11804
-rwxrwxrwx 1 kh kh 6022715 2010-05-04 23:55 foo.mp3
-rw-r--r-- 1 kh kh 1313394 2010-05-07 01:09 x00
-rw-r--r-- 1 kh kh 1313394 2010-05-07 01:09 x01
-rw-r--r-- 1 kh kh 1313394 2010-05-07 01:09 x02
-rw-r--r-- 1 kh kh 1313394 2010-05-07 01:09 x03
-rw-r--r-- 1 kh kh  769139 2010-05-07 01:09 x04
Step 5. We'll rename the very first part (x00) to part1.mp3, and remove the rest.
Code:
$ mv x00 part1.mp3
$ rm x*
Step 6. We'll now split foo.mp3 once more, but we'll make each split 1713819 bytes long -- recall, this is where the dirty portion ends.

Code:
$ split -d -b 1713819 foo.mp3
Observe that there are fewers amounts of split, because we're splitting the same file by larger sum sizes.

Step 7. We now will delete the first portion -- x00, which is the everything up until where the dirty part ends:

Code:
$ rm x00
Step 8. Finally, we merge the first part (i.e. part1.mp3) with the rest of the newly created splits (except x00, which we just sent to hell):

Code:
$ cat part1.mp3 x* > for_mitchell.mp3
Step 9. Uh huh. You tiger. Well, what are you waiting for? Try it out!

Code:
$ mplayer for_mitchell.mp3
Mmmhmm. That's what I'm talkin' 'bout.

Step 10. Now go collect your sex.

Last edited by wheeliee; 05-07-2010 at 02:01 AM.
 
Old 05-07-2010, 05:20 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Um, um .... you know you can just do this with mencoder or ffmpeg in one single command and collect your sex earlier and for longer. Well, ok, no sex cuz you're just a silly geek, but maybe some Counter Strike or Half Life or Quake * ... with is better anyway, less risk.
 
Old 05-07-2010, 08:20 AM   #3
wheeliee
Member
 
Registered: Aug 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by H_TeXMeX_H View Post
Um, um .... you know you can just do this with mencoder or ffmpeg in one single command and collect your sex earlier and for longer. Well, ok, no sex cuz you're just a silly geek, but maybe some Counter Strike or Half Life or Quake * ... with is better anyway, less risk.
Yes. Well, no, I didn't know specifically, but I think I knew that it probably could have been done more easily by some other method.

Anyway, the narrative was written on a condition, as I was being helped by someone on IRC ("I'll help you in the condition that you'll post a how-to guide detailing what I've just told you now"). Beyond a bit of fun, and just getting used to tools of bash (and in the eventual process ensuing criticism that includes help in itself, like yours), I just like the idea of writing human-readable, followable narratives (read: _not_ the soul-consuming man pages).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cut and paste from command line dulli1 Linux - Newbie 9 10-12-2010 09:31 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
MP3 from command line songeek Solaris / OpenSolaris 3 04-03-2007 11:28 PM
Cut(command) line by line leventis Programming 4 09-24-2006 06:51 PM
command line mp3 xviddivxoggmp3 Slackware 3 02-26-2004 09:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration