what should i do for playing a m4a file in python3? - python-3.x

I used pygame and mplayer library to play a m4a music file in python languages,but all failed. Is there a library i can use to play m4a file in python?
here is my code:
pipes = dict(stdin=PIPE, stdout=PIPE, stderr=PIPE)
mplayer = Popen(["mplayer", r"C:\Users\sam\PycharmProjects\untitled\music.m4a"], **pipes)
# to control u can use Popen.communicate
mplayer.communicate(input=b">")
sys.stdout.flush()
i used the pycharm ,and the result is :
Traceback (most recent call last):
File "C:/Users/sam/PycharmProjects/untitled/tt.py", line 23, in
mplayer = Popen(["mplayer", r"C:\Users\sam\PycharmProjects\untitled\music.m4a"], **pipes)
File "C:\Users\sam\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in init
restore_signals, start_new_session)
File "C:\Users\sam\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

Here is a link to a good thread that I believe answers the exact same thing that you are having issues with.
Play m4a in python script with mplayer

Related

How to compress a video using ffmpeg in python?

I made this script for compressing videos in Python 3:
import os
import sys
import subprocess
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
print(result)
When I run the above, some error will come up like System cannot find the file specified :
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
File "C:\Program Files\Python37\lib\subprocess.py", line 488, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Program Files\Python37\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python37\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Question: How to fix code for correct compressing of videos?
Nearly correct! It looks like the only module you need is subprocess. You should run your command in run() function. Try this:
import subprocess
result = subprocess.run('ffmpeg -i output.mp4 -b 800k output.mp4')
print(result)

Indexing audio to get timestamps for each word using python

I have an audio file and I want to get the timestamps for each word. I want to know during which time period was each the words spoken.
(For example if an audio file says "I am a doctor" I want to know during which instant "I" was said, "am" was said and so on)
I want to do this using python.
I have tried the following code.
from SimpleAudioIndexer import SimpleAudioIndexer as sai
indexer = sai(mode="ibm", src_dir="D:/Codes/Python/audio recognition",
username_ibm="", password_ibm="")
indexer.index_audio(basename = "target.wav")
indexer.save_indexed_audio("{}/indexed_audio".format(indexer.src_dir))
indexer.load_indexed_audio("{}/indexed_audio.txt".format(indexer.src_dir))
print(indexer.get_timestamps())
However I am running into following error.
Traceback (most recent call last):
File "D:\Codes\Python\audio recognition\rec.py", line 5, in <module>
indexer.index_audio(basename = "target.wav")
File "C:\Users\Awais\AppData\Roaming\Python\Python37\site-
packages\SimpleAudioIndexer\__init__.py", line 1108, in index_audio
self._index_audio_ibm(*args, **kwargs)
File "C:\Users\Awais\AppData\Roaming\Python\Python37\site-
packages\SimpleAudioIndexer\__init__.py", line 928, in _index_audio_ibm
replace_already_indexed=replace_already_indexed)
File "C:\Users\Awais\AppData\Roaming\Python\Python37\site-
packages\SimpleAudioIndexer\__init__.py", line 730, in _prepare_audio
self._filtering_step(basename)
File "C:\Users\Awais\AppData\Roaming\Python\Python37\site-
packages\SimpleAudioIndexer\__init__.py", line 638, in _filtering_step
universal_newlines=True).communicate()
File "C:\Program Files (x86)\Microsoft Visual
Studio\Shared\Python37_64\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Program Files (x86)\Microsoft Visual
Studio\Shared\Python37_64\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
From what I can tell from your code/errors is that you're running Windows, but you're using forward slashes "/" in your src_dir string. This won't work as Windows uses the backslash "\" when navigating folders. Such as:
C:\Windows
while many Unix based systems use the forward slash like this:
/home/Awais
In your code you should try to change the "/" to "\" and see if that makes a difference.
If it is your intention on using this program on both Windows and Unix systems, you should use python3's "pathlib" to ensure your directory paths work on either OS. Here is a link that you can use to learn more about handling paths in python3:
https://medium.com/#ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f

FileNot FoundError:fluidsynth

I just want to convert my midi files to mp3 by using midi2audio, i use this code:
----------
from midi2audio import FluidSynth
FluidSynth().midi_to_audio('test.mid','test.mp3')
---------- or
from midi2audio import FluidSynth
FluidSynth().play_midi('test.mid')
but i got the same result:
----------
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/blake/Desktop/Bsmart_music/test/t_p/1.py
Traceback (most recent call last):
File "/Users/blake/Desktop/Bsmart_music/test/t_p/1.py", line 5, in <module>
FluidSynth().play_midi('test.mid')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/midi2audio.py", line 49, in play_midi
subprocess.call(['fluidsynth', '-i', self.sound_font, midi_file, '-r', str(self.sample_rate)])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'fluidsynth': 'fluidsynth'
Process finished with exit code 1
----------
So, i try to copy the fold "fluidsynth" to "midi2audio", but it still doesn't work. I have installed both midi2audio and fluidsynth, who knows what happened?
you must copy "fluidsynth" in some folder pointed by your PATH environ variable
( type "echo $PATH" in terminal app to see what is registered actually ).
On unix-like systems python will usually find fluidsynth located at /usr/bin/fluidsynth ( as /usr/bin is listed in the PATH ). on Os X things are not much different any documentation should fit. A common practice is to use /usr/local/bin folder for adding custom programs.
Maybe you should install fluidsynth first
for ubuntu:
sudo apt-get install fluidsynth

Segmentation fault (core dumped) while calling python script from NodeJS through spawn

I have python script which prints out long list through statistical R (by PypeR). This python script is working absolutely fine.
Now I am trying to run this script from NodeJS through spawn functionality of child_process but it fails with following error:-
Traceback (most recent call last):
File "pyper_sample.py", line 5, in <module>
r=R()
File "/home/mehtam/pyper.py", line 582, in __init__
'prog' : Popen(RCMD, stdin=PIPE, stdout=PIPE, stderr=return_err and _STDOUT or childstderr, startupinfo=info),
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
./temp.sh: line 1: 27500 Segmentation fault (core dumped) python pyper_sample.py o1dn01.tsv cpu_overall
child process exited with code : 139
Note: My python script is working perfectly. I already tested it manually.
My python script is working perfectly. I already tested it manually.
The output clearly shows that OSError: No such file or directory exception happened during Popen() call.
It means that the program is not found e.g.,
>>> from subprocess import Popen
>>> p = Popen(["ls", "-l"]) # OK
>>> total 0
>>> p = Popen(["no-such-program-in-current-path"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Also, passing the whole command as a string instead of a list (shell=False by default) is a common error:
>>> p = Popen("ls -l")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Make sure:
your (child) program can be found in current $PATH
use a list argument instead of a string
test whether it works if you run it manually from a different working directory, different user, etc
Note: your Popen() call passes startupinfo that is Windows only. A string command with several arguments that would work on Windows fails with the "No such file or directory" error on Unix.

chardet in python3 and unknown file encoding

I use chardet for recognize my file encoding, but this error happend :
fh= open("file", mode="r")
sc= chardet.detect(fh)
Traceback (most recent call last):
File "/home/alireza/test.py", line 19, in <module>
sc= chardet.detect(fh)
File "/usr/lib/python3/dist-packages/chardet/__init__.py", line 24, in detect
u.feed(aBuf)
File "/usr/lib/python3/dist-packages/chardet/universaldetector.py", line 65, in feed
aLen = len(aBuf)
TypeError: object of type '_io.TextIOWrapper' has no len()
and i can't open file with out know the encoding,
fh= open("file", mode="r").read()
sc= chardet.detect(fh)
Traceback (most recent call last):
File "/home/alireza/workspacee/makecdown/test.py", line 21, in <module>
fh= open("910.srt", mode="r").read()
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 34: invalid continuation byte
how to use chardet without open file ?! or any way to find out file encoding after/before opening ?
Try opening the file like this
fh= open("file", mode="rb")
Command Line Tool
If this does not work, try the command line tool of chardet.
Description from https://github.com/erikrose/chardet:
chardet comes with a command-line script which reports on the
encodings of one or more files:
% chardetect.py somefile someotherfile
somefile: windows-1252 with confidence 0.5
someotherfile: ascii with confidence 1.0
Not a direct answer, but you can find the description how it works in Python 3 here http://getpython3.com/diveintopython3/case-study-porting-chardet-to-python-3.html. After studying that, you may find the way how to detect another specific encoding.
The code was initially derived from Mozilla Seamonkey. You may find more information also there. Or look for some more advanced Python package related to Seamonkey.

Resources