最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python抖音快手代碼舞(字符舞)的實現(xiàn)方法

 更新時間:2021年02月07日 15:14:29   作者:一個超會寫B(tài)ug的安太狼  
這篇文章主要給大家介紹了關(guān)于Python抖音快手代碼舞的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

先上效果,視頻敬上:

字符舞:

代碼舞

源代碼:

video_2_code_video.py

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

# 命令行輸入?yún)?shù)處理
# 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--保留,F(xiàn)alse--不保留
  "save_cache_flag": False,
  # 使用使用的字符集
  "ascii_char_list": list("01B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:oa+>!:+. "),
 }

 # 第一步從函數(shù),將像素轉(zhuǎn)換為字符
 # 調(diào)用棧: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)]

 # 第一步從函數(shù),將txt轉(zhuǎn)換為圖片
 # 調(diào)用棧: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拆分后的圖像,需要轉(zhuǎn)換,否則報錯,由于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 = ""
 colors = []
 for i in range(height):
  for j in range(width):
  pixel = im.getpixel((j, i))
  colors.append((pixel[0], pixel[1], pixel[2]))
  if (len(pixel) == 4):
   txt += self.rgb_2_char(pixel[0], pixel[1], pixel[2], pixel[3])
  else:
   txt += self.rgb_2_char(pixel[0], pixel[1], pixel[2])
  txt += '\n'
  colors.append((255, 255, 255))

 im_txt = Image.new("RGB", (raw_width, raw_height), (255, 255, 255))
 dr = ImageDraw.Draw(im_txt)
 # font = ImageFont.truetype(os.path.join("fonts","漢儀楷體簡.ttf"),18)
 font = ImageFont.load_default().font
 x = y = 0
 # 獲取字體的寬高
 font_w, font_h = font.getsize(txt[1])
 font_h *= 1.37 # 調(diào)整后更佳
 # ImageDraw為每個ascii碼進行上色
 for i in range(len(txt)):
  if (txt[i] == '\n'):
  x += font_h
  y = -font_w
  # self, xy, text, fill = None, font = None, anchor = None,
  # *args, ** kwargs
  dr.text((y, x), txt[i], fill=colors[i])
  # dr.text((y, x), txt[i], font=font, fill=colors[i])
  y += font_w

 name = file_name
 # print(name + ' changed')
 im_txt.save(name)


 # 第一步,將原視頻轉(zhuǎn)成字符圖片
 # 調(diào)用棧:video_2_txt_jpg -> txt_2_image -> rgb_2_char
 def video_2_txt_jpg(self, file_name):
 vc = cv2.VideoCapture(file_name)
 c = 1
 if vc.isOpened():
  r, frame = vc.read()
  if not os.path.exists(self.config_dict["cache_dir"]):
  os.mkdir(self.config_dict["cache_dir"])
  os.chdir(self.config_dict["cache_dir"])
 else:
  r = False
 while r:
  cv2.imwrite(str(c) + '.jpg', frame)
  self.txt_2_image(str(c) + '.jpg') # 同時轉(zhuǎn)換為ascii圖
  r, frame = vc.read()
  c += 1
 os.chdir('..')
 return vc

 # 第二步,將字符圖片合成新視頻
 def txt_jpg_2_video(self, outfile_name, fps):
 fourcc = VideoWriter_fourcc(*"MJPG")

 images = os.listdir(self.config_dict["cache_dir"])
 im = Image.open(self.config_dict["cache_dir"] + '/' + images[0])
 vw = cv2.VideoWriter(outfile_name + '.avi', fourcc, fps, im.size)

 os.chdir(self.config_dict["cache_dir"])
 for image in range(len(images)):
  # Image.open(str(image)+'.jpg').convert("RGB").save(str(image)+'.jpg')
  frame = cv2.imread(str(image + 1) + '.jpg')
  vw.write(frame)
  # print(str(image + 1) + '.jpg' + ' finished')
 os.chdir('..')
 vw.release()

 # 第三步,從原視頻中提取出背景音樂
 def video_extract_mp3(self, file_name):
 outfile_name = file_name.split('.')[0] + '.mp3'
 subprocess.call('ffmpeg -i ' + file_name + ' -f mp3 -y ' + outfile_name, shell=True)

 # 第四步,將背景音樂添加到新視頻中
 def video_add_mp3(self, file_name, mp3_file):
 outfile_name = file_name.split('.')[0] + '-txt.mp4'
 subprocess.call('ffmpeg -i ' + file_name + ' -i ' + mp3_file + ' -strict -2 -f mp4 -y ' + outfile_name, shell=True)

 # 第五步,如果沒配置保留則清除過程文件
 def clean_cache_while_need(self):
 # 為了清晰+代碼比較短,直接寫成內(nèi)部函數(shù)
 def remove_cache_dir(path):
  if os.path.exists(path):
  if os.path.isdir(path):
   dirs = os.listdir(path)
   for d in dirs:
   if os.path.isdir(path + '/' + d):
    remove_cache_dir(path + '/' + d)
   elif os.path.isfile(path + '/' + d):
    os.remove(path + '/' + d)
   os.rmdir(path)
   return
  elif os.path.isfile(path):
   os.remove(path)
  return
 # 為了清晰+代碼比較短,直接寫成內(nèi)部函數(shù)
 def delete_middle_media_file():
  os.remove(self.config_dict["input_file"].split('.')[0] + '.mp3')
  os.remove(self.config_dict["input_file"].split('.')[0] + '.avi')
 # 如果沒配置保留則清除過程文件
 if not self.config_dict["save_cache_flag"]:
  remove_cache_dir(self.config_dict["cache_dir"])
  delete_middle_media_file()

 # 程序主要邏輯
 def main_logic(self):
 # 第一步,將原視頻轉(zhuǎn)成字符圖片
 vc = self.video_2_txt_jpg(self.config_dict["input_file"])
 # 獲取原視頻幀率
 fps = vc.get(cv2.CAP_PROP_FPS)
 # print(fps)
 vc.release()
 # 第二步,將字符圖片合成新視頻
 self.txt_jpg_2_video(self.config_dict["input_file"].split('.')[0], fps)
 print(self.config_dict["input_file"], self.config_dict["input_file"].split('.')[0] + '.mp3')
 # 第三步,從原視頻中提取出背景音樂
 self.video_extract_mp3(self.config_dict["input_file"])
 # 第四步,將背景音樂添加到新視頻中
 self.video_add_mp3(self.config_dict["input_file"].split('.')[0] + '.avi', self.config_dict["input_file"].split('.')[0] + '.mp3')
 # 第五步,如果沒配置保留則清除過程文件
 self.clean_cache_while_need()

if __name__ == '__main__':
 obj = Video2CodeVideo()
 obj.main_logic()

運行環(huán)境:

操作系統(tǒng):win10

版本:Python 3.8.4

依賴庫:pip install opencv-python pillow

管理員權(quán)限安裝,我的已安裝過,顯示這樣:

依賴應(yīng)用: ffpmeg(下載直接解壓、將bin目錄加到PATH環(huán)境變量)

小白式運行(大佬請裝瞎):

將上面的源代碼命名video_2_code_video.py,在同一目錄下新建文件夾video:

在video中放入要轉(zhuǎn)換的原視頻,命名test.mp4:

打開Python3.8

運行video_2_code_video.py,如下圖顯示表示正在運行:

會產(chǎn)生一些中間文件諸如:

經(jīng)過漫長的等待,終于得償所愿:

test-txt.mp4就是所要的代碼舞啦:

總結(jié) 

到此這篇關(guān)于Python抖音快手代碼舞實現(xiàn)的文章就介紹到這了,更多相關(guān)Python抖音快手代碼舞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實現(xiàn)文件按照日期命名的方法

    Python實現(xiàn)文件按照日期命名的方法

    這篇文章主要介紹了Python實現(xiàn)文件按照日期命名的方法,涉及Python針對文件的遍歷、讀寫及時間操作相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • 淺談Selenium 控制瀏覽器的常用方法

    淺談Selenium 控制瀏覽器的常用方法

    這篇文章主要介紹了淺談Selenium 控制瀏覽器的常用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 使用selenium和pyquery爬取京東商品列表過程解析

    使用selenium和pyquery爬取京東商品列表過程解析

    這篇文章主要介紹了使用selenium和pyquery爬取京東商品列表過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • 基于Python的身份證驗證識別和數(shù)據(jù)處理詳解

    基于Python的身份證驗證識別和數(shù)據(jù)處理詳解

    這篇文章主要介紹了基于Python的身份證驗證識別和數(shù)據(jù)處理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 基于Python實現(xiàn)一鍵獲取電腦瀏覽器的賬號密碼

    基于Python實現(xiàn)一鍵獲取電腦瀏覽器的賬號密碼

    發(fā)現(xiàn)很多人在學校圖書館喜歡用電腦占座,而且出去的時候經(jīng)常不鎖屏,為了讓大家養(yǎng)成良好的習慣,本文將分享一個小程序,可以快速獲取你存儲在電腦瀏覽器中的所有賬號和密碼,感興趣的可以了解一下
    2022-05-05
  • windows系統(tǒng)上通過whl文件安裝triton模塊的簡單步驟

    windows系統(tǒng)上通過whl文件安裝triton模塊的簡單步驟

    這篇文章主要介紹了在Windows系統(tǒng)中通過.whl文件安裝Triton的步驟,包括確認系統(tǒng)環(huán)境、下載合適的.whl文件、使用pip安裝、驗證安裝、使用Triton以及解決潛在問題,需要的朋友可以參考下
    2025-01-01
  • Python類的動態(tài)綁定實現(xiàn)原理

    Python類的動態(tài)綁定實現(xiàn)原理

    這篇文章主要介紹了Python類的動態(tài)綁定實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • Python3自動安裝第三方庫,跟pip說再見

    Python3自動安裝第三方庫,跟pip說再見

    很多朋友私信小編Python安裝第三方庫安裝技巧,在這就不一一回復(fù)大家了,今天小編給大家分享一篇教程關(guān)于Python自動安裝第三方庫的小技巧,本文以安裝plotly為例給大家詳細講解,感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • 分享8點超級有用的Python編程建議(推薦)

    分享8點超級有用的Python編程建議(推薦)

    這篇文章主要介紹了分享8點超級有用的Python編程建議(推薦),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-10-10
  • pytorch 模型的train模式與eval模式實例

    pytorch 模型的train模式與eval模式實例

    今天小編就為大家分享一篇pytorch 模型的train模式與eval模式實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02

最新評論

洪泽县| 商水县| 博客| 资兴市| 香格里拉县| 新绛县| 定兴县| 蓬莱市| 鄱阳县| 洛宁县| 凤冈县| 长顺县| 高雄市| 汤原县| 南靖县| 临高县| 祁阳县| 金坛市| 南汇区| 潮州市| 九寨沟县| 略阳县| 武宁县| 咸丰县| 高密市| 郓城县| 突泉县| 江华| 长海县| 佛学| 大兴区| 米泉市| 鄯善县| 南岸区| 开封县| 清水县| 汉川市| 清河县| 项城市| 马关县| 乌拉特前旗|