Как превратить изображение A + аудио + A в видео A с теми же атрибутами, что и у видео B, чтобы их можно было объединитьPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как превратить изображение A + аудио + A в видео A с теми же атрибутами, что и у видео B, чтобы их можно было объединить

Сообщение Anonymous »

У меня есть изображение A, оно в формате PNG
У меня есть аудио A, оно в формате MP3
Я есть видео B, которое может быть любого формата, разрешения, кодировки, звука и т. д.
Теперь я хочу превратить изображение A + аудио A в видео A с точными атрибутами видео B, чтобы можно было объединить их без кодирования
Вот мой код, который не работает. Я использую Python и последнюю версию FFMPEG с большинством кодеков и функций
def get_video_info(video_path):
cmd = [
FFPROBE_PATH,
'-v', 'quiet',
'-print_format', 'json',
'-show_format',
'-show_streams',
video_path
]
result = subprocess.run(cmd, capture_output=True, text=True)
info = json.loads(result.stdout)

video_stream = next((s for s in info['streams'] if s['codec_type'] == 'video'), None)
audio_stream = next((s for s in info['streams'] if s['codec_type'] == 'audio'), None)

if not video_stream or not audio_stream:
logging.error("Failed to find video or audio stream")
return None

return {
'width': int(video_stream.get('width', 0)),
'height': int(video_stream.get('height', 0)),
'fps': eval(video_stream.get('r_frame_rate', '30/1')),
'video_codec': video_stream.get('codec_name', ''),
'audio_codec': audio_stream.get('codec_name', ''),
'audio_sample_rate': int(audio_stream.get('sample_rate', 0)),
'audio_channels': audio_stream.get('channels', 0),
'pixel_format': video_stream.get('pix_fmt', ''),
'color_space': video_stream.get('color_space', ''),
'color_transfer': video_stream.get('color_transfer', ''),
'color_primaries': video_stream.get('color_primaries', ''),
}

def create_thumbnail_video(thumbnail_url, video_id, video_info, duration=40):
output_dir = os.path.abspath('downloads')
output_path = os.path.join(output_dir, f"{video_id}_thumbnail.mp4")
audio_path = os.path.abspath("a.mp3")

try:
# 1. Download and process the image
logging.info("Downloading thumbnail image...")
response = requests.get(thumbnail_url, timeout=30)
response.raise_for_status()

# Save the original image
original_image_path = os.path.join(output_dir, f'{video_id}_original_image.png')
with open(original_image_path, 'wb') as f:
f.write(response.content)

# 2. Open and resize the image
image = Image.open(original_image_path)
width, height = video_info['width'], video_info['height']
image = image.resize((width, height), Image.LANCZOS)

# Save resized image
resized_image_path = os.path.join(output_dir, f'{video_id}_resized_image.png')
image.save(resized_image_path, 'PNG')

# 3. Get properties of the main video using ffprobe
main_video_path = os.path.join(output_dir, f"{video_id}.mp4") # Assuming main video has .mp4 extension
main_video_info = get_video_info(main_video_path)

if not main_video_info:
raise Exception("Could not get information about the main video.")

# 4. Generate video with matching encoding
logging.info("Generating thumbnail video with matching codec and properties...")

ffmpeg_command = [
FFMPEG_PATH,
'-y',
'-loop', '1',
'-i', resized_image_path,
'-i', audio_path,
'-c:v', main_video_info['video_codec'],
'-c:a', main_video_info['audio_codec'],
'-ar', str(main_video_info['audio_sample_rate']),
'-pix_fmt', main_video_info['pixel_format'],
'-color_range', main_video_info.get('color_range', '1'), # Default to tv/mpeg (limited) if not found
'-colorspace', main_video_info['color_space'],
'-color_trc', main_video_info['color_transfer'],
'-color_primaries', main_video_info['color_primaries'],
'-vf', f"fps={main_video_info['fps']},scale={width}:{height}",
'-shortest',
'-t', str(duration),
output_path
]

subprocess.run(ffmpeg_command, check=True, capture_output=True, text=True)
logging.info(f"Thumbnail video created: {output_path}")

# 5. Clean up intermediate files
for file_path in [original_image_path, resized_image_path]:
if os.path.exists(file_path):
os.remove(file_path)
logging.info(f"Removed intermediate file: {file_path}")

return output_path

except requests.RequestException as e:
logging.error(f"Error downloading thumbnail: {e}")
except subprocess.CalledProcessError as e:
logging.error(f"FFmpeg error: {e.stderr}")
except Exception as e:
logging.error(f"Error in create_thumbnail_video: {e}")
logging.error(traceback.format_exc())

# If we've reached this point, an error occurred
logging.warning("Thumbnail video creation failed. Returning None.")
return None

Вот пример
Сгенерированное видео из изображения и аудио, как показано ниже
General
Complete name : C:\stream\downloads\hqDA0-waGwI_thumbnail.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom/iso2/avc1/mp41)
File size : 1.50 MiB
Duration : 40 s 0 ms
Overall bit rate mode : Variable
Overall bit rate : 315 kb/s
Frame rate : 30.000 FPS
Writing application : Lavf61.9.100

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4
Format settings : CABAC / 4 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference fra : 4 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 40 s 0 ms
Bit rate : 179 kb/s
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 30.000 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.003
Stream size : 875 KiB (57%)
Writing library : x264 core 164
Encoding settings : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=34 / lookahead_threads=5 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=23.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Color range : Limited
Matrix coefficients : BT.709
Codec configuration box : avcC

Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 40 s 0 ms
Source duration : 40 s 23 ms
Source_Duration_LastFrame : -8 ms
Bit rate mode : Variable
Bit rate : 128 kb/s
Channel(s) : 2 channels
Channel layout : L R
Sampling rate : 44.1 kHz
Frame rate : 43.066 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 621 KiB (40%)
Source stream size : 621 KiB (40%)
Default : Yes
Alternate group : 1

Исходное видео используется для получения атрибутов видео
General
Complete name : C:\stream\downloads\hqDA0-waGwI.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom/iso2/avc1/mp41)
File size : 24.9 MiB
Duration : 1 min 36 s
Overall bit rate : 2 171 kb/s
Frame rate : 30.000 FPS
Writing application : Lavf61.9.100

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4
Format settings : CABAC / 3 Ref Frames
Format settings, CABAC : Yes
Format settings, Reference fra : 3 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 1 min 36 s
Bit rate : 2 036 kb/s
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 30.000 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.033
Stream size : 23.3 MiB (94%)
Title : ISO Media file produced by Google Inc.
Color range : Limited
Color primaries : BT.709
Transfer characteristics : BT.709
Matrix coefficients : BT.709
Codec configuration box : avcC

Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 1 min 36 s
Bit rate mode : Constant
Bit rate : 128 kb/s
Channel(s) : 2 channels
Channel layout : L R
Sampling rate : 44.1 kHz
Frame rate : 43.066 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 1.47 MiB (6%)
Title : ISO Media file produced by Google Inc.
Language : English
Default : Yes
Alternate group : 1


Подробнее здесь: https://stackoverflow.com/questions/792 ... butes-of-v
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»