Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Video To Audio

#1
Python Video To Audio

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1077175&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p>A good friend and his wife recently founded an AI startup in the lifestyle niche that uses <a href="https://blog.finxter.com/machine-learning-engineer-income-and-opportunity/" data-type="post" data-id="306050" target="_blank" rel="noreferrer noopener">machine learning</a> to discover specific real-world patterns from videos.</p>
<p>For their business system, they need a pipeline that takes a video file, converts it to audio, and transcribes the audio to standard text that is then used for further processing. I couldn’t help but work on a basic solution to help fix their business problem. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/large-audio-to-text-heres-my-speech-recognition-solution-in-python/" data-type="post" data-id="1075593" target="_blank" rel="noreferrer noopener">Large Audio to Text? Here’s My Speech Recognition Solution in Python</a></p>
<h2>Mini Project Goal – Video to Audio in Python</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="507" height="762" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-254.png" alt="" class="wp-image-1075728" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-254.png 507w, https://blog.finxter.com/wp-content/uplo...00x300.png 200w" sizes="(max-width: 507px) 100vw, 507px" /></figure>
</div>
<p>In this short project tutorial, I’ll share my code solution to <strong>convert a video to audio</strong>, i.e., extracting a <code>.wav</code> file from a <code>.mp4</code> file.</p>
<pre class="wp-block-preformatted"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Given:</strong>
<code>.mp4</code> video file <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Required:</strong>
<code>.wav</code> audio file</pre>
<p>Use Python to accomplish this programmatically! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </p>
<h2>Mini Project Solution – Video to Audio in Python</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="690" height="460" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-252.png" alt="" class="wp-image-1075726" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-252.png 690w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w" sizes="(max-width: 690px) 100vw, 690px" /></figure>
</div>
<p>You can convert a video file to an audio file as follows. First, create a function that takes the location of your input video file as the <code>in_path</code> parameter and the location of your output audio file as the <code>out_path</code> parameter. Second, install and import the <code>moviepy</code> library to read the video file and write out the audio file in the <code>.wav</code> format.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import moviepy.editor as mp def video_to_audio(in_path, out_path): """Convert video file to audio file""" video = mp.VideoFileClip(in_path) video.audio.write_audiofile(out_path) # Video to audio
video_to_audio('sample_video.mp4', 'sample_audio.wav')</pre>
<p>Before this code runs without error, you first need to <code>pip install moviepy</code> in your environment. For instance, if you run Python 3.10, you’d probably have to run <code>pip3.10 install moviepy</code>. More here:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-install-xxx-in-python/" data-type="post" data-id="653128" target="_blank" rel="noreferrer noopener">How to Install a Python Library?</a></p>
<h2>Alternative Using Subprocess</h2>
<p>Using Python, you can convert a video file to an audio file by using the subprocess library. First, store the location of the input video file in the <code>in_path</code> variable, and the location of the output audio file in the <code>out_path</code> variable. Then, call the <code>subprocess.run()</code> method with the command line parameters to convert the video file to an audio file. The output audio file will be in the <code>.wav</code> format.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import subprocess in_path = 'sample_video.mp4'
out_path = 'sample_audio.wav' subprocess.run(["ffmpeg", "-i", in_path, out_path])</pre>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-call-an-external-command-in-python/" data-type="post" data-id="12602" target="_blank" rel="noreferrer noopener">How to Run an Operating System Command in Python</a></p>
<h2>Thanks! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2665.png" alt="♥" class="wp-smiley" style="height: 1em; max-height: 1em;" /></h2>
<p>To keep learning and improving your Python skills with practical code projects, cheat sheets, and free ebooks, <a href="https://blog.finxter.com/email-academy/" data-type="page" data-id="12278" target="_blank" rel="noreferrer noopener">join our email academy here</a> or here:</p>
</div>


https://www.sickgaming.net/blog/2023/01/...-to-audio/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016