Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.09k stars 1.51k forks source link

webm format video with transparent background #2082

Open ingale726 opened 6 months ago

ingale726 commented 6 months ago

I have several consecutive PNG images with a transparent background, and a WAV file. How do I use moviepy to combine them into a webm video with a transparent background?

ingale726 commented 6 months ago

@zodman @chunder @abramhindle @shawwn

ingale726 commented 6 months ago

this is my code:

import os
from moviepy.editor import ImageSequenceClip, AudioFileClip
image_folder_path = 'd:/desktop/webm/'
audio_file_path = 'd:/desktop/3.wav'
image_files = sorted([f for f in os.listdir(image_folder_path) if f.endswith('.png')])
image_clip = ImageSequenceClip([os.path.join(image_folder_path, f) for f in image_files], fps=24)
audio_clip = AudioFileClip(audio_file_path)
video = image_clip.set_opacity(0)
video = video.set_audio(audio_clip)
video.write_videofile("d:/desktop/output.webm", codec="libvpx", audio_codec="libvorbis")

The above code can generate webm format video, but its background is opaque, whereas every png image I have has a transparent background

SohamTilekar commented 6 months ago

Bro I Faced this Issue Already I Know What to Do.

The ImageSequenceClip Has an Attribute ismask Which Specify the Input Images are transparent or not. the Duration Should be audio_clip.duration For the Audio And Video Sync I think there is No Need For the Specifing the codec, etc.

import os
from moviepy.editor import ImageSequenceClip, AudioFileClip

image_folder_path = 'path/to/your/png_images/'
audio_file_path = 'path/to/your/audio.wav'

image_files = sorted([f for f in os.listdir(image_folder_path) if f.endswith('.png')])

audio_clip = AudioFileClip(audio_file_path)

image_clip = ImageSequenceClip([os.path.join(image_folder_path, f) for f in image_files], ismask=True, durations=audio_clip.duration)

video = image_clip.set_audio(audio_clip)

video.write_videofile("d:/desktop/output.webm", codec="libvpx", audio_codec="libvorbis")
SohamTilekar commented 6 months ago

Try to use the Following Codec='libvpx-vp9'

ingale726 commented 6 months ago

Try to use the Following Codec='libvpx-vp9'

Hello bro, when I use your code, the generated video picture is weird, I don't know what is going on

import os
from moviepy.editor import ImageSequenceClip, AudioFileClip
image_folder_path = 'd:/desktop/webm/'
audio_file_path = 'd:/desktop/3.wav'
image_files = sorted([f for f in os.listdir(image_folder_path) if f.endswith('.png')])
audio_clip = AudioFileClip(audio_file_path)
image_clip = ImageSequenceClip([os.path.join(image_folder_path, f) for f in image_files], ismask=True,fps=25)
video = image_clip.set_audio(audio_clip)
video.write_videofile("d:/desktop/output.webm", codec="libvpx-vp9", audio_codec="libvorbis")

Snipaste_2023-12-21_17-17-53 The original video was of a woman's upper body

SohamTilekar commented 6 months ago

Does it Work With Normal Codec ['libvpx'].

Could You Show me Your Code Part Where You read the Video Transformation and the saving

SohamTilekar commented 6 months ago

Try to run code without providing codec.

SohamTilekar commented 6 months ago

Use the 'ffmpeg -codecs' command for getting the list of the codecs supported by the ffmpeg choose any one of it.

ingale726 commented 6 months ago

@SohamTilekar

import os
from moviepy.editor import ImageSequenceClip, AudioFileClip
image_folder_path = 'd:/desktop/webm/'
audio_file_path = 'd:/desktop/3.wav'
image_files = sorted([f for f in os.listdir(image_folder_path) if f.endswith('.png')])
audio_clip = AudioFileClip(audio_file_path)
image_clip = ImageSequenceClip([os.path.join(image_folder_path, f) for f in image_files], ismask=True,fps=25)
video = image_clip.set_audio(audio_clip)
video.write_videofile("d:/desktop/output.webm", codec="libvpx-vp9", audio_codec="libvorbis")

This is my code, and when I use 'libvpx', the effect is the same My image is PNG format, 4 channels, the background is transparent

env: win11 python3.9 moviepy 1.0.3

ffmpeg:

ffmpeg version N-112960-g96f646cdc2-20231212 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 13.2.0 (crosstool-NG 1.25.0.232_c175b21)
configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libharfbuzz --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libaribcaption --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libvpl --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --enable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20231212
libavutil      58. 32.100 / 58. 32.100
libavcodec     60. 35.100 / 60. 35.100
libavformat    60. 18.100 / 60. 18.100
libavdevice    60.  4.100 / 60.  4.100
libavfilter     9. 14.100 /  9. 14.100
libswscale      7.  6.100 /  7.  6.100
libswresample   4. 13.100 /  4. 13.100
libpostproc    57.  4.100 / 57.  4.100
SohamTilekar commented 6 months ago

output.webm It Work on my Laptop i use the i use the fps=4 Because i do not have that many images

ingale726 commented 6 months ago

the result is same

---- Replied Message ---- | From | Soham @.> | | Date | 12/21/2023 17:00 | | To | @.> | | Cc | @.>@.> | | Subject | Re: [Zulko/moviepy] webm format video with transparent background (Issue #2082) |

Try to use the Following Codec='libvpx-vp9'

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

SohamTilekar commented 6 months ago

It Workes when i set the ismask = False But Not working with the ismask = True

Good0007 commented 6 months ago

Moviepy has issues processing transparent channel videos, which can be achieved using the ffmpeg command for faster speed and better results.

ingale726 commented 6 months ago

Moviepy has issues processing transparent channel videos, which can be achieved using the ffmpeg command for faster speed and better results.

When I use ffmpeg, it can be successfully generated, but it is too time consuming, whether there are any parameters to speed it up

Good0007 commented 6 months ago

Moviepy has issues processing transparent channel videos, which can be achieved using the ffmpeg command for faster speed and better results.

When I use ffmpeg, it can be successfully generated, but it is too time consuming, whether there are any parameters to speed it up

What is the duration of the video you generated? How long does it take? This is the result of my test on Intel I9 13900K:

(base) [root@localhost er_nerf_lz-BOc57IhkymAAtd5TNl]# ffmpeg -i ./pngs/image%06d.png -i ./audio/f3ab7be97c98a6abca9723a8c17cf769.wav -c:v qtrle -r 24 -crf 22 -map 0:v -map 1:a out.mov -y
ffmpeg version 6.1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)
  configuration: --prefix=/root/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags=-L/root/ffmpeg_build/lib --extra-libs=-lpthread --extra-libs=-lm --bindir=/root/bin --enable-gpl --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-cuvid --enable-cuda --enable-nvenc
  libavutil      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   4. 12.100 /  4. 12.100
  libpostproc    57.  3.100 / 57.  3.100
Input #0, image2, from './pngs/image%06d.png':
  Duration: 00:00:46.80, start: 0.000000, bitrate: N/A
  Stream #0:0: Video: png, rgba(pc, gbr/unknown/unknown), 720x1280, 25 fps, 25 tbr, 25 tbn
[aist#1:0/pcm_s16le @ 0x4f1fa80] Guessed Channel Layout: mono
Input #1, wav, from './audio/f3ab7be97c98a6abca9723a8c17cf769.wav':
  Metadata:
    encoder         : Lavf56.40.101
  Duration: 00:00:46.82, bitrate: 256 kb/s
  Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, 1 channels, s16, 256 kb/s
[out#0/mov @ 0x4f15fc0] Codec AVOption crf (Select the quality for constant quality mode) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> qtrle (native))
  Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[in#0/image2 @ 0x4ee8a40] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
Output #0, mov, to 'out.mov':
  Metadata:
    encoder         : Lavf60.16.100
  Stream #0:0: Video: qtrle (rle  / 0x20656C72), argb(pc, gbr/unknown/unknown, progressive), 720x1280, q=2-31, 200 kb/s, 24 fps, 12288 tbn
    Metadata:
      encoder         : Lavc60.31.102 qtrle
  Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 69 kb/s
    Metadata:
      encoder         : Lavc60.31.102 aac
[out#0/mov @ 0x4f15fc0] video:790729kB audio:364kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.002149%
frame= 1125 fps= 99 q=-0.0 Lsize=  791110kB time=00:00:46.83 bitrate=138379.5kbits/s dup=0 drop=45 speed=4.14x    
[aac @ 0x4f60180] Qavg: 37831.180
(base) [root@localhost er_nerf_lz-BOc57IhkymAAtd5TNl]# ls -alh out.mov
-rw-r--r-- 1 root root 773M Dec 22 15:32 out.mov
Good0007 commented 6 months ago

output.webm It Work on my Laptop i use the i use the fps=4 Because i do not have that many images I suggest using qtrle for encoding, which is faster but has a larger file size.

ingale726 commented 6 months ago

output.webm It Work on my Laptop i use the i use the fps=4 Because i do not have that many images I suggest using qtrle for encoding, which is faster but has a larger file size.

ok,i will try