提取視頻中的音頻 Python只需要三行代碼!
身處數(shù)據(jù)爆炸增長(zhǎng)的信息時(shí)代,各種各樣的數(shù)據(jù)都飛速增長(zhǎng),視頻數(shù)據(jù)也不例外。我們可以使用 python 來提取視頻中的音頻,而這僅僅需要安裝一個(gè)體量很小的 python 庫(kù),然后執(zhí)行三行代碼!
語音數(shù)據(jù)在數(shù)據(jù)分析領(lǐng)域極為重要。比如可以分析語義、口音、根據(jù)人的情緒等等。可以應(yīng)用于偏好分析、謊話檢測(cè)等等。
一、提取音頻需要用到 python 的 moviepy 庫(kù)
moviepy的 github 地址:https://github.com/Zulko/moviepy
命令行 pip 安裝上 moviepy 即可!
pip install moviepy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

提取音頻:假設(shè)有一個(gè) mp4 文件路徑為'D:pythonpycharm2020my_programvideo_processtest_01.mp4',我們想提取其中的音頻保存到'D:pythonpycharm2020my_programvideo_processvst01.mp3',三行 Python 代碼實(shí)現(xiàn)如下:
import moviepy.editor as mpy# 截取背景音樂audio_background = mpy.AudioFileClip(r’D:pythonpycharm2020my_programvideo_processtest_01.mp4’).subclip(1, 60)audio_background.write_audiofile(r’D:pythonpycharm2020my_programvideo_processvst01.mp3’)
結(jié)果如下:

執(zhí)行上面的三行代碼,就會(huì)發(fā)現(xiàn)音頻文件已經(jīng)成功提取到指定文件夾啦~ ~這里的視頻格式和音頻格式都支持其他格式,比如讀取 mp4 格式視頻,抽取其中的背景音樂保存為 MP3 格式音頻。
二、引力波繪制
數(shù)據(jù)來源:
http://python123.io/dv/grawave.html http://python123.io/dv/H1_Strain.wav http://python123.io/dv/L1_Strain.wav http://python123.io/dv/wf_template.txt從配置文檔中讀取時(shí)間相關(guān)數(shù)據(jù)
import numpy as np # 科學(xué)計(jì)算所用的numpy庫(kù)import matplotlib.pyplot as plt # 繪圖所用的庫(kù)matplotlibfrom scipy.io import wavfile # 讀取波形文件所用的庫(kù)rate_h, hstrain = wavfile.read(r'H1_Strain.wav', 'rb') # 讀取音頻文件rate_l, lstrain = wavfile.read(r'L1_Strain.wav', 'rb')# reftime, ref_H1 = np.genfromtxt(’GW150914_4_NR_waveform_template.txt’).transpose()reftime, ref_H1 = np.genfromtxt(’wf_template.txt’).transpose() # 使用python123.io下載txt文件
構(gòu)造應(yīng)變數(shù)據(jù)
htime_interval = 1 / rate_hltime_interval = 1 / rate_lfig = plt.figure(figsize=(12, 6)) # 創(chuàng)建大小為12*6的繪圖空間# 丟失信號(hào)起始點(diǎn)htime_len = hstrain.shape[0] / rate_h # 讀取數(shù)據(jù)第一維的長(zhǎng)度,得到函數(shù)在坐標(biāo)軸上總長(zhǎng)度htime = np.arange(-htime_len / 2, htime_len / 2, htime_interval) # (起點(diǎn),終點(diǎn),時(shí)間間隔)
使用來自 “H1” 探測(cè)器的數(shù)據(jù)作圖
plth = fig.add_subplot(221) # 設(shè)置繪圖區(qū)域plth.plot(htime, hstrain, ’r’) # 畫出以時(shí)間為x軸,應(yīng)變數(shù)據(jù)為y軸的圖像,‘y’為黃色plth.set_xlabel(’Time (seconds)’)plth.set_ylabel(’H1 Strain’)plth.set_title(’H1 Strain’)
繪制 L1 Strain 和Template
ltime_len = lstrain.shape[0] / rate_lltime = np.arange(-ltime_len / 2, ltime_len / 2, ltime_interval)pltl = fig.add_subplot(222)pltl.plot(ltime, lstrain, ’k’)pltl.set_xlabel(’Time (seconds)’)pltl.set_ylabel(’L1 Strain’)pltl.set_title(’L1 Strain’)pltref = fig.add_subplot(212)pltref.plot(reftime, ref_H1, ’purple’)pltref.set_xlabel(’Time (seconds)’)pltref.set_ylabel(’Template Strain’)pltref.set_title(’Template’)fig.tight_layout() # 自動(dòng)調(diào)整圖像外部邊緣
保存并顯示圖像
plt.savefig('Gravitational_Waves_Original.png') # 保存圖像為png格式plt.show()plt.close(fig)
結(jié)果如下:


以上就是提取視頻中的音頻 Python只需要三行代碼!的詳細(xì)內(nèi)容,更多關(guān)于python 提取視頻中的音頻的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 解決spring @ControllerAdvice處理異常無法正確匹配自定義異常2. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法3. 從Python的字符串中剝離所有非數(shù)字字符(“。”除外)4. IDEA一鍵完成格式化、去除無用引用、編譯的操作5. android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn)6. 詳解python 內(nèi)存優(yōu)化7. 詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式8. python新手學(xué)習(xí)使用庫(kù)9. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條10. PHP如何開啟Opcache功能提升程序處理效率

網(wǎng)公網(wǎng)安備