7 Replies - 1238 Views - Last Post: 26 November 2012 - 10:48 PM Rate Topic: -----

#1 JeffBurnsRed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 08-November 12

Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 10:33 AM

I am trying to remove duplicate songs based on their ID3 tags rather than just by the title of the file. Is there any way I can do this based on what I have done so far? Also, I would like to be able to read all the mp3 files in a directory without having to select them all. This program can sort all the files nicely but will not display them on the JTextArea unless I have all of the files selected. This poses a problem when you try to select all the files in a directory that contains any sub directory.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class ProjectGUI extends JPanel implements ActionListener {
	
	public static void main(String[] args) {
		new ProjectGUI();
	}
	
	private JButton selectButton;
	private JButton clearButton;
	
	JRadioButton titleBox;
	JRadioButton artistBox;
	JRadioButton albumBox;
	JRadioButton yearBox;
	ButtonGroup group;
	
	final JFileChooser fc = new JFileChooser();
	final String sep = System.getProperty("line.separator");
	
	JTextArea displayBox;
	JTextArea displayBox2;
	JTextArea displayBox3;
	JTextArea displayBox4;
	
	
	public ProjectGUI() {
	displayBox = new JTextArea(20,10);
	displayBox2 = new JTextArea(20, 15);
	displayBox3 = new JTextArea(20, 15);
	displayBox4 = new JTextArea(20, 6);
	
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Dimension dim = toolkit.getScreenSize();	
	
	selectButton = new JButton("Select Directory");
	JButton exportButton = new JButton("Export List to .txt");
	selectButton.addActionListener(this);
		
	titleBox = new JRadioButton("Title", true);
	artistBox = new JRadioButton("Artist", false);
	albumBox = new JRadioButton("Album", false);
	yearBox = new JRadioButton("Year", false);
		
	group = new ButtonGroup();
	group.add(titleBox);
	group.add(artistBox);
	group.add(albumBox);
	group.add(yearBox);
		
	JPanel north = new JPanel(new GridLayout(1, 2));
	north.add(selectButton);
	north.add(exportButton);

	JPanel west = new JPanel(new GridLayout(7, 1));
	west.add(new JLabel ("Sort Options:"));
	west.add(titleBox);
	west.add(artistBox);
	west.add(albumBox);
	west.add(yearBox);

	JPanel center = new JPanel(new GridLayout(1, 4));
	center.add(displayBox);
	center.add(displayBox2);
	center.add(displayBox3);
	center.add(displayBox4);
	
	JPanel south = new JPanel(new FlowLayout());
	clearButton = new JButton("Clear Text");
	clearButton.addActionListener(this);
	south.add(clearButton);
	
	JFrame frame = new JFrame("Music Sorter");
	frame.setSize(1000, 500);
	frame.setLocation(dim.width/2 - 500, dim.height/2 - 250); // divide frame size in half for second values
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setLayout(new BorderLayout());
	frame.add(north, BorderLayout.NORTH);
	frame.add(west, BorderLayout.WEST);
	frame.add(new JScrollPane(center), BorderLayout.CENTER);
	frame.add(south, BorderLayout.SOUTH);
	frame.setVisible(true);
	}
	
	
	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == clearButton)
		displayBox.setText("");
		displayBox2.setText("");
		displayBox3.setText("");
		displayBox4.setText("");
		
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.setMultiSelectionEnabled(true);
		if(event.getSource() == selectButton) {
		int returnVal = fc.showOpenDialog(this);
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				File[] files = fc.getSelectedFiles();
				for(File filelist : files) {
				try {
				FileInputStream stream = new FileInputStream(filelist);
				int size = (int)filelist.length();
				stream.skip(size - 128);
				byte[] last128 = new byte[128];
				stream.read(last128);
				String id3 = new String(last128);
				String tag = id3.substring(0, 3);
				if (tag.equals("TAG")) {
					if (titleBox.isSelected()) {
					// title, artist, album, year
					displayBox.append(id3.substring(3, 32));
					displayBox2.append(id3.substring(33, 62));
					displayBox3.append(id3.substring(63, 91));
					displayBox4.append(id3.substring(93, 97));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					
					// artist, title, album, year
					} else if (artistBox.isSelected()) {
					displayBox.append(id3.substring(33, 62));
					displayBox2.append(id3.substring(3, 32));
					displayBox3.append(id3.substring(63, 91));
					displayBox4.append(id3.substring(93, 97));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					
					// album, title, artist, year	
					} else if (albumBox.isSelected()) {
					displayBox.append(id3.substring(63, 91));
					displayBox2.append(id3.substring(3, 32));
					displayBox3.append(id3.substring(33, 62));
					displayBox4.append(id3.substring(93, 97));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					
					// year, title, artist, album	
					} else if (yearBox.isSelected()) {
					displayBox.append(id3.substring(93, 97));
					displayBox2.append(id3.substring(3, 32));
					displayBox3.append(id3.substring(33, 62));
					displayBox4.append(id3.substring(63, 91));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					}
					
				} else
					stream.close();
					} catch (Exception e) {
					}
				}
			}
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Removing Duplicates Based on ID3 tags

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 12:35 PM

View PostJeffBurnsRed, on 23 November 2012 - 01:33 PM, said:

I am trying to remove duplicate songs based on their ID3 tags rather than just by the title

and wher in your code do you try that ?
Was This Post Helpful? 0
  • +
  • -

#3 JeffBurnsRed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 08-November 12

Re: Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 12:50 PM

I have nothing in my code currently to try and remove duplicates. I should have specified that I need some guidance as to how I can accomplish removing duplicates. Do you have any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 12:57 PM

Removing them from where ? The JTextArea ? Haven't seen any array, arrayList, file to store them
Was This Post Helpful? 0
  • +
  • -

#5 JeffBurnsRed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 08-November 12

Re: Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 01:05 PM

I would like to remove the duplicate files by placing them in another directory.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 01:17 PM

after
File[] files = fc.getSelectedFiles();

create an ArrayList<String>

when you read a id3 check if ity is in the ArrayList (it has a contains() method)
if it is not there simply process as you do now and add the id3 to the ArrayList
if it is already there then do your other stuff
Was This Post Helpful? 1
  • +
  • -

#7 JeffBurnsRed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 08-November 12

Re: Removing Duplicates Based on ID3 tags

Posted 23 November 2012 - 01:27 PM

Alright I will modify my program. Thank you for your help!
Was This Post Helpful? 0
  • +
  • -

#8 JeffBurnsRed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 08-November 12

Re: Removing Duplicates Based on ID3 tags

Posted 26 November 2012 - 10:48 PM

What can I do now to display only the id3 tags of the files that are not duplicated? I have added all the id3 tags to an ArrayList but I'm not sure how to append only the unique tags to the JTextAreas.

The part of the code listed above that I am referring to starts here. I added the tags to the ArrayList at line 25.
  
public void actionPerformed(ActionEvent event) {
		if(event.getSource() == clearButton)
		displayBox.setText("");
		displayBox2.setText("");
		displayBox3.setText("");
		displayBox4.setText("");
		
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fc.setMultiSelectionEnabled(true);
		if(event.getSource() == selectButton) {
		int returnVal = fc.showOpenDialog(this);
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				File[] files = fc.getSelectedFiles();
					ArrayList<String> include = new ArrayList<String>();
				for(File filelist : files) {
				try {
				FileInputStream stream = new FileInputStream(filelist);
				int size = (int)filelist.length();
				stream.skip(size - 128);
				byte[] last128 = new byte[128];
				stream.read(last128);
				String id3 = new String(last128);
				String tag = id3.substring(0, 3);
				if (tag.equals("TAG")) {
					include.add(id3);
					
					// title, artist, album, year
					if (titleBox.isSelected()) {
					displayBox.append(id3.substring(3, 32));
					displayBox2.append(id3.substring(33, 62));
					displayBox3.append(id3.substring(63, 91));
					displayBox4.append(id3.substring(93, 97));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					
					// artist, title, album, year
					} else if (artistBox.isSelected()) {
					displayBox.append(id3.substring(33, 62));
					displayBox2.append(id3.substring(3, 32));
					displayBox3.append(id3.substring(63, 91));
					displayBox4.append(id3.substring(93, 97));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					
					// album, title, artist, year	
					} else if (albumBox.isSelected()) {
					displayBox.append(id3.substring(63, 91));
					displayBox2.append(id3.substring(3, 32));
					displayBox3.append(id3.substring(33, 62));
					displayBox4.append(id3.substring(93, 97));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					
					// year, title, artist, album	
					} else if (yearBox.isSelected()) {
					displayBox.append(id3.substring(93, 97));
					displayBox2.append(id3.substring(3, 32));
					displayBox3.append(id3.substring(33, 62));
					displayBox4.append(id3.substring(63, 91));
						
					displayBox.append(sep);
					displayBox2.append(sep);
					displayBox3.append(sep);
					displayBox4.append(sep);
					}
				} else
					stream.close();
					} catch (Exception e) {
					// System.out.println("Error - " + e.toString());
					}
				}
			}
		}
	}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1