{"id":131498,"date":"2023-01-22T09:11:33","date_gmt":"2023-01-22T09:11:33","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1077175"},"modified":"2023-01-22T09:11:33","modified_gmt":"2023-01-22T09:11:33","slug":"python-video-to-audio","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/22\/python-video-to-audio\/","title":{"rendered":"Python Video To Audio"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<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>\n<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\u2019t help but work on a basic solution to help fix their business problem. <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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\u2019s My Speech Recognition Solution in Python<\/a><\/p>\n<h2>Mini Project Goal &#8211; Video to Audio in Python<\/h2>\n<div class=\"wp-block-image\">\n<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\/uploads\/2023\/01\/image-254-200x300.png 200w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/figure>\n<\/div>\n<p>In this short project tutorial, I\u2019ll 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>\n<pre class=\"wp-block-preformatted\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Given:<\/strong>\n<code>.mp4<\/code> video file <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Required:<\/strong>\n<code>.wav<\/code> audio file<\/pre>\n<p>Use Python to accomplish this programmatically! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/p>\n<h2>Mini Project Solution &#8211; Video to Audio in Python<\/h2>\n<div class=\"wp-block-image\">\n<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\/uploads\/2023\/01\/image-252-300x200.png 300w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/figure>\n<\/div>\n<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>\n<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\nvideo_to_audio('sample_video.mp4', 'sample_audio.wav')<\/pre>\n<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&#8217;d probably have to run <code>pip3.10 install moviepy<\/code>. More here:<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<h2>Alternative Using Subprocess<\/h2>\n<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>\n<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'\nout_path = 'sample_audio.wav' subprocess.run([\"ffmpeg\", \"-i\", in_path, out_path])<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<h2>Thanks! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2665.png\" alt=\"\u2665\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/h2>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) A good friend and his wife recently founded an AI startup in the lifestyle niche that uses machine learning to discover specific real-world patterns from videos. 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-131498","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=131498"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131498\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}