4

I am trying to download a .mp3 file, although, when I download it, it is downloaded but sounds like sqeaks and other strange noises through my speakers (similar to when you have a .mp3 file that has errors in the coding).

Here is my current code:

# sets the song url to mp3link variable.
mp3link = dLink[0]

# opens the .mp3 file - using the same procedure as above.
openmp3 = open('testing.mp3', 'w')
dl = urllib2.urlopen(mp3link)
dl2 = dl.read()

# writes the .mp3 file to the file 'testing.mp3' which is in the variable openmp3.
openmp3.write(dl2)
openmp3.close()

print 'done'

I am aware that I could use this code as a faster method:

dlmp3 = urllib2.urlopen(url)
with open('testing2.mp3', 'wb') as filee:
    filee.write(dlmp3.read())

Is there somebody that can tell me what I am doing wrong and how I would be able to fix it? Thanks.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
ericmusembi
  • 61
  • 1
  • 3

1 Answers1

2

I found the fix at: Downloading Mp3 using Python in Windows mangles the song however in Linux it doesn't

"Try binary file mode. open(mp3Name, "wb") You're probably getting line ending translations.

The file is binary, yes. It's the mode that wasn't. When a file is opened, it can be set to read as a text file (this is default). When it does this, it will convert line endings to match the platform. On Windows, line ends are \r\n In most other places it's either \r or \n. This change messes up the data stream. "

Community
  • 1
  • 1
ericmusembi
  • 61
  • 1
  • 3