Skip to content

Commit

Permalink
Delete temporary files in AudioSegment.export() (#665)
Browse files Browse the repository at this point in the history
* Delete temporary files even when export() AudioSegment.export() has an error

* refactor: clean up temp files with a finally block

This is a cleaner solution where we don't need to repeat the clean up
code, since the finally block executes in all cases.

Co-authored-by: James Robert <[email protected]>
  • Loading branch information
joanise and jiaaro authored Dec 8, 2022
1 parent 3cee07e commit 996cec4
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions pydub/audio_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,19 +966,20 @@ def export(self, out_f=None, format='mp3', codec=None, bitrate=None, parameters=
log_subprocess_output(p_out)
log_subprocess_output(p_err)

if p.returncode != 0:
raise CouldntEncodeError(
"Encoding failed. ffmpeg/avlib returned error code: {0}\n\nCommand:{1}\n\nOutput from ffmpeg/avlib:\n\n{2}".format(
p.returncode, conversion_command, p_err.decode(errors='ignore') ))

output.seek(0)
out_f.write(output.read())
try:
if p.returncode != 0:
raise CouldntEncodeError(
"Encoding failed. ffmpeg/avlib returned error code: {0}\n\nCommand:{1}\n\nOutput from ffmpeg/avlib:\n\n{2}".format(
p.returncode, conversion_command, p_err.decode(errors='ignore') ))

data.close()
output.close()
output.seek(0)
out_f.write(output.read())

os.unlink(data.name)
os.unlink(output.name)
finally:
data.close()
output.close()
os.unlink(data.name)
os.unlink(output.name)

out_f.seek(0)
return out_f
Expand Down

0 comments on commit 996cec4

Please sign in to comment.