{"id":2012,"date":"2019-05-15T06:10:13","date_gmt":"2019-05-15T04:10:13","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2012"},"modified":"2019-08-31T07:21:26","modified_gmt":"2019-08-31T05:21:26","slug":"record-with-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/record-with-python\/","title":{"rendered":"Record Sounds with Python and convert wav to mp3"},"content":{"rendered":"<p>To <strong>record<\/strong> with <strong>Python<\/strong> install <strong>scipy<\/strong> and <strong>sounddevice<\/strong> in cmd or powershell,<\/p>\n<ul>\n<li>pip install sounddevice<\/li>\n<li>pip install scipy<\/li>\n<\/ul>\n<p>and then save a file (call it recording.py for example) and run it with the following code below here (using any editor):<\/p>\n<pre class=\"lang:default decode:true \">import sounddevice as sd\r\nfrom scipy.io.wavfile import write\r\nimport os\r\n\r\nfs = 44100  # this is the frequency sampling; also: 4999, 64000\r\nseconds = 5  # Duration of recording\r\n\r\nmyrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)\r\nprint(\"Starting: Speak now!\")\r\nsd.wait()  # Wait until recording is finished\r\nprint(\"finished\")\r\nwrite('output.wav', fs, myrecording)  # Save as WAV file\r\nos.startfile(\"output.wav\")<\/pre>\n<p>It will take some seconds to start recording, you will know when to start looking at the console on which it will be printed &#8220;Starting: Speak now&#8221;. There is no way to see on the screen when the time ends, for now.<\/p>\n<p><iframe loading=\"lazy\" title=\"Python to record audio\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/4zkievc54S4?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>This is a sample recorder with the code above. I used the microphone that I was using while I was recording, so it is possible to record from the microphone and save audio at the same time in the video file and the audio file.<\/p>\n<audio class=\"wp-audio-shortcode\" id=\"audio-2012-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/wav\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/05\/output.wav?_=1\" \/><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/05\/output.wav\">https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/05\/output.wav<\/a><\/audio>\n<h2>Convert wav in mp3<\/h2>\n<p>Install pydub with pip install pydub and then save a file with this code<\/p>\n<pre class=\"lang:default decode:true \">from pydub import AudioSegment\r\nsound = AudioSegment.from_wav('output.wav')\r\n\r\nsound.export('myfile.mp3', format='mp3')<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"Record sounds python scipy and sounddevice\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/record-with-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2009,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[384],"class_list":["post-2012","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","tag-record-sounds"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=2012"}],"version-history":[{"count":12,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2012\/revisions"}],"predecessor-version":[{"id":3300,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2012\/revisions\/3300"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2009"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}