不二如是 发表于 2022-5-30 08:58:41

Python 版本代码舞~猛男爱你

本帖最后由 不二如是 于 2022-5-31 09:11 编辑

在线视频:

https://www.bilibili.com/video/BV1LT4y1B7KC

static/image/hrline/1.gif



**** Hidden Message *****

上面视频中通过 JavaScript 实现用字符来跳舞,这次我们用 Python 来实现。

通常处理图片/音视/视频都是 pillow+opencv+ffmpeg 的组合。

主要分为以下几大步:


[*]将原视频转成字符图片
[*]将字符图片合成新视频
[*]从原视频中提取出背景音乐
[*]将背景音乐添加到新视频中
[*]如果没配置保留则清除过程文件

def __init__(self):
      self.config_dict = {
            # 原视频文件
            "input_file": "resource.mp4",
            # 中间文件存放目录
            "cache_dir": "cache",
            # 是否保留过程文件。True--保留,False--不保留
            "save_cache_flag": False,
            # 使用使用的字符集
            "ascii_char_list": list("01B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:oa+>!:+. "),
      }

    # 第一步从函数,将像素转换为字符
    # 调用栈:video_2_txt_jpg -> txt_2_image -> rgb_2_char
    def rgb_2_char(self, r, g, b, alpha=256):
      if alpha == 0:
            return ''
      length = len(self.config_dict["ascii_char_list"])
      gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
      unit = (256.0 + 1) / length
      return self.config_dict["ascii_char_list"]
每一步中还有很多细化操作,注释见源码:

如果 OpenCV 安不上,可以试试国内清华的镜像:

pip install opencv-contrib-python -i https://pypi.mirrors.ustc.edu.cn/simple/

关于 OpenCV 的玩法可以看:安装 OpenCV 模块【人工智能】

bool想学C 发表于 2022-5-30 16:34:42

Python 好好玩

hrpzcf 发表于 2022-5-30 17:19:43

Python 好好玩

我去年写了一个类似的模块:https://fishc.com.cn/thread-202561-1-1.html{:10_250:}

tiangtian 发表于 2022-5-30 17:30:02

Python 好好玩

1molHF 发表于 2022-5-30 18:56:48

Python 我最爱

hornwong 发表于 2022-5-30 19:20:31

Python 好好玩

中方天际 发表于 2022-5-30 19:35:41

Python 好好玩

中方天际 发表于 2022-5-30 19:44:32

unindent does not match any outer indentation level
第十五行

不二如是 发表于 2022-5-31 09:28:29

Python 好好玩

GX123 发表于 2022-5-31 11:02:59

Python 好好玩

fxl881116 发表于 2022-5-31 15:08:54

Python 好好玩

pk大魔王 发表于 2022-5-31 17:18:14

Python 好好玩

ENHEN 发表于 2022-5-31 17:21:53

Python 好好玩

lucaswong 发表于 2022-5-31 17:51:46

Python 好好玩

Noviem 发表于 2022-5-31 22:10:28

Python 好好玩

刘华峰 发表于 2022-6-23 14:31:01

Python 好好玩

xx153398 发表于 2022-6-24 11:17:36

Python 好好玩

Nate_2020 发表于 2022-6-24 11:37:01

Python 好好玩

jap777 发表于 2022-7-11 10:48:26

Python 好好玩

duoyong 发表于 2022-7-16 11:57:20

Python 好好玩
页: [1] 2
查看完整版本: Python 版本代码舞~猛男爱你