作者: Jet L

  • 【Python】提取视频画面并生成PPT

    比较笨的方法,用来提取PPT课程视频画面,并生成对应的PPT,代码检测黑屏但没有检测白屏,没有检测重复画面(因为有些人讲课会来回翻PPT),因此还有优化空间。内存占用会逐渐增多,不过测试没有出现崩溃的情况。

    PS:做完发现可以直接问讲课人要PPT原件,我,,,

    import cv2
    import os
    import numpy as np
    from pptx import Presentation
    from pptx.util import Inches
    from skimage.metrics import structural_similarity as ssim
    import tkinter as tk
    from tkinter import filedialog, messagebox
    
    # 选择视频和输出目录
    def select_video_and_output():
        video_path = filedialog.askopenfilename(title="选择视频文件", filetypes=[("MP4 files", "*.mp4")])
        if not video_path:
            messagebox.showwarning("选择视频", "未选择视频文件")
            return None, None
        
        output_dir = filedialog.askdirectory(title="选择输出目录")
        if not output_dir:
            messagebox.showwarning("选择输出目录", "未选择输出目录")
            return None, None
    
        pptx_path = os.path.join(output_dir, "output_presentation.pptx")
        return video_path, pptx_path
    
    # 处理视频并生成 PPT
    def process_video_to_ppt(video_path, pptx_path):
        os.makedirs("ppt_images", exist_ok=True)
        
        cap = cv2.VideoCapture(video_path)
        _, prev_frame = cap.read()
        prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
    
        frame_count = 0
        slide_count = 0
        images = []
        similarity_threshold = 0.95  # 提高 SSIM 阈值,减少相似图片
        brightness_threshold = 10  # 黑屏检测(平均亮度 < 10 认为是黑屏)
    
        def process_frame(frame):
            """ 计算 SSIM 相似度,判断是否保存该帧 """
            nonlocal prev_gray, slide_count
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            score = ssim(prev_gray, gray)
    
            # 计算平均亮度,过滤黑屏
            avg_brightness = np.mean(gray)
            if avg_brightness < brightness_threshold:
                return  # 跳过黑屏帧
    
            if score < similarity_threshold:  
                img_path = os.path.join("ppt_images", f"slide_{slide_count}.jpg")
    
                # 确保不同的幻灯片才保存
                if len(images) == 0 or images[-1] != img_path:  
                    cv2.imwrite(img_path, frame)
                    images.append(img_path)
                    slide_count += 1
                    prev_gray = gray  # 只在确认变化时更新参考帧
    
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
    
            # 仅每隔 15 帧处理一次
            if frame_count % 15 == 0:
                process_frame(frame)
    
            frame_count += 1
    
        cap.release()
        # cv2.destroyAllWindows()
    
        # 创建 PPT
        prs = Presentation()
        for img in images:
            slide = prs.slides.add_slide(prs.slide_layouts[5])  # 空白幻灯片
            left, top, width, height = Inches(0), Inches(0), Inches(10), Inches(7.5)
            slide.shapes.add_picture(img, left, top, width, height)
    
        prs.save(pptx_path)
        messagebox.showinfo("完成", f"PPTX 生成完成: {pptx_path}")
    
    # 主函数
    def main():
        root = tk.Tk()
        root.withdraw()  # 隐藏主窗口
        video_path, pptx_path = select_video_and_output()
        if video_path and pptx_path:
            process_video_to_ppt(video_path, pptx_path)
    
    if __name__ == "__main__":
        main()
    
  • 【网站】正确配置Nginx及DNS解析,实现全站裸域名

    网站之前一直是www和裸域名并行,现在有了多个域名,因此想让www直接重定向到裸域名,但是实施过程比我想得要复杂的多。

    一、DNS解析

    设定www域名的CNAME解析,绑定到裸域名。

    二、Nginx配置重定向逻辑

    可以分成几个代码块,此外还需要注意证书的配置,示例:

    1、重定向 HTTP 到 HTTPS,并强制跳转到裸域名:

    server {
    listen 80;
    listen [::]:80;
    server_name example.cn www.example.cn;
    return 301 https://example.cn$request_uri;

    }

    2、重定向 HTTPS 的www 到裸域名

    server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.cn;
    return 301 https://example.cn$request_uri;

    }

    3、HTTPS 裸域名配置

    server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.cn;

    }

    此外,还需要注意的是,Wordpress和Apache2搭配,可以很好的进行重写操作,比如链接结构的更改,但是变更到Nginx之后这种变更就没有那么的自动化了,这个问题在我从Apache2切换到Nginx之后去实现全站配置裸域名时出现得非常频繁。

    如果不想使用插件,可以在配置主站点的各种设置到裸域名后,直接去数据库对post表里的www域名进行查询和替换,实现快速的替换。

    UPDATE wp_posts
    SET post_content = REPLACE(post_content, ‘http://www.example.com’, ‘http://example.com’);

  • 【网站】通过模板函数及WP设置文件配置SMTP邮件服务

    为什么我们需要配置邮件服务?

    第一,配置邮件服务可以避免WP中勾选邮件通知但是后台未配置导致的评论发送延迟。第二,可以让找回密码等服务可用。(也就是你要是没有配置服务所谓的密码找回是无效的,当然有自己服务器的就无所谓了,有更加快捷的方法重置密码)

    服务器注意事项

    前提1:服务器端需要开放465端口。

    前提2:防火墙开放465端口。

    采用465端口是因为云服务器的提供商普遍禁25端口,尝试解封了一下没有什么卵用。

    因此我们可以使用465端口进行邮件服务,国内可以直接使用QQ邮箱,在设置中获取授权码、服务器地址等配置基础信息。Hotmail需要配置另外一套验证机制,有点麻烦了,还是建议使用QQ邮箱,响应速度很快。

    配置步骤

    一般分为两个步骤,对wp-config文件和function模板函数进行修改,其实也可以直接配置模板函数,不过理论上通过WP配置文件配置更加安全,因为权限更高。

    WP-Config:

    // SMTP 配置
    define('HOST', 'smtp.qq.com');// smtp服务器
    define('PORT', 465);// 端口
    define('USERNAME', 'email@qq.com'); // 设置邮箱
    define('PASSWORD', 'password'); // 授权码
    define('SECURE', 'ssl'); // ssl
    define('FROM', 'your_qq_email@qq.com'); // 邮箱
    define('FROM_NAME', 'Name'); // 发件人名称

    模板函数:

    function custom_phpmailer_settings($phpmailer) {
    $phpmailer->isSMTP();
    $phpmailer->Host = HOST;
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port =PORT;
    $phpmailer->Username = USERNAME;
    $phpmailer->Password = PASSWORD;
    $phpmailer->SMTPSecure = SECURE;
    $phpmailer->From = FROM;
    $phpmailer->FromName = FROM_NAME;
    }
    add_action('phpmailer_init', 'custom_phpmailer_settings');