Python之抖音快手代码舞--字符舞

先上效果,视频敬上:

字符舞:

代码舞

源代码:

video_2_code_video.py

import argparse
import os
import cv2
import subprocess
from cv2 import VideoWriter_fourcc
from PIL import Image, ImageFont, ImageDraw

# 命令行输入参数处理
# aparser = argparse.ArgumentParser()
# aparser.add_argument('file')
# aparser.add_argument('-o','--output')
# aparser.add_argument('-f','--fps',type = float, default = 24)#帧
# aparser.add_argument('-s','--save',type = bool, nargs='?', default = False, const = True)
# 是否保留Cache文件,默认不保存

class Video2CodeVideo:
    def __init__(self):
        self.config_dict = {
   
            # 原视频文件
            "input_file": "video/test.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"][int(gray / unit)]

    # 第一步从函数,将txt转换为图片
    # 调用栈:video_2_txt_jpg -> txt_2_image -> rgb_2_char
    def txt_2_image(self, file_name):
        im = Image.open(file_name).convert('RGB')
        # gif拆分后的图像,需要转换,否则报错,由于gif分割后保存的是索引颜色
        raw_width = im.width
        raw_height = im.height
        width = int(raw_width / 6)
        height = int(raw_height / 15)
        im = im.resize((width, height), Image.NEAREST)

        txt = ""
        
  • 28
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 21
    评论
Python爬虫用于从网站上抓取数据,抖音作为视频分享平台,其API是封闭的,直接爬取公开网页内容通常是可行的,但涉及到用户隐私、版权和反爬机制,不建议用于未经许可的数据抓取。如果你对学习Python爬虫技术,尤其是针对公开网页,可以开始了解如何使用requests、BeautifulSoup、Scrapy等库进行基础的HTML解析。 以下是一个简单的Python爬虫示例,演示如何使用`requests`和`BeautifulSoup`获取抖音主页的部分信息: ```python import requests from bs4 import BeautifulSoup def get_douyin_homepage(): url = 'https://www.douyin.com/' # 抖音主页URL headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } # 设置请求头模拟浏览器 response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 这里通常会提取页面上的特定元素,例如视频列表 video_blocks = soup.select('.video-list .item') # 假设是这个类名 for block in video_blocks: title = block.select_one('.title').text # 获取标题 thumbnail = block.select_one('.cover')['src'] # 获取缩略图URL print(f'Title: {title}\nThumbnail: {thumbnail}\n---') # 调用函数 get_douyin_homepage() ``` 注意: 1. 这只是一个基本示例,实际爬取可能需要处理分页、动态加载内容(如JavaScript渲染)等复杂情况。 2. 实际使用时,请遵守抖音的使用条款和robots.txt协议,不要频繁请求或造成服务器压力。 3. 抓取个人数据或敏感信息时要特别谨慎,尊重用户隐私和法律法规。
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BugMiaowu2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值