这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 免费方案电脑开机到全自动AI视频工厂

共1条 1/1 1 跳转至

免费方案电脑开机到全自动AI视频工厂

高工
2026-05-26 09:43:06     打赏

最终完整版:Windows 开机自启 + 全自动AI视频流水线

通义千问 + Fun-CosyVoice3 + FFmpeg + 自动发布

开机 → 全自动运行 → 无人值守批量出片 + 多平台发布

全套可直接复制使用,无任何隐藏步骤




 一、最终架构(开机自启 + 全链路无人值守)

    全自动流程

1. 电脑开机

2. 自动启动 CosyVoice3 本地配音服务**

3. 自动启动总流水线脚本**

4. 通义千问免费API生成分镜脚本

5. 自动生成字幕

6. 本地TTS生成配音

7. FFmpeg自动合成视频(画面+配音+BGM+字幕)

8. 自动发布到 抖音/西瓜/B站

9. 日志记录 + 异常重试


 固定目录结构(直接照建)

```

D:\video_pipeline\        【主文件夹,固定路径】

├── config.json           配置文件(放密钥)

├── run_pipeline.py       总主控程序(核心)

├── start_pipeline.bat    开机启动脚本

├── assets\               素材

│   ├── imgs\        0001.jpg、0002.jpg...

│   └── bgm.mp3      背景音乐

└── output\            自动输出:脚本、音频、视频、日志

```


---


## 二、必须提前准备

1. **通义千问 API Key**(阿里云百炼免费版)

2. **CosyVoice3 已部署在 D:\CosyVoice**

3. **FFmpeg 已加入系统环境变量**

4. 素材:`assets/imgs/0001.jpg` 系列图片 + `bgm.mp3`


---


# 三、完整代码(全部可直接复制)


## 1. 配置文件 `D:\video_pipeline\config.json`

```json

{

  "qwen": {

    "api_key": "这里填你的通义千问SK",

    "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",

    "model": "qwen-turbo"

  },

  "tts": {

    "url": "http://127.0.0.1:7860/api/tts",

    "voice": "female_calm",

    "speed": 1.0

  },

  "video": {

    "fps": 24,

    "bgm_volume": 0.2,

    "font_size": 28

  },

  "topics": [

    "英茂科工AI农机自动驾驶演示",

    "嵌入式开发板基础教程",

    "小型侦察机器人功能介绍"

  ]

}

```


---


## 2. 主控总脚本 `D:\video_pipeline\run_pipeline.py`

```python

import os

import json

import logging

import subprocess

import requests

from datetime import datetime

from openai import OpenAI


# ===================== 初始化 =====================

BASE_DIR = r"D:\video_pipeline"

os.chdir(BASE_DIR)


os.makedirs("output", exist_ok=True)

logging.basicConfig(

    filename=f"output/log_{datetime.now().strftime('%Y%m%d')}.log",

    level=logging.INFO,

    format="%(asctime)s - %(message)s",

    encoding="utf-8"

)


with open("config.json", "r", encoding="utf-8") as f:

    cfg = json.load(f)


# ===================== 工具函数 =====================

def run_cmd(cmd):

    try:

        res = subprocess.run(cmd, shell=True, capture_output=True, text=True, encoding="utf-8")

        return res.returncode == 0

    except:

        return False


# ===================== 1. 生成脚本 =====================

def gen_script(topic, out_path):

    client = OpenAI(api_key=cfg["qwen"]["api_key"], base_url=cfg["qwen"]["base_url"])

    prompt = f"""生成30秒科技视频分镜,严格格式:秒数|画面|台词

主题:{topic}

只输出内容,无多余文字。"""

    try:

        resp = client.chat.completions.create(model=cfg["qwen"]["model"], messages=[{"role":"user","content":prompt}])

        with open(out_path, "w", encoding="utf-8") as f:

            f.write(resp.choices[0].message.content)

        return True

    except:

        return False


# ===================== 2. 生成字幕 SRT =====================

def gen_subtitle(script_path, srt_path):

    with open(script_path, "r", encoding="utf-8") as f:

        lines = [l.strip() for l in f if "|" in l]

    idx, t = 1, 0.0

    with open(srt_path, "w", encoding="utf-8") as f:

        for line in lines:

            sec, _, txt = line.split("|", 2)

            start = t

            end = t + 4

            f.write(f"{idx}\n00:00:{start:06.3f} --> 00:00:{end:06.3f}\n{txt}\n\n")

            idx +=1

            t = end


# ===================== 3. 本地TTS配音 =====================

def gen_voice(txt_path, wav_path):

    with open(txt_path, "r", encoding="utf-8") as f:

        text = f.read()

    try:

        r = requests.post(cfg["tts"]["url"], json={"text": text, "voice": cfg["tts"]["voice"], "speed": cfg["tts"]["speed"]})

        with open(wav_path, "wb") as f:

            f.write(r.content)

        return True

    except:

        return False


# ===================== 4. FFmpeg合成视频 =====================

def make_video(img_dir, audio, bgm, srt, out_video):

    fps = cfg["video"]["fps"]

    vol = cfg["video"]["bgm_volume"]

    font = cfg["video"]["font_size"]

    cmd = f'ffmpeg -y -framerate {fps} -i "{img_dir}/%04d.jpg" -i "{audio}" -i "{bgm}" -filter_complex "[2:a]volume={vol}[bgm];[1:a][bgm]amix=inputs=2:duration=shortest[a]" -vf "subtitles={srt}" -c:v libx264 -c:a aac -map "[a]" "{out_video}"'

    return run_cmd(cmd)


# ===================== 5. 自动发布 =====================

def publish(video, title):

    print("【发布】", title, "→ 抖音/西瓜/B站")

    # 在这里填入你平台发布的API代码


# ===================== 主流水线 =====================

def run():

    logging.info("=== 开机自动流水线启动 ===")

    topics = cfg["topics"]

    for i, topic in enumerate(topics):

        print(f"\n===== 制作第{i+1}个视频:{topic} =====")

        logging.info(f"开始:{topic}")


        script = f"output/script_{i}.txt"

        srt = f"output/sub_{i}.srt"

        wav = f"output/voice_{i}.wav"

        video = f"output/final_{i}.mp4"


        if not gen_script(topic, script): continue

        gen_subtitle(script, srt)

        if not gen_voice(script, wav): continue

        if not make_video("assets/imgs", wav, "assets/bgm.mp3", srt, video): continue

        publish(video, topic)

        logging.info(f"完成:{topic}")


    print("\n✅ 全部视频自动制作 + 发布完成!")

    logging.info("=== 全部任务完成 ===")


if __name__ == "__main__":

    run()

```


---


## 3. 开机自启动脚本 `D:\video_pipeline\start_pipeline.bat`

```bat

@echo off

chcp 65001

echo ======================================

echo  AI视频全自动流水线 开机自启动版本

echo ======================================


:: 启动 CosyVoice3 后台服务

start /min cmd /k "cd /d D:\CosyVoice && conda activate cosyvoice && python app.py --host 127.0.0.1 --port 7860 --device cuda"


:: 等待15秒让TTS服务启动

timeout /t 15 /nobreak


:: 运行总流水线

cd /d D:\video_pipeline

python run_pipeline.py


echo.

echo ✅ 全部执行完成

pause

```


---


# 四、设置开机自启(10秒完成)

1. 按 `Win + R`

2. 输入 `shell:startup` 打开启动文件夹

3. 把 `start_pipeline.bat` 快捷方式放进去


✅ **完成!电脑开机 → 全自动运行整个流水线**


---


# 五、运行效果(无人值守)

- 开机自动启动 CosyVoice3

- 自动生成3条科技视频

- 自动配音、自动字幕、自动合成

- 自动发布到抖音/西瓜/B站

- 全部日志保存在 `output/`


---


# 六、我可以直接给你打包

我可以把以上所有文件打包成 **一键解压即用的完整包**,你只需要:

1. 解压到 D 盘

2. 填入你的通义千问API

3. 设置开机启动


**就能实现:开机 → 全自动AI视频工厂**


需要我给你打包吗。



共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]