I just started using pydub. I am confused that should we download the music to find the path or we need to do something else? Because the example below seems like it already have the file in his terminal or IDE. But I don't see how can I import a music into the IDE.
from pydub import AudioSegment
song = AudioSegment.from_wav("never_gonna_give_you_up.wav")
You need to have the never_gonna_give_you_up.wav audio in the same directory as the Python script for this to work. Or if you want to use it from another directory, you need to provide the full path, for example:
song = AudioSegment.from_wav("D:\Music\never_gonna_give_you_up.wav")
Related
I am trying to use python to convert files from mp4 to mp3. After some research most places recommend moviepy. I used the command pip install moviepy and it seemed to go off without a hitch. I go to VS Code and enter what a youtube video told me to enter (I know its not recommended to do that, I just wanted to see if it would work). This is what I have
`
#This code should convert a .mp4 file into a .mp3 file
#This imports the moviepy package
from moviepy.editor import *
#here are the names of my files (I have subbed out actual files names)
mp4_file = "file_name.mp4"
mp3_file = "file_name.mp3
#Here is the the audio being stripped from the .mp4 file
video_clip = VideoFileClip(mp4_file)
audio_clip = video_clip.audio
#this is writing the audio to a .mp3 file at the path that is specified.
audio_clip.write_audiofile(mp3_file)
#this closes the conversion code
audio_clip.close()
VideoClip.close()
`
After running the code I get this error:
RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.
There is a bunch of gibberish above it but that is the final line that gets spit out.
After looking up what the issue is I tried to input:
`
from moviepy.config import change_settings
change_settings({"FFMPEG_BINARY": "/usr/bin/ffmpeg"})
`
And it also did not work. I have tried searching for where ffmpeg is and it is not in /usr/bin/ffmepg or /usr/local/bin/ffmpeg like most sources I have looked at tell me it should be.
I have tried installing ffmpeg on its own by doing pip install ffmpeg and 'brew install ffmpeg'. Both of those go off without a hitch as well but the error still pops.
I am using a macbook air m1 and I have I think everything I need installed already so I am so lost on what is causing this error to pop.
Can someone please help?
I have tried installing ffmpeg on its own as well as searching for the file directly.
I should expect to get the .py file to run fine.
I instead get the error seen above:
RuntimeError: No ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.
try to use this code which is i got
import moviepy.editor
import os
os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg"
mp4_file = "file_name.mp4"
mp3_file = "file_name.mp3"
# Replace the parameter with the location of the video
video = moviepy.editor.VideoFileClip("mp4_file")
audio = video.audio
# Replace the parameter with the location along with filename
audio.write_audiofile("mp3_file")
if it is still not find the ffmpeg then find its actual path and note that
I am writing a program that needs to know which Drive is Windows drive . because I need to go to "Windows-Drive:\Users\publick\Desktop", I need some code or module that will give me the windows drive.
Thanks for your kindness.
If what you really want is the drive where Windows is installed, use
import os
windows_drive = os.environ['SystemDrive']
But it looks like what you actually need is the public desktop folder. You can get that easier and more reliably like this:
import os
desktop_folder = os.path.join(os.environ['PUBLIC'], 'Desktop')
For the current user's desktop folder instead, you can use this:
import os
desktop_folder = os.path.join(os.environ['USERPROFILE'], 'Desktop')
But I'm not sure that the desktop folder is always called 'Desktop' and that it's always a subdirectory of the profile folder.
There is a more reliable way using the pywin32 package (https://github.com/mhammond/pywin32), but it obviously only works if you install that package:
import win32comext.shell.shell as shell
public_desktop_folder = shell.SHGetKnownFolderPath(shell.FOLDERID_PublicDesktop)
user_desktop_folder = shell.SHGetKnownFolderPath(shell.FOLDERID_Desktop)
(Unfortunately pywin32 is not exactly well documented. It's mostly a matter of first figuring out how to do things using Microsoft's Win32 API, then figuring out where to find the function within pywin32.)
#!/usr/bin/env python3
# Python program to explain os.system() method
# importing os module
import os
# Command to execute
# Using Windows OS command
cmd = 'echo %WINDIR%'
# Using os.system() method
os.system(cmd) # check if returns the directory [var = os.system(cmd) print(var) ] in Linux it doesnt
# Using os.popen() method
return_value = os.popen(cmd).read()
can you check this approach ? I am not on Win
I found my Answer!
`import os
Windows-Drive=os.path.dirname(os.path.dirname(os.path.join(os.path.join(os.environ['USERPROFILE']))))
This code is answer!
I am curious to know if I send someone a python file with embedded sound files, if it will still play on another computer or if the file has to be in their drive.
Here I am using playsound, but I can use other modules as needed.
from playsound import playsound
playsound('partyhorn.wav')
The answer is no. The file has to be in the same relative directory.
Now, I'm working on a voice controlling assistant application.
When I say "open Google Chrome", it should open Chrome. I do not have any problems with speech recognition but I do have a problem with starting the program.
For example, the following code:
import os
os.system("chrome.exe")
Works fine for the notepad.exe or cmd.exe programs but when I give external programs like chrome, I need to give the entire path.
However, in C# we can only give the direct name like chrome.exe to run the program.
So, my problem is that, is there any ways to start external programs without giving the entire path like in C#?
Giving path to start the program would be a serious problem. Because when we move the program to another computer, we will face many code errors.
Try os.path.abspath
os.path.abspath("chrome.exe")
returns the following:
C:\Users\Yourname\chrome.exe
The PATH system variable governs this inside Python just as outside. If you want to modify it from Python, it's os.environ.
As an aside, a better solution is probably to use subprocess instead, as suggested in the os.system documentation.
The PATH manipulation will be the same regardless.
import os
import subprocess
os.environ['PATH'] = os.environ['PATH'] + r';c:\google\chrome\'
subprocess.run(['chrome.exe'])
... where obviously you need to add the directory which contains chrome.exe to your PATH.
Probably you will want to make sure the PATH is correct even before running Python in your scenario, though.
The first part of this answer is OS specific and doesn't solve it in code or python
Add the path where Chrome exists to your PATH variable (you'll likely need a restart), and then when you execute a command such as chrome.exe it will try to run it from that path location
UPDATE:
If you want to implement a search for the absolute path here are some good resources on path listing How do I list all files of a directory?
But again you'll likely need some educated guesses and will need to do some OS specific things to find it.
The Chrome binary is often installed in the same set of locations
I solved this problem by using C#. Like I already mentioned, we can directly call programs like "chrome.exe" in C#. I created a console application in C# which open Google Chrome via using bellow code. Then I complied that console application as .exe, after that I link that exe file with my python program.
C#
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "chrome.exe";
ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
ExternalProcess.Start();
ExternalProcess.WaitForExit();
Python
import os
os.system('console_application.exe')
I am using sqlite3 and python for a new website I am creating. The problem is, the "files_storage.db" file I am trying to create will not appear in any Windows 10 Folder Window, PyCharm's Directory View, nor will it appear via the command line interface.
The catch to this is, if I execute my python script multiple times, I get an error that states the database file already exists... So this file is somewhere, I guess it is a game of cat and mouse to find it.
I have ran into this problem before, but I have ALWAYS been able to find the file via the command line. Usually, I wouldn't both yall with such a question but, this is really irking me and I am going to run into serious issues when it comes time to put this baby on a server. :(
thanks in advance, and here's some screenshots I suppose.
You are using a relative file path.
Relative paths are converted to absolute ones by something like os.path.join(os.getcwd(), <relative path>). So they depend on the current working directory of the process.
Try to open it with an absolute path (starting with drive letter) to avoid any ambiguities.
If you use just a filename without a path, the file will be saved in whatever the current working directory of the Python interpreter is.
To see where the current working directory is, add the following code to the beginning of your program:
import os
print(os.getcwd())
You should then see the working directory in the output.
There is a setting for the current working directory in your IDE somewhere. See e.g. the answers to this question.
You can also do something like:
import os
path = os.path.expanduser("~") + '/Documents'
print(path)
This will allow you to access the directories for the current user. For me, this prints:
'/Users/thomasweeks/Documents'