提问人:mahmudsajib 提问时间:11/12/2023 最后编辑:mahmudsajib 更新时间:11/12/2023 访问量:3971
在 Django 中检查 base64 视频文件中的音频和视频编解码器
Check audio and video codec from base64 video file in Django
问:
我目前正在处理一个 Django 项目,我需要检查 base64 编码视频文件的音频和视频编解码器。为了实现这一点,我实现了一个函数,将 base64 字符串解码为二进制数据,然后尝试使用 MoviePy 加载视频剪辑。但是,我遇到了 AttributeError: '_io.BytesIO“对象在尝试运行代码时没有属性”endswith”。
以下是代码的相关部分:
import base64
from io import BytesIO
from moviepy.editor import VideoFileClip
def get_video_codec_info(base64_data):
# Decode base64 string into binary data
_format, _file_str = base64_data.split(";base64,")
binary_data = base64.b64decode(_file_str)
# Load the video clip using MoviePy
clip = VideoFileClip(BytesIO(binary_data))
# Get information about the video codec
codec_info = {
'video_codec': clip.video_codec,
'audio_codec': clip.audio_codec,
}
return codec_info
该错误发生在该行,似乎与 BytesIO 的使用有关。我试图找到一个解决方案,但我现在被困住了。clip = VideoFileClip(BytesIO(binary_data))
关于如何解决此问题的任何建议或检查 Django 中 base64 编码视频文件的音频和视频编解码器的替代方法将不胜感激。谢谢!
答:
经过进一步调查和考虑,我找到了该问题的替代解决方案。该问题源于原始实现中对 with 的使用。为了解决这个问题,我建议采用一种不同的方法,将二进制数据保存到临时文件中,然后用于收集视频信息。BytesIO
MoviePy
base64-decoded
ffprobe
codec
import base64
import tempfile
import subprocess
import os
import json
def get_video_codec_info(base64_data):
codec_names = []
# Decode base64 string into binary data
_, _file_str = base64_data.split(";base64,")
binary_data = base64.b64decode(_file_str)
# Save binary data to a temporary file
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(binary_data)
temp_filename = temp_file.name
try:
# Run ffmpeg command to get video codec information
command = ['ffprobe', '-v', 'error', '-show_entries', 'stream=codec_name:stream_tags=language', '-of', 'json', temp_filename]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
# Try to parse the JSON output
data = json.loads(result.stdout)
# Iterate through streams and print information
for stream in data['streams']:
codec_names.append(stream.get('codec_name'))
finally:
# Clean up: delete the temporary file
os.remove(temp_filename)
return codec_names
此方法使用 ffprobe 直接从视频文件中提取编解码器信息,绕过 BytesIO 和 MoviePy 遇到的问题。确保在您的系统上安装 FFmpeg,因为它是 ffprobe 的先决条件。
您遇到的错误是因为 MoviePy 需要文件路径或 URL 作为其参数,但您传递的是对象。若要解决此问题,可以从二进制数据创建一个临时文件,然后将该临时文件的路径提供给 。下面是代码的更新版本:AttributeError: '_io.BytesIO' object has no attribute 'endswith'
VideoFileClip
BytesIO
VideoFileClip
import base64
import tempfile
from moviepy.editor import VideoFileClip
def get_video_codec_info(base64_data):
_format, _file_str = base64_data.split(";base64,")
binary_data = base64.b64decode(_file_str)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
temp_file.write(binary_data)
temp_file.close()
try:
clip = VideoFileClip(temp_file.name)
codec_info = {
'video_codec': clip.video_codec,
'audio_codec': clip.audio_codec,
}
finally:
temp_file.unlink(temp_file.name)
return codec_info
评论