New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

音を鳴らす #2

Closed
tamago324 opened this Issue May 23, 2018 · 16 comments

Comments

Projects
None yet
1 participant
@tamago324
Owner

tamago324 commented May 23, 2018

通知は音を使ってみる

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

mp3をPythonで再生するにはjiaaro/pydubがいい?よくわかんないけど

色々あるけど
https://github.com/vinta/awesome-python#audio

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

音楽は以下のサイトからダウンロードした

ボタン・システム音[1]|効果音ラボ

決定ボタン、押下7のアプリ風っていう効果音をダウロードした

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

pipenv install pydub

でインストール

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

構成は以下の通り

.
└── sound
    ├── decision7.mp3
    └── sound.py
@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

pyhubで音楽を鳴らすにはpyhub.playbackのplayを使うらしい?

python - Playing audio in pydub - Stack Overflow

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

以下を実行した

from pydub import AudioSegment
from pydub.playback import play

def play_sound():
    sound = AudioSegment.from_mp3('decision7.mp3')
    play(sound)

if __name__ == '__main__':
    play_sound()

エラーが出た

$ penvpy sound/sound.py
Loading .env environment variables…
/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/utils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/utils.py:178: RuntimeWarning: Couldn't find ffplay or avplay - defaulting to ffplay, but may not work
  warn("Couldn't find ffplay or avplay - defaulting to ffplay, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "sound/sound.py", line 14, in <module>
    main()
  File "sound/sound.py", line 10, in main
    play_sound()
  File "sound/sound.py", line 5, in play_sound
    sound = AudioSegment.from_mp3('decision7.mp3')
  File "/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/audio_segment.py", line 532, in from_mp3
    return cls.from_file(file, 'mp3', parameters)
  File "/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/audio_segment.py", line 438, in from_file
    file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
  File "/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/utils.py", line 59, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)
FileNotFoundError: [Errno 2] No such file or directory: 'decision7.mp3'

ファイルのパスがおかしいのかな

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

絶対パスで指定してみる

[Python] スクリプト実行ディレクトリを絶対パスで取得する - Qiita

import os

from pydub import AudioSegment
from pydub.playback import play

ABS_PATH = os.path.dirname(os.path.abspath(__file__))

def play_sound():
    sound = AudioSegment.from_mp3(f'{ABS_PATH}/decision7.mp3')
    play(sound)


if __name__ == '__main__':
    play_sound()

またエラーが出た

penvpy sound/sound.py
Loading .env environment variables…
/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/utils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/utils.py:178: RuntimeWarning: Couldn't find ffplay or avplay - defaulting to ffplay, but may not work
  warn("Couldn't find ffplay or avplay - defaulting to ffplay, but may not work", RuntimeWarning)
Traceback (most recent call last):
  File "sound/sound.py", line 14, in <module>
    play_sound()
  File "sound/sound.py", line 9, in play_sound
    sound = AudioSegment.from_mp3(f'{ABS_PATH}/decision7.mp3')
  File "/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/audio_segment.py", line 532, in from_mp3
    return cls.from_file(file, 'mp3', parameters)
  File "/Users/tamago324/.local/share/virtualenvs/src-yrNssJ3A/lib/python3.6/site-packages/pydub/audio_segment.py", line 512, in from_file
    p = subprocess.Popen(conversion_command, stdin=devnull, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/local/Cellar/python/3.6.4_4/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: 'ffmpeg': 'ffmpeg'

ffmpegっていうのをインストールしないといけないのかな

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

jiaaro/pydub: Manipulate audio with a simple and easy high level interface

pydubのGitHubのページに書いてあったからインストールしてみる

$ brew install ffmpeg --with-libvorbis --with-sdl2 --with-theora

上記のコマンドを実行

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

エラーが出てインストールできなかった...

$ brew install ffmpeg --with-libvorbis --with-sdl2 --with-theora
...(省略)
==> Installing ffmpeg --with-sdl2 --with-libvorbis --with-theora
==> Downloading https://ffmpeg.org/releases/ffmpeg-4.0.tar.xz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/ffmpeg/4.0 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags=
==> make install
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/man/man3/libavcodec.3
/usr/local/share/man/man3 is not writable.

You can try again using:
  brew link ffmpeg
==> Summary
🍺  /usr/local/Cellar/ffmpeg/4.0: 251 files, 51.8MB, built in 8 minutes 30 seconds
@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

和訳してみた

ビルドされていますが、/ usr / localにシンボリックリンクされていません。symlinkをhare / man / man3 / libavcodec.3 / usr / local / share / man / man3に書き込めませんでした。

シンボリックリンクができなかったよーってことか

どういうこと

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

以下を実行してみる

$ brew link ffmpeg
@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

$ brew link ffmpeg
Linking /usr/local/Cellar/ffmpeg/4.0...
Error: Could not symlink share/man/man3/libavcodec.3
/usr/local/share/man/man3 is not writable.

エラー...

調べてみる

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

Homebrewインストールのlinkエラーを修正する方法 - 微忘録

symlink(brewとpostgreSQLの中継役)が正しく設定されていないらしく、postgreSQLが(簡単には)利用できない状況にある。なので、writableになるよう設定を整備します。

解決策
1.writableにしたいman3があるディレクトリに移動し、
2.chownでman3のユーザー権限を現アカウントに委譲
3.再びbrew linkしてpostgresを上書き保存

$ cd /usr/local/share/man
$ sudo chown -R $USER man3
$ brew link --force postgres
Linking /usr/local/Cellar/postgresql/10.1... 
Error: Could not symlink lib/pkgconfig/libecpg.pc
/usr/local/lib/pkgconfig is not writable.

やってみる

$ cd /usr/local/share/man/
$ sudo chown -R $USER man3
$ brew link --force ffmpeg

できているか確認

$ ffmpeg
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-ffplay --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

できていないときに、ffmpegを実行してなかったから、できてるのかわからないけど、実行で聞きてるってことはいいのかな?

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 23, 2018

プログラムを実行してみたら、音なった!!!!!!

$ penvpy sound/sound.py
Loading .env environment variables…
Input #0, wav, from '/var/folders/93/g9_qrz2519z_p3y0k44pctt00000gn/T/tmpme6evsyf.wav':
  Duration: 00:00:00.86, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
   0.78 M-A: -0.000 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0

@tamago324 tamago324 closed this May 23, 2018

@tamago324 tamago324 reopened this May 23, 2018

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 24, 2018

PyAudioのインストールは色々めんどくさそうなのでやめた

@tamago324

This comment has been minimized.

Owner

tamago324 commented May 24, 2018

@tamago324 tamago324 closed this May 24, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment