Skip to main content
 首页 » 编程设计

Python之mencoder 不会在不同的文件夹中生成视频

2025年05月04日10www_RR

Python - mencoder 不会在不同的文件夹中生成视频。

我正在使用 Python 调用 mencoder 来拍摄一堆图像并将它们转换成视频。我为此所做的代码是:

def Video(self,X,Y,location):#generate a video from the images 
    location=location.replace('\\','/') 
    name='Slide.avi' 
 
    command = ('C:\Downloads\MPlayer-athlon-svn-34401\mencoder.exe',# location of mencoder 
               'mf://'+location+'/*.png',# location of the images 
               '-mf', 
               'type=png:w='+str(X)+':h='+str(Y)+':fps=25',#size of video and fps 
               '-ovc', 
               'lavc', 
               '-lavcopts', 
               'vcodec=ffv1', 
               '-oac', 
               'copy', 
               '-o', 
               location+'/'+name)#output  
 
   subprocess.check_call(command) 

其中 location 是包含我想要组合成视频的所有图像的文件夹。 现在的问题是,如果我的 python 脚本位于其中也有图像的文件夹中,那么它工作得很好,我得到了一个视频。但如果位置不同,mencoder 只会给我一个空视频。我不明白为什么会这样。如果我的 python 脚本位于该文件夹中,则代码有效,但如果它位于不同的文件夹中,则代码无效。

我已经打开了 mencoder 的详细信息,这就是我在成功时得到的结果:

MEncoder Sherpya-SVN-r34401-4.6.2 (C) 2000-2011 MPlayer Team 
 
Setting PATH to C:\Downloads\MPlayer-athlon-svn-34401\codecs 
 
get_path('codecs.conf') -> 'C:\Users\Main/mplayer/codecs.conf' 
 
Reading optional codecs config file C:\Users\Main/mplayer/codecs.conf: No such file or directory 
 
Reading optional codecs config file /mingw/etc/mplayer/codecs.conf: No such file or directory 
 
Using built-in default codecs.conf. 
 
init_freetype 
 
Using MMX (with tiny bit MMX2) Optimized OnScreenDisplay 
 
get_path('fonts') -> 'C:\Users\Main/mplayer/fonts' 
 
Configuration: --extra-cflags=-I/c/Work/mplayer/live --extra-ldflags=-lws2_32 --prefix=/mingw --enable-static --enable-md5sum --enable-menu --enable-faac --enable-enca --enable-sdl --enable-caca --enable-gl --enable-freetype --enable-png --enable-mng --enable-jpeg --enable-gif --enable-tga --enable-mad --enable-tv --enable-theora --disable-vidix --disable-faac-lavc --disable-inet6 --flavor=athlon 
 
WINSOCK2 init: 0 
 
STREAM: [mf] mf://C:/Users/Main/Desktop/temp/*.png 
 
STREAM: Description: Multiple files input 
 
STREAM: Author: Benjamin Zores, Albeu 
 
STREAM: Comment:  
 
success: format: 16  data: 0x0 - 0x0 
 
MF file format detected. 
 
[mf] search expr: C:/Users/Main/Desktop/temp/*.png 
 
[mf] number of files: 50 (200) 
 
==> Found video stream: 0 
 
VIDEO:  [MPNG]  560x560  24bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s) 
 
[V] filefmt:16  fourcc:0x474E504D  size:560x560  fps:25.000  ftime:=0.0400 
 
WINSOCK2 init: 0 
 
WINSOCK2 init: 0 
 
[file] File size is 0 bytes 
 
STREAM: [file] C:/Users/Main/Desktop/temp/Slide.avi 
 
STREAM: Description: File 
 
STREAM: Author: Albeu 
 
STREAM: Comment: based on the code from ??? (probably Arpi) 
 
Opening video filter: [expand osd=1] 
 
Expand: -1 x -1, -1 ; -1, osd: 1, aspect: 0.000000, round: 1 
 
========================================================================== 
 
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family 
 
[png @ 01235100]err{or,}_recognition separate: 2; 1 
 
[png @ 01235100]err{or,}_recognition combined: 2; 196609 
 
INFO: libavcodec init OK! 
 
Selected video codec: [ffpng] vfm: ffmpeg (FFmpeg PNG) 
 
========================================================================== 
 
[ffmpeg] aspect_ratio: 0.000000 
 
VDec: vo config request - 560 x 560 (preferred colorspace: RGB 24-bit) 
 
Trying filter chain: expand lavc 
 
Could not find matching colorspace - retrying with -vf scale... 
 
Opening video filter: [scale] 
 
SwScale params: -1 x -1 (-1=no scaling) 
 
Trying filter chain: scale expand lavc 
 
VDec: using RGB 24-bit as output csp (no 6) 
 
Movie-Aspect is undefined - no prescaling applied. 
 
VO Config (560x560->560x560,flags=0,'MPlayer',0x52474218) 
 
SwScaler: reducing / aligning filtersize 1 -> 4 
 
SwScaler: reducing / aligning filtersize 1 -> 4 
 
SwScaler: reducing / aligning filtersize 1 -> 1 
 
SwScaler: reducing / aligning filtersize 9 -> 8 
 
[swscaler @ 01216d40]BICUBIC scaler, from rgb24 to yuv420p using MMX2 
 
[swscaler @ 01216d40]560x560 -> 560x560 
 
[swscaler @ 01216d40]lum srcW=560 srcH=560 dstW=560 dstH=560 xInc=65536 yInc=65536 
 
[swscaler @ 01216d40]chr srcW=280 srcH=560 dstW=280 dstH=280 xInc=65536 yInc=131072 
 
REQ: flags=0x401  req=0x0   
 
REQ: flags=0x401  req=0x0   
 
videocodec: libavcodec (560x560 fourcc=31564646 [FFV1]) 
 
[ffv1 @ 01235100]err{or,}_recognition separate: 1; 1 
 
[ffv1 @ 01235100]err{or,}_recognition combined: 1; 65537 
 
[ffv1 @ 01235100]Unsupported bit depth: 0 
 
*** [scale] Allocating mp_image_t, 560x560x24bpp RGB packed, 940800 bytes 
 
*** [lavc] Allocating mp_image_t, 560x560x12bpp YUV planar, 470400 bytes 
 
*** [expand] Direct Rendering mp_image_t, 560x560x12bpp YUV planar, 470400 bytes 
 
Unicode font: 773 glyphs. 
 
Unicode font: 773 glyphs. 
 
Muxer frame buffer sending 1 frame(s) to the muxer. 
 
Writing header... 
 
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header. 
 
Writing header... 
 
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header. 
 
Pos:   0.0s      1f ( 1%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/14 D/B/S 0/0/0  
 
 
*** [scale] Allocating mp_image_t, 560x560x24bpp RGB packed, 940800 bytes 
 
Pos:   0.1s      2f ( 3%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/12 D/B/S 0/0/0  
Pos:   0.1s      3f ( 5%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/11 D/B/S 0/0/0  
Pos:   0.2s      4f ( 7%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/11 D/B/S 0/0/0  
Pos:   0.2s      5f (10%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.2s      6f (11%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.3s      7f (14%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.3s      8f (15%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.4s      9f (18%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.4s     10f (20%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.4s     11f (21%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.5s     12f (23%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.5s     13f (25%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.6s     14f (28%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.6s     15f (30%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.6s     16f (31%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/10 D/B/S 0/0/0  
Pos:   0.7s     17f (34%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   0.7s     18f (36%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   0.8s     19f (37%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   0.8s     20f (40%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   0.8s     21f (41%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   0.9s     22f (43%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   0.9s     23f (46%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.0s     24f (47%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.0s     25f (50%)   0fps Trem:   0min   1mb  A-V:0.000 [0:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.0s     26f (52%)   0fps Trem:   0min   1mb  A-V:0.000 [5215:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.1s     27f (55%)   0fps Trem:   0min   1mb  A-V:0.000 [5176:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.1s     28f (56%)   0fps Trem:   0min   1mb  A-V:0.000 [5136:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.2s     29f (58%)   0fps Trem:   0min   1mb  A-V:0.000 [5097:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.2s     30f (61%)   0fps Trem:   0min   1mb  A-V:0.000 [5059:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.2s     31f (62%)   0fps Trem:   0min   1mb  A-V:0.000 [5021:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.3s     32f (64%)   0fps Trem:   0min   1mb  A-V:0.000 [4983:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.3s     33f (67%)   0fps Trem:   0min   1mb  A-V:0.000 [4946:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.4s     34f (68%)   0fps Trem:   0min   1mb  A-V:0.000 [4910:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.4s     35f (70%)   0fps Trem:   0min   1mb  A-V:0.000 [4873:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.4s     36f (73%)   0fps Trem:   0min   1mb  A-V:0.000 [4838:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.5s     37f (75%)   0fps Trem:   0min   1mb  A-V:0.000 [4802:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.5s     38f (76%)   0fps Trem:   0min   1mb  A-V:0.000 [4767:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.6s     39f (79%)   0fps Trem:   0min   1mb  A-V:0.000 [4732:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.6s     40f (81%)   0fps Trem:   0min   1mb  A-V:0.000 [4698:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.6s     41f (82%)   0fps Trem:   0min   1mb  A-V:0.000 [4664:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.7s     42f (85%)   0fps Trem:   0min   1mb  A-V:0.000 [4631:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.7s     43f (87%)   0fps Trem:   0min   1mb  A-V:0.000 [4599:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.8s     44f (88%)   0fps Trem:   0min   1mb  A-V:0.000 [4566:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.8s     45f (91%)   0fps Trem:   0min   1mb  A-V:0.000 [4534:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.8s     46f (93%)   0fps Trem:   0min   1mb  A-V:0.000 [4502:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.9s     47f (94%)   0fps Trem:   0min   1mb  A-V:0.000 [4471:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   1.9s     48f (97%)   0fps Trem:   0min   1mb  A-V:0.000 [4440:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   2.0s     49f (100%)   0fps Trem:   0min   1mb  A-V:0.000 [4409:0] A/Vms 0/9 D/B/S 0/0/0  
Pos:   2.0s     50f (100%)   0fps Trem:   0min   1mb  A-V:0.000 [4378:0] A/Vms 0/9 D/B/S 0/0/0  
 
 
ds_fill_buffer: EOF reached (stream: video)   
 
 
 
Flushing video frames. 
 
Writing index... 
 
Writing header... 
 
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header. 
 
 
 
Video stream: 4378.820 kbit/s  (547352 B/s)  size: 1094705 bytes  2.000 secs  50 frames 
 
Uninit video: ffmpeg 
 
WINSOCK2 uninit 
 
deo stream: 4378.820 kbit/s  (547352 B/s)   
 
size: 1094705 bytes  2.000 secs  50 framesUninit video: ffmpegWINSOCK2  
 
uninit 

但是当我更改文件夹时,我得到这个:

MEncoder Sherpya-SVN-r34401-4.6.2 (C) 2000-2011 MPlayer Team 
 
Setting PATH to C:\Downloads\MPlayer-athlon-svn-34401\codecs 
 
get_path('codecs.conf') -> 'C:\Users\Main/mplayer/codecs.conf' 
 
Reading optional codecs config file C:\Users\Main/mplayer/codecs.conf: No such file or directory 
 
Reading optional codecs config file /mingw/etc/mplayer/codecs.conf: No such file or directory 
 
Using built-in default codecs.conf. 
 
init_freetype 
 
Using MMX (with tiny bit MMX2) Optimized OnScreenDisplay 
 
get_path('fonts') -> 'C:\Users\Main/mplayer/fonts' 
 
Configuration: --extra-cflags=-I/c/Work/mplayer/live --extra-ldflags=-lws2_32 --prefix=/mingw --enable-static --enable-md5sum --enable-menu --enable-faac --enable-enca --enable-sdl --enable-caca --enable-gl --enable-freetype --enable-png --enable-mng --enable-jpeg --enable-gif --enable-tga --enable-mad --enable-tv --enable-theora --disable-vidix --disable-faac-lavc --disable-inet6 --flavor=athlon 
 
WINSOCK2 init: 0 
 
STREAM: [mf] mf://C:/Users/Main/Desktop/marshmarshmarshmarsh/*.png 
 
STREAM: Description: Multiple files input 
 
STREAM: Author: Benjamin Zores, Albeu 
 
STREAM: Comment:  
 
success: format: 16  data: 0x0 - 0x0 
 
MF file format detected. 
 
[mf] search expr: C:/Users/Main/Desktop/marshmarshmarshmarsh/*.png 
 
[mf] number of files: 50 (200) 
 
==> Found video stream: 0 
 
VIDEO:  [MPNG]  560x560  24bpp  25.000 fps    0.0 kbps ( 0.0 kbyte/s) 
 
[V] filefmt:16  fourcc:0x474E504D  size:560x560  fps:25.000  ftime:=0.0400 
 
WINSOCK2 init: 0 
 
WINSOCK2 init: 0 
 
[file] File size is 0 bytes 
 
STREAM: [file] C:/Users/Main/Desktop/marshmarshmarshmarsh/Slide.avi 
 
STREAM: Description: File 
 
STREAM: Author: Albeu 
 
STREAM: Comment: based on the code from ??? (probably Arpi) 
 
Opening video filter: [expand osd=1] 
 
Expand: -1 x -1, -1 ; -1, osd: 1, aspect: 0.000000, round: 1 
 
========================================================================== 
 
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family 
 
[png @ 01235100]err{or,}_recognition separate: 2; 1 
 
[png @ 01235100]err{or,}_recognition combined: 2; 196609 
 
INFO: libavcodec init OK! 
 
Selected video codec: [ffpng] vfm: ffmpeg (FFmpeg PNG) 
 
========================================================================== 
 
ds_fill_buffer: EOF reached (stream: video)   
 
 
 
Flushing video frames. 
 
Writing index... 
 
Writing header... 
 
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header. 
 
 
 
Video stream:      nan kbit/s  (-2147483648 B/s)  size: 0 bytes  0.000 secs  0 frames 
 
Uninit video: ffmpeg 
 
WINSOCK2 uninit 

这是仅使用 50 个 png 图像的测试运行。为了成功运行,我的 python 脚本位于包含图像的文件夹中,并告诉它将视频放在那里。 (“临时”文件夹)

失败的运行是一个包含图像的文件夹,但不是我的 python 脚本。 ('marshmarshmarshmarsh' 文件夹)。它似乎可以很好地找到图像,但不知何故无法从中生成视频。 我不明白为什么它只能在一个文件夹中工作,但当它被告知将视频放在其他文件夹中时却无法工作。

请您参考如下方法:

迟到的答案:我遇到了同样的问题,尽管进行了大量搜索,我找到的唯一解决方法是更改​​到目标目录,制作视频然后切换回原始目录,如果你需要做一些进一步处理:

# Make sure the image directory exists 
if not os.path.exists(img_dir): 
    raise Exception("\nThe directory '" + img_dir + "' does not exist !") 
 
# Navigate to the image directory 
os.chdir(img_dir) 
 
# Sort image files 
subprocess.call('dir /b /o *.png > files.txt', shell=True)   
 
 
animationname = "Animation_" + foldername[:-1].lower() + "_" + scalar_to_export +  ".avi" 
command = ('mencoder', 
           'mf://@files.txt', #works only in the current dir 
           '-mf', 
           'type=png:fps='+str(fps), 
           '-ovc', 
           'lavc', 
           '-lavcopts', 
           'vcodec=mpeg4', 
           '-oac', 
           'copy', 
           '-o', 
           dirname + animationname )    
subprocess.call(command)     
 
# Switch back to the original directory 
os.chdir(dirname)