很困 随便写写
一,准备:
屏:oled 128*64 spi接口 sh1106驱动
树莓派 4 kali系统
python3
跳线若干
二,接线
gun接gun
vcc接3v3
clk接spisclk
mos接spimosi
res接gpio17
dc接gpio22
cs接spice0
三,准备材料
随便去哪下载个badapple mp3格式
找个软件把视频分帧 分成一个一个的图片
然后用格式工厂把视频的音乐分出来
你要懒得下用以下代码也可以(不推荐)
import cv2 from moviepy.editor import * videos_src_path = "C:\\Users\\Administrator\\Desktop\\12"#视频文件夹 video_formats = [".mp4", ".MOV"]#视频可选格式 frames_save_path = "C:\\Users\\Administrator\\Desktop\\save"#图片保存文件夹 width = 128#宽 height = 64#高 time_interval = 29#需要的帧数 def audio(): video = VideoFileClip(videos_src_path+'\\1233.mp4') # 视频所在路径 audio = video.audio audio.write_audiofile(frames_save_path+'\\1.mp3') # 音频所在路径 def video2frame(video_src_path, formats, frame_save_path, frame_width, frame_height, interval): """ 将视频按固定间隔读取写入图片 :param video_src_path: 视频存放路径 :param formats: 包含的所有视频格式 :param frame_save_path: 保存路径 :param frame_width: 保存帧宽 :param frame_height: 保存帧高 :param interval: 保存帧间隔 :return: 帧图片 """ videos = os.listdir(video_src_path) def filter_format(x, all_formats): if x[-4:] in all_formats: return True else: return False videos = filter(lambda x: filter_format(x, formats), videos) for each_video in videos: print("正在读取视频:", each_video) each_video_full_path = os.path.join(video_src_path, each_video) cap = cv2.VideoCapture(each_video_full_path) vc=cap fps = vc.get(cv2.CAP_PROP_FPS) # 获取帧率 print(fps) # 帧率可能不是整数 需要取整 fps=round(fps) frame_index = 0 frame_count = 0 if cap.isOpened(): success = True else: success = False print("读取失败!") while (success): success, frame = cap.read() print("---> 正在读取第%d帧:" % frame_index, success) if ((frame_index % round(fps/interval) == 0) or interval>=fps) and success: resize_frame = cv2.resize(frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA) resize_frame = cv2.cvtColor(resize_frame, cv2.COLOR_BGR2GRAY) ret, resize_frame = cv2.threshold(resize_frame, 150, 255,cv2.THRESH_BINARY) cv2.imwrite(frames_save_path+'//'+str(frame_count)+"p.jpg", resize_frame) print("已经存取第%d帧:" % frame_index) frame_count += 1 frame_index += 1 cap.release() if __name__ == '__main__': #video2frame(videos_src_path, video_formats, frames_save_path, width, height, time_interval)#拆分出图片 audio()#拆分出声效
代码都是网上下载的 我做了些更改 你们需要改啥上面都有写
然后上传图片和音效至树莓派的 最好放在不同的文件夹 看好自己的权限
四,更改树莓派配置文件
树莓派连接hdmi时 默认是声音不从3.5mm口走
更改 /boot/config.txt 加填 dtparam=audio=on
调大音量 试播音频 如果没有试下非root账户
五,代码
from luma.core.interface.serial import spi from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106 from PIL import Image import os,threading from playsound import playsound import time class pyAU(threading.Thread): def __init__(self): threading.Thread.__init__(self) pass def run(self): playsound('/root/apple-mp3/apple.mp3')#声音文件目录 pass serial =spi(gpio_DC=22,gpio_RST=17,device=0, port=0) device = sh1106(serial) # ssd1306, ssd1325, ssd1331, sh1106 path='/root/apple-img'#图片目录 a=os.listdir(path) device.clear() time.sleep(1) pyAU().start() fps=54#帧率进行微调与声音同步 try: for i in range(len(a)): im=Image.open(path+'/'+str(i)+'p.jpg').convert('1') device.display(im) time.sleep(1/fps) finally: device.clear()
以上没包的自行pip3 install 安装
不推荐用py进行视频分帧和取音频
以上。。。
---
我好困 心脏好难受。。 呃呃呃呃呃