App下載

提取視頻中的音頻 Python只需要三行代碼!

迷糊的小七 2021-08-20 14:21:09 瀏覽數(shù) (2636)
反饋

身處數(shù)據(jù)爆炸增長的信息時代,各種各樣的數(shù)據(jù)都飛速增長,視頻數(shù)據(jù)也不例外。我們可以使用 python 來提取視頻中的音頻,而這僅僅需要安裝一個體量很小的 python 庫,然后執(zhí)行三行代碼!

語音數(shù)據(jù)在數(shù)據(jù)分析領(lǐng)域極為重要。比如可以分析語義、口音、根據(jù)人的情緒等等??梢詰?yīng)用于偏好分析、謊話檢測等等。

一、提取音頻

需要用到 python 的 moviepy 庫

moviepy的 github 地址:https://github.com/Zulko/moviepy

命令行 pip 安裝上 moviepy 即可!

pip install moviepy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

提取音頻:假設(shè)有一個 mp4 文件路徑為"D:pythonpycharm2020my_programvideo_process est_01.mp4",我們想提取其中的音頻保存到"D:pythonpycharm2020my_programvideo_processvst01.mp3",三行 Python 代碼實現(xiàn)如下:

import moviepy.editor as mpy


# 截取背景音樂
audio_background = mpy.AudioFileClip(r'D:pythonpycharm2020my_programvideo_process	est_01.mp4').subclip(1, 60)
audio_background.write_audiofile(r'D:pythonpycharm2020my_programvideo_processvst01.mp3')

結(jié)果如下:

執(zhí)行上面的三行代碼,就會發(fā)現(xiàn)音頻文件已經(jīng)成功提取到指定文件夾啦~ ~這里的視頻格式和音頻格式都支持其他格式,比如讀取 mp4 格式視頻,抽取其中的背景音樂保存為 MP3 格式音頻。

二、引力波繪制

數(shù)據(jù)來源:

從配置文檔中讀取時間相關(guān)數(shù)據(jù)

import numpy as np  # 科學(xué)計算所用的numpy庫
import matplotlib.pyplot as plt  # 繪圖所用的庫matplotlib
from scipy.io import wavfile  # 讀取波形文件所用的庫


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_h
ltime_interval = 1 / rate_l
fig = plt.figure(figsize=(12, 6))  # 創(chuàng)建大小為12*6的繪圖空間

# 丟失信號起始點
htime_len = hstrain.shape[0] / rate_h  # 讀取數(shù)據(jù)第一維的長度,得到函數(shù)在坐標(biāo)軸上總長度
htime = np.arange(-htime_len / 2, htime_len / 2, htime_interval)  # (起點,終點,時間間隔)

使用來自 “H1” 探測器的數(shù)據(jù)作圖

plth = fig.add_subplot(221)  # 設(shè)置繪圖區(qū)域
plth.plot(htime, hstrain, 'r')  # 畫出以時間為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_l
ltime = 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()  # 自動調(diào)整圖像外部邊緣

保存并顯示圖像

plt.savefig("Gravitational_Waves_Original.png")  # 保存圖像為png格式
plt.show()
plt.close(fig)

結(jié)果如下:

以上就是python提取視頻中的音頻的詳細(xì)內(nèi)容,更多python學(xué)習(xí)內(nèi)容請關(guān)注W3Cschool其它相關(guān)文章!

0 人點贊