Skip to content

Instantly share code, notes, and snippets.

@oelna
Last active January 19, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oelna/91714e4dea10c432efbec7a3e64789a1 to your computer and use it in GitHub Desktop.
Save oelna/91714e4dea10c432efbec7a3e64789a1 to your computer and use it in GitHub Desktop.
Clean an iTunes M4A audio track from all identifying/personal information with AtomicParsley and Python

What is this

This python script is a radical attempt to remove all personal information from iTunes M4A audio tracks. All ID3 information will be lost in the process, sadly. You need to figure out a special offset in one of your audio files in a hex editor for this to work. If you are looking for a very simple solution, this is not your script.

If this helps you, please consider tipping to make up for the ridiculous amount of time I lost putting this together:
Gittip Flattr

Instructions for macOS (was OS X)

  • get a recent (0.9.5+) version of AtomicParsley (I downloaded the stable OS X binary from bitbucket.org/shield007/atomicparsley), put it in your /Applications folder (or note where you put it) and make it executable
  • modify the file clean.py: adjust the path to the AtomicParsley executable where necessary, figure out the required value for the variable stringtoreplace. You need to open one of your M4A files with a hex editor such as HexFiend and locate the offset at the beginning of the file where your name is written in plain text. Note that exact string and enter it as the variable value. The script will replace this part of the file with zeros.
  • make a new Automator app, containing a Get Selected Finder Items action and a Run Shell Script action. For the shell script, set pass input to as arguments and enter the code from automatorscript.sh.
  • save the Automator file as app.

You should be able to drag and drop one or multiple M4A audio tracks to the app icon now. New versions of the tracks will be created with a _clean suffix. They should be smaller in file size as well.

Apologies

I suck at Python, bash shell scripting, AtomicParsley, Automator and pretty much every aspect of this app. Thanks again, stackoverflow!

for f in "$@"
do
python ~/Desktop/clean.py "$f"
done
import os
import sys
import subprocess
# exit if no filename argument
if len(sys.argv[1:]) < 1:
print "Please provide a filename as argument"
sys.exit()
# get the filename parts from the argument
fdir=os.path.dirname(sys.argv[1:][0])
file=os.path.basename(sys.argv[1:][0])
fname=os.path.splitext(file)[0]
fext=os.path.splitext(file)[1]
tmpfile=fdir+'/'+fname+'_temp'+fext if (fdir != '') else fname+'_temp'+fext
command='/Applications/AtomicParsley "'+sys.argv[1:][0]+'" --metaEnema -o "'+tmpfile+'"'
# run AtomicParsley metaEnema
check=subprocess.check_output(['/Applications/AtomicParsley', sys.argv[1:][0], '--metaEnema', '-o', tmpfile]) #, shell=True
# make sure the target tmpfile exists, even if AP didn't do anything
if not os.path.isfile(tmpfile):
from shutil import copyfile
copyfile(sys.argv[1:][0], tmpfile)
print 'AtomicParsley did not find any atoms to remove'
# make a replacement string of the same length
stringtoreplace='Your Name' # have a look at one of your purchased titles in a Hex Editor and enter the name here
cleanstring="0" * len(stringtoreplace)
# read the source file
f=open(tmpfile,"rb")
s=f.read()
f.close()
s=s.replace(bytes(stringtoreplace), bytes(cleanstring))
# write the new file
outfile=fdir+'/'+fname+'_clean'+fext if (fdir != '') else fname+'_clean'+fext
f2=open(fdir+'/'+fname+'_clean'+fext, 'w')
f2.write(s)
f2.close()
# delete the temporary file
os.remove(tmpfile)
print 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment