Python Tutorial: Download Bilibili Video, Extract Frames, Perform Human Segmentation with Baidu AI, Generate Word Cloud, and Compose Final Video
This article demonstrates how to use Python to download a Bilibili video, extract frames with OpenCV, perform human segmentation via Baidu AI, generate a word‑cloud animation using MoviePy, and finally compose the processed clips into a complete video with added audio.
This guide walks through a complete Python workflow for processing a Bilibili dance video, including downloading, frame extraction, AI‑based human segmentation, word‑cloud creation, and final video composition with audio.
1. Install required libraries
<code>import os</code><code>import time</code><code>libs = {"lxml","requests","pandas","numpy","you-get","opencv-python","pandas","fake_useragent","matplotlib","moviepy"}</code><code>try:</code><code> for lib in libs:</code><code> os.system(f"pip3 install -i https://pypi.doubanio.com/simple/ {lib}")</code><code> print(lib+"下载成功")</code><code>except:</code><code> print("下载失败")</code>2. Import modules
<code>import os</code><code>import re</code><code>import cv2</code><code>import jieba</code><code>import requests</code><code>import moviepy</code><code>import pandas as pd</code><code>import numpy as np</code><code>from PIL import Image</code><code>from lxml import etree</code><code>from wordcloud import WordCloud</code><code>import matplotlib.pyplot as plt</code><code>from fake_useragent import UserAgent</code>3. Download the video using you-get (the original Bilibili URL is provided in the article).
4. Split video into frames with OpenCV:
<code># -*- coding:utf-8 -*-</code><code># @Author : 北山啦</code><code># @File : 视频分割.py</code><code>import cv2</code><code>cap = cv2.VideoCapture(r"无价之姐~让我乘风破浪~~~.flv")</code><code>while 1:</code><code> ret, frame = cap.read()</code><code> if ret:</code><code> cv2.imwrite(f"./pictures/img_{num}.jpg", frame)</code><code> else:</code><code> break</code><code>cap.release() # 释放资源</code>5. Human segmentation using Baidu AI (Python SDK):
<code>import cv2</code><code>import base64</code><code>import numpy as np</code><code>import os</code><code>from aip import AipBodyAnalysis</code><code>APP_ID = '******'</code><code>API_KEY = '*******************'</code><code>SECRET_KEY = '********************'</code><code>client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)</code><code>path = './mask_img/'</code><code>img_files = os.listdir('./pictures')</code><code>for num in range(1, len(img_files)+1):</code><code> img = f'./pictures/img_{num}.jpg'</code><code> img1 = cv2.imread(img)</code><code> height, width, _ = img1.shape</code><code> with open(img, 'rb') as fp:</code><code> img_info = fp.read()</code><code> seg_res = client.bodySeg(img_info)</code><code> labelmap = base64.b64decode(seg_res['labelmap'])</code><code> nparr = np.frombuffer(labelmap, np.uint8)</code><code> labelimg = cv2.imdecode(nparr, 1)</code><code> labelimg = cv2.resize(labelimg, (width, height), interpolation=cv2.INTER_NEAREST)</code><code> new_img = np.where(labelimg == 1, 255, labelimg)</code><code> mask_name = path + f'mask_{num}.png'</code><code> cv2.imwrite(mask_name, new_img)</code><code> print(f'======== 第{num}张图像分割完成 ========')</code>6. Generate a word‑cloud animation from the extracted comments:
<code># -*- coding:utf-8 -*-</code><code># @Author : 北山啦</code><code># @File : 跳舞词云图生成.py</code><code>from wordcloud import WordCloud</code><code>import collections</code><code>import jieba</code><code>import re</code><code>from PIL import Image</code><code>import matplotlib.pyplot as plt</code><code>import numpy as np</code><code>with open('barrages.txt') as f:</code><code> data = f.read()</code><code>jieba.load_userdict("./词云图//add.txt")</code><code>new_data = "/".join(re.findall('[\u4e00-\u9fa5]+', data, re.S))</code><code>seg_list_exact = jieba.cut(new_data, cut_all=True)</code><code>result_list = []</code><code>with open('./词云图/stoplist.txt', encoding='utf-8') as f:</code><code> stop_words = set(f.read().split('\n'))</code><code>for word in seg_list_exact:</code><code> if word not in stop_words and len(word) > 1:</code><code> result_list.append(word)</code><code>word_counts = collections.Counter(result_list)</code><code>mask_ = 255 - np.array(Image.open('./mask_img/mask_1.png'))</code><code>my_cloud = WordCloud(background_color='black', mask=mask_, mode='RGBA', max_words=500, font_path='simhei.ttf').generate_from_frequencies(word_counts)</code><code>plt.figure(figsize=(8,5), dpi=200)</code><code>plt.imshow(my_cloud)</code><code>plt.axis('off')</code><code>my_cloud.to_file('./wordcloud/wordcloud_1.png')</code><code>print(f'======== 第1张词云图生成 ========')</code>7. Compose the final video from the word‑cloud images using OpenCV:
<code># -*- coding:utf-8 -*-</code><code># @Author : 北山啦</code><code># @File : 跳舞词云图生成.py</code><code>import cv2</code><code>import os</code><code>video_dir = 'result.mp4'</code><code>fps = 30</code><code>img_size = (1920, 1080)</code><code>fourcc = cv2.VideoWriter_fourcc('M','P','4','V')</code><code>videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)</code><code>img_files = os.listdir('./wordcloud')</code><code>for i in range(88, 888):</code><code> img_path = f'./wordcloud/wordcloud_{i}.png'</code><code> frame = cv2.imread(img_path)</code><code> frame = cv2.resize(frame, img_size)</code><code> videoWriter.write(frame)</code><code> print(f'======== 按照视频顺序第{i}张图片合进视频 ========')</code><code>videoWriter.release() # 释放资源</code>Finally, add background music with moviepy :
<code># -*- coding:utf-8 -*-</code><code># @Author : 北山啦</code><code>import moviepy.editor as mpy</code><code>my_clip = mpy.VideoFileClip('result.mp4')</code><code>audio_background = mpy.AudioFileClip('song.mp3').subclip(0,25)</code><code>final_clip = my_clip.set_audio(audio_background)</code><code>final_clip.write_videofile('final_video.mp4')</code>The resulting video combines the dance footage, AI‑segmented human masks, and animated word clouds, all synchronized with a background soundtrack.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.