0

I'm trying to use pydub to combine multiple audio files.
When I create a string and then try to exec that while assigning it to a variable it doesn't work and the variable returns a 'none' type error when I try to export the audio.

from pydub import AudioSegment

bass = AudioSegment.from_file("bass.ogg")
gtr = AudioSegment.from_file("guitar.ogg")

tracks = "gtr.overlay(bass)" 
execTracks = exec(tracks) # I'd like this to work 
manualTracks = gtr.overlay(bass) # This works properly

print(execTracks)
print(manualTracks)

output:
None 
<pydub.audio_segment.AudioSegment object at 0x100b915e0>  

The reason I want to use 'exec' is because the 'tracks' variable will change depending on the files available.

Thank you for any tips or guidance!

jtbuddha
  • 1
  • 2
  • Welcome to SO. ```exec()``` returns ```None``` as indicated by : https://docs.python.org/3/library/functions.html#exec so ```print(exec(tracks))``` will be equivalent to ```print(None)```. ```exec()``` just executes the code. What exactly is ```tracks```? You could take a look at ```eval()``` but I'm not confident that's your intention. – ewokx Mar 09 '22 at 04:18
  • I should have changed my variable 'execTracks' to something different that's a little confusing I see. I have ```print(execTracks)``` at the end. I did type ```execTracks = exec(tracks)``` but I thought that would assign the output to that pydub.audiosegment like the manualTracks statement does. – jtbuddha Mar 09 '22 at 04:24
  • please read the link for ```exec```. It returns ```None```, which is not useful to you. You could try using ```eval()``` but that's potentially a security hazard. – ewokx Mar 09 '22 at 04:26
  • Will check out eval() and be conscious of security, and I think i understand that exec can't return a value.. would the solution be to include the variable assignation inside of the string that exec executes? (giving that a trying now regardless!) – jtbuddha Mar 09 '22 at 04:32
  • YES IT DID. THANK YOU. - trying to find how to upvote your answer or comments but I think I'm too new or something? Anyways... thank you! – jtbuddha Mar 09 '22 at 04:34

0 Answers0