提取視頻中的音頻 Python只需要三行代碼!
身處數(shù)據(jù)爆炸增長(zhǎng)的信息時(shí)代,各種各樣的數(shù)據(jù)都飛速增長(zhǎng),視頻數(shù)據(jù)也不例外。我們可以使用 python 來(lái)提取視頻中的音頻,而這僅僅需要安裝一個(gè)體量很小的 python 庫(kù),然后執(zhí)行三行代碼!
語(yǔ)音數(shù)據(jù)在數(shù)據(jù)分析領(lǐng)域極為重要。比如可以分析語(yǔ)義、口音、根據(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ù)來(lái)源:
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í)間間隔)
使用來(lái)自 “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. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實(shí)例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 如何通過python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例9. 開發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
