<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by DmP314 on Medium]]></title>
        <description><![CDATA[Stories by DmP314 on Medium]]></description>
        <link>https://medium.com/@dmp314?source=rss-16af5b1b403e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VK9nzyM2ytqOdE1b.</url>
            <title>Stories by DmP314 on Medium</title>
            <link>https://medium.com/@dmp314?source=rss-16af5b1b403e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 03:11:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dmp314/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Transcribing long audio recordings in python Jupyter notebook with Google Cloud Services]]></title>
            <link>https://medium.com/@dmp314/transcribing-long-audio-recordings-in-python-jupyter-notebook-with-google-cloud-services-53282591336a?source=rss-16af5b1b403e------2</link>
            <guid isPermaLink="false">https://medium.com/p/53282591336a</guid>
            <category><![CDATA[speech-recognition]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[jupyter-notebook]]></category>
            <dc:creator><![CDATA[DmP314]]></dc:creator>
            <pubDate>Sun, 29 Sep 2019 13:12:36 GMT</pubDate>
            <atom:updated>2019-09-29T13:15:15.910Z</atom:updated>
            <content:encoded><![CDATA[<h3>Transcribing long audio recordings in python Jupyter notebook with Google Cloud Speech-to-Text</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*ByAt0SK9HPUED1ekq4XcAA.png" /><figcaption>Picture from <a href="https://voice.mozilla.org/en/languages">here</a>.</figcaption></figure><p>he task that I recently had at hand was the transcription of few interviews that were made by my wife for her master thesis project. The length of the interviews was around one hour each. <br>Usually, transcription (i.e. creation of the text version of the interview from audio) takes even longer time than the interview itself. It is laborious and pretty dull task that is definitely worse of automation. Recent development of ML techniques made speech recognition very well possible.<br>However, we have not managed to find any ready-made tool or product to make the transcription <strong>of long audio files</strong> affordable for any user without programming skills. I decided to use <strong>Python and Jupyter notebook</strong> to solve this problem.</p><p>The whole procedure consists of several steps:</p><ol><li>Record the audio and save it to your computer</li><li>Set-up Python virtual environment, Jupyter notebook and convert files to .wav format</li><li>Set-up speech recognition software (Google Cloud Speech-to-Text in my case) and transcribe (or recognise) audio files</li><li>Correct the mistakes (unfortunately, still manually)</li></ol><p>Below, we will take a look into these steps in more details. I will primary focus on <strong>macOS</strong> implementation but I am sure that these steps can be reproduces on Windows and Linux.</p><p>The interviews were recoded on IPhone with few applications:</p><ul><li><a href="https://apps.apple.com/us/app/voice-memos/id1069512134">Voice memos</a> (m4a file format)</li><li><a href="https://apps.apple.com/fi/app/voice-recorder-recording-app/id731368203">Voice Recorder — Recording Ap</a> (mp3 files)</li><li><a href="https://apps.apple.com/fi/app/voice-recorder-hd-recording/id492995106">Voice Recorder HD — Recording</a> (mp3 files)</li></ul><p>All of the apps are free to use, or at least have free modes sufficient for the recording of few long audio files. Afterwards it is easy enough to share and save the recordings to the computer as audio files.</p><p>Next, the files needs to be transcoded to the .wav format, because it is used by the majority of speech recognition software packages.<br>You may use a free online converter for that on your own risk, such as <a href="https://audio.online-convert.com/convert-to-wav">Online-convert</a>.<br>Another way (that I followed) is to make the conversion locally within the Jupyter notebook.</p><p>I am assuming that Python 3 is already installed on your computer. Then, I recommend to crate a virtual environment for this small project. Virtual environments are always handy because they isolate the modules you are working without touching system Python installation. You can also safely delete it and start over if something went wrong. <a href="https://janakiev.com/blog/jupyter-virtual-envs/">Hereby</a> is a nice instruction to follow if you do that for the first time.<br>I created a separate folder for the project, navigated to it in the terminal and executed few commands:</p><pre>pip3 install --user virtualenv<br>python3 -m venv speech_env<br>source speech_env/bin/activate</pre><pre>pip install --user ipykernel</pre><pre>python -m ipykernel install --user --name=speech_env</pre><pre>pip install --user jupyterlab</pre><p>To convert the audio files I was using <em>ffmpeg</em> and <em>pybud</em> Python module.<br>The simplest way to install <em>ffmpeg</em> on macOS is using <a href="https://brew.sh/">Homebrew</a>.</p><pre>brew install ffmpeg</pre><p>On Windows the installation is a bit more complicated, and instructions can be found <a href="http://blog.gregzaal.com/how-to-install-ffmpeg-on-windows/">here</a>.</p><p>The p<em>ydub</em> module can be added with</p><pre>pip install pydub</pre><p>Finally, we can start our jupyter lab in terminal with</p><pre>jupyter lab&amp;</pre><p>and create a new notebook with our speech_env kernel.</p><p>In the beginning of the notebook we need to load the modules:</p><pre>from os import path<br>from pydub import AudioSegment</pre><p>Then, we define input and output audio files:</p><pre># files                                                                         <br>src = &quot;Input.mp3&quot;<br>dst = &quot;Output.wav&quot;</pre><p>Finally, we can convert mp3 (or m4a) to wav:</p><pre># convert wav to mp3                                                            <br>sound = AudioSegment.from_file(src, format=&quot;mp3&quot;)<br>sound.export(dst, format=&quot;wav&quot;)</pre><p>As a results you should get</p><pre>&lt;_io.BufferedRandom name=&#39;Output.wav&#39;&gt;</pre><p>and our waf audio file is ready to be transcribed.</p><p>I am sure that there are multiple ways to recognise speech even if we consider only python-based implementations. For example, in the beginning I tried to use <a href="https://pypi.org/project/SpeechRecognition/">Python SpeechRecognition</a> module (you may also check <a href="https://realpython.com/python-speech-recognition/">this tutorial</a>). Maybe, I didn’t spend enough effort on it but I have not managed to make Sphinx working with my 1-hour long file. Thus, I stopped my choice on Google Cloud Speech API, and decided to use it directly without SpeechRecognition module. This option worked out for me.<br>Below, are the steps that need to be taken.</p><p><strong>#1</strong> It is necessary to <strong>register in Google Cloud platform</strong>. It is fairly easy to do <a href="https://cloud.google.com/speech-to-text/">over the link</a> if you already have Google account.<br>Note, that the online form below can be used to try with very short audio files. This was not my case.<br>I believe that you can start with free trial, if you are not planning to recognise many files in the future.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eRL9XVxt4mLdn6cxTuKDIg.png" /><figcaption>Google Cloud Platform (GCP) Speech-to-Text service</figcaption></figure><p>The main source of information for the next steps is <a href="https://cloud.google.com/speech-to-text/docs/">Google Cloud Speech-to-Text documentation</a>.</p><p><strong>#2</strong> In order to make an audio transcription request, the easiest way <strong>is to setup the environment for GCP</strong> following the <a href="https://cloud.google.com/speech-to-text/docs/quickstart-client-libraries">Quickstart: Using client libraries</a>.<br>Like it is described there, we need to</p><ol><li><em>Set up a GCP Console project</em> (just press the button “Set up a project”)<br>Important, that as a result you should get a private JSON Service Account key for the project.<br>Then, it is possible to check and generate a new key if needed from the <a href="https://console.cloud.google.com/apis/credentials">Google Cloud Platform (GCP) Console Credentials</a> -&gt; Create Credentials -&gt;Service Account Key -&gt; You/or new service account, JSON<br>You can save the key to your project folder with notebook and audio files.</li><li>Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON in Terminal. Remember, this variable only applies to your current shell session.<br>export GOOGLE_APPLICATION_CREDENTIALS=”/home/user/Downloads/service-account-file.json”</li><li>Install the latest Cloud SDK version following <a href="https://cloud.google.com/sdk/docs/">the instructions</a><br>Just do not forget initialise the SDK with<br>gcloud init</li></ol><p>After that you can check that the key is working correctly with</p><pre>gcloud auth application-default print-access-token</pre><p>Finally, we need to install Google cloud speech python client library</p><pre>pip install --upgrade google-cloud-speech</pre><p>It may be possible that audio transcription request will work already now with the local audio file, but it didn’t in my case. I guess again due to the size and leng of the audio file.</p><p><strong>#3</strong> To work with lager files on GCP, it is necessary to put audio files into the <strong>Google Cloud Storage (GS)</strong>.</p><p>Firstly, it is necessary to create a new a new GS bucket for your already existing project. This can be done either <a href="https://cloud.google.com/storage/docs/quickstart-console">from the Console</a> or from the <a href="https://cloud.google.com/storage/docs/quickstart-gsutil">Terminal with gsutils</a> (use your unique but non-personal bucket name instead “my-awesome-bucket”):</p><pre>gsutil mb -l us-east1 gs://my-awesome-bucket/</pre><p>Then, you can copy your audio file to the bucket (BTW, this can be added to your Jupyter notebook by just adding “!” in the beginning of the cell):</p><pre>gsutil cp Output.wav gs://my-awesome-bucket</pre><p>Now we are ready for the transcribing!</p><p><strong>#4 </strong>Finally, there is <a href="https://cloud.google.com/speech-to-text/docs/async-recognize#speech-async-recognize-gcs-python"><strong>Asynchronous speech recognition</strong></a> mode in GCP for long audio files.</p><p>I recommend to close and open Jupyter notebook again at this stage so that all our installations are taken into account in the kernel.</p><p>Let’s load the modules:</p><pre>from google.cloud import speech_v1<br>from google.cloud.speech_v1 import enums</pre><p>Define input parameters (default more worked best for me):</p><pre>storage_uri = &#39;gs://my-awesome-bucket/Output.wav&#39;<br>model = &#39;default&#39; # The transcription model to use, e.g. video, phone_call, default</pre><p>Define the function to do the asynchronous transcript request:</p><pre>def sample_recognize_uri(storage_uri, model):<br>    &quot;&quot;&quot;<br>    Transcribe a short audio file using a specified transcription model</pre><pre>Args:<br>      local_file_path Path to local audio file, e.g. /path/audio.wav<br>      model The transcription model to use, e.g. video, phone_call, default<br>      For a list of available transcription models, see:<br>      <a href="https://cloud.google.com/speech-to-text/docs/transcription-model#transcription_models">https://cloud.google.com/speech-to-text/docs/transcription-model#transcription_models</a><br>    &quot;&quot;&quot;</pre><pre>client = speech_v1.SpeechClient()</pre><pre># The language of the supplied audio<br>    language_code = &quot;en-US&quot;<br>    <br>    config = {<br>        &quot;model&quot;: model,<br>        &quot;language_code&quot;: language_code,<br>    }<br>    <br>    audio = {&quot;uri&quot;: storage_uri}</pre><pre>operation = client.long_running_recognize(config, audio)</pre><pre>print(u&quot;Waiting for operation to complete...&quot;)<br>    response = operation.result()</pre><pre>for result in response.results:<br>        alternative = result.alternatives[0]<br>        print(u&quot;Transcript: {}&quot;.format(alternative.transcript))<br>        <br># Returning the recognized text<br>return response.results</pre><p>Now we can send the request to the server</p><pre>result = sample_recognize_uri(storage_uri, model)</pre><p>and wait for the results (processing is done in batches and takes some time):</p><pre>Waiting for operation to complete...<br>Transcript:  hello<br>Transcript:  hello<br>Transcript:  can you hear me<br>Transcript:  can you hear me</pre><pre>and so on</pre><p>This approach gives what I was expecting. Of course, there is a room for improvement:</p><ul><li>you can try to d<a href="https://cloud.google.com/speech-to-text/docs/multiple-voices">istinguish between several voices</a> in the discussion</li><li>check several <a href="https://cloud.google.com/speech-to-text/docs/transcription-model">transcription model</a></li><li>check confidence level of different alternatives: confidence: 0.9471473693847656</li><li>etc.</li></ul><p>Nevertheless, the final step in any case is to go through the text manually to avoid such mistakes :)</p><pre>I pee questions and problems</pre><p>instead of IP (Intellectual Property).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53282591336a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>