多媒体技术 音视频文件格式分析和实现

音视频文件格式分析和实现

用matlab2016处理,部分题目使用了Matlab7。

1.声音的播放、保存

播放:[y,fs]=audioread(‘1.wav’);
sound(y,fs);
保存:[y,fs]=audioread(‘1.wav’);
audiowrite(‘d:\img\music2.wav’,y,fs)

2.声音的特效处理

变音量:[y,fs]=audioread(‘music.wav’);
sound(10*y,fs);

声道控制:[x,FS]=audioread(‘music.wav’); % 将 WAV 文件转换成变量
x1=x(:,1); % 抽取第 1 声道
x2=x(:,2); % 抽取第 2 声道
sound(x2,FS);
%sound(x1,FS);%分别听效果

倍速播放:[y,fs]=audioread(‘music.wav’);
sound(y,2fs);%2倍速播放。sound(y,0.5fs);0.5倍速播放

3.视频的播放

mov= aviread(‘d:\img\1.avi’); %Matlab7使用
movie(mov)%播放视频

4.视频与静态图像的转换

mov=aviread(‘d:\img\3ds.avi’);
fnum=size(mov,2); %读取电影的祯数
for i=1:fnum
strtemp=strcat(‘d:\img\im1’,int2str(i),’.bmp’);%将每祯转成jpg的图片
imwrite(mov(i).cdata,strtemp,‘bmp’);
end

5.视频水印效果

Python代码:
import cv2
import numpy as np
def frame_pip(frame1, frame2, N, w):
“Add frame2 at the top-right corner of frame1”
" N - the size of the top-left region"
" w - weight og frame 2"
offset_y = 10 # offset ot the top border
offset_x = 20 # offset ot the right border

frame2_resized = cv2.resize(frame2, (N, N))
# Generate a circle type mask
logo_region_mask = np.zeros((N, N, 3), dtype = np.uint8)
# 在logo_region_mask中,以(N,N)为圆心,以N为半径画一个实心圆
cv2.circle(logo_region_mask, (np.int(N/2),np.int(N/2)), np.int(N/2), (1,1,1), -1)

region_tmp = w*frame2_resized + (1-w)*frame1[offset_y:(N+offset_y), -(N+offset_x):-offset_x, :]
region_tmp = region_tmp.astype(np.uint8)
# 先生成一个小区域logo_region
logo_region = frame1[offset_y:(N+offset_y), -(N+offset_x):-offset_x, :]
logo_region[logo_region_mask>0] = region_tmp[logo_region_mask>0]
# 把小区域logo_region的值更新回img_marked
frame1[offset_y:(N+offset_y), -(N+offset_x):-offset_x, :] = logo_region
frame1 = frame1.astype(np.uint8)

vid_file = ‘movie.mp4’
#1. open a video and the camera
vid_reader = cv2.VideoCapture(vid_file)

vid_capture = cv2.VideoCapture(0)

fps = 30
sz = (int(vid_reader.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vid_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)))

#2. Open an video writer in mp4 format
fourcc = cv2.VideoWriter_fourcc(‘m’, ‘p’, ‘4’, ‘v’)
vid_writer = cv2.VideoWriter()
vid_writer.open(‘hahaha.mp4’, fourcc, fps, sz, True)

while(vid_reader.isOpened() and vid_capture.isOpened()):
ret1, frame1 = vid_reader.read() # 读视频一帧
ret2, frame2 = vid_capture.read() # 读摄像头一帧
if not(ret1) or not(ret2):
break
# 镜像
# frame1 = frame1[::-1, :, :]
# frame1 = frame1[:, ::-1, :]
frame_pip(frame1, frame2, N=81, w=0.6)

#3. save the video via the writer
vid_writer.write(frame1)

cv2.imshow(‘zhangsan’, frame1)
#wait until key strokes to break
if cv2.waitKey(30) & 0xFF == ord(‘q’):
break

#4. close the video handlers
vid_reader.release()
vid_capture.release()
vid_writer.release()

6.视频快照实现

vidobj = videoinput(‘winvideo’, 1);
snapshot = getsnapshot(vidobj);
imagesc(snapshot)

7.视频的保存

video=videoinput(‘winvideo’,1);
preview(video);

wVideo=VideoWriter( ‘Video.avi’);%创建文件保存名
wVideo.FrameRate = 13;%设置视频的帧率
open(wVideo);%打开WriterVideo对象准备写入
for ii=1:50
EVI=getsnapshot(video);%得到当前时刻视频对象video的画面矩阵
writeVideo(wVideo, EVI);%写入文件
end
disp(‘视频储存完成!!!’);

close(wVideo);%关闭该对象
closepreview;


 

上一篇:「作者推荐」【JVM性能优化】史上最清晰的探究和分析【Safe Point+Safe Region】


下一篇:[LeetCode] 1257. Smallest Common Region