在保留原始音频的情况下调整视频文件的大小
Resizing a video file with keeping original audio
我想将视频从 1280*720 调整为 854*480 而不改变 fps 或任何东西,并且还想保留原始音频剪辑。
我在 Windows 8.1.
中使用 Jupyter Notebook
Input Video file:
Name - test.mp4
Length - 7min, 41sec
Size - 13.5MB
FPS - 29.97
我使用了以下代码:
import cv2
import numpy as np
cap = cv2.VideoCapture('test.mp4')
fourcc = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter('output.mp4',fourcc, 29.97, (854,480))
while True:
ret, frame = cap.read()
if ret == True:
b = cv2.resize(frame,(854,480),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
out.write(b)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
但是,我得到的输出是 29fps,而且输出中也没有音频。输出大小也是 673MB。请告诉一些有效的方法。
我试过:
import skvideo.io
videodata = skvideo.io.vread("test.mp4")
print(videodata.shape)
它给出的错误为:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-46-13276c4b2368> in <module>
----> 1 videodata = skvideo.io.vread("lol.mp4")
2 print(videodata.shape)
C:\Anaconda3\lib\site-packages\skvideo\io\io.py in vread(fname, height, width, num_frames, as_grey, inputdict, outputdict, backend, verbosity)
131 if backend == "ffmpeg":
132 # check if FFMPEG exists in the path
--> 133 assert _HAS_FFMPEG, "Cannot find installation of real FFmpeg (which comes with ffprobe)."
134
135 if ((height != 0) and (width != 0)):
AssertionError: Cannot find installation of real FFmpeg (which comes with ffprobe).
我尝试了很多方法来安装 ffmpeg 但没有用,对我来说没有任何效果。
然后我尝试了以下给出无效语法错误的方法:
ffmpeg -i test.mp4 -vf scale=640:360 movie_360p.mp4
我也试过:
import subprocess
command = f"ffmpeg -i test.mp4 -vf scale=640:360 movie_360p.mp4"
subprocess.call(command, shell=True)
这会产生以下输出,但不会生成任何视频
1
opencv 本身无法处理 auto。叫OPEN Computer Vision不开audio
如果这只是为了一些实际的转换用途,我建议
ffmpeg -i input.mp4 -vf scale=640x360,setdar=16:9 -c:v libx264 \ -preset veryslow -profile:v main -crf 18 -c:a copy output.mp4
将 input.mpr 更改为您的视频并输出为任何名称
参考。 https://superuser.com/questions/837032/ffmpeg-resize-video-but-maintain-audio-quality
如果你真的需要opencv。那可能很难
你可以关注这个Audio output with video processing with opencv
你应该使用 moviepy。它同时处理视频和音频,并且有一个调整大小的功能。
我想将视频从 1280*720 调整为 854*480 而不改变 fps 或任何东西,并且还想保留原始音频剪辑。 我在 Windows 8.1.
中使用 Jupyter NotebookInput Video file:
Name - test.mp4
Length - 7min, 41sec
Size - 13.5MB
FPS - 29.97
我使用了以下代码:
import cv2
import numpy as np
cap = cv2.VideoCapture('test.mp4')
fourcc = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter('output.mp4',fourcc, 29.97, (854,480))
while True:
ret, frame = cap.read()
if ret == True:
b = cv2.resize(frame,(854,480),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
out.write(b)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
但是,我得到的输出是 29fps,而且输出中也没有音频。输出大小也是 673MB。请告诉一些有效的方法。
我试过:
import skvideo.io
videodata = skvideo.io.vread("test.mp4")
print(videodata.shape)
它给出的错误为:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-46-13276c4b2368> in <module>
----> 1 videodata = skvideo.io.vread("lol.mp4")
2 print(videodata.shape)
C:\Anaconda3\lib\site-packages\skvideo\io\io.py in vread(fname, height, width, num_frames, as_grey, inputdict, outputdict, backend, verbosity)
131 if backend == "ffmpeg":
132 # check if FFMPEG exists in the path
--> 133 assert _HAS_FFMPEG, "Cannot find installation of real FFmpeg (which comes with ffprobe)."
134
135 if ((height != 0) and (width != 0)):
AssertionError: Cannot find installation of real FFmpeg (which comes with ffprobe).
我尝试了很多方法来安装 ffmpeg 但没有用,对我来说没有任何效果。
然后我尝试了以下给出无效语法错误的方法:
ffmpeg -i test.mp4 -vf scale=640:360 movie_360p.mp4
我也试过:
import subprocess
command = f"ffmpeg -i test.mp4 -vf scale=640:360 movie_360p.mp4"
subprocess.call(command, shell=True)
这会产生以下输出,但不会生成任何视频
1
opencv 本身无法处理 auto。叫OPEN Computer Vision不开audio
如果这只是为了一些实际的转换用途,我建议
ffmpeg -i input.mp4 -vf scale=640x360,setdar=16:9 -c:v libx264 \ -preset veryslow -profile:v main -crf 18 -c:a copy output.mp4
将 input.mpr 更改为您的视频并输出为任何名称
参考。 https://superuser.com/questions/837032/ffmpeg-resize-video-but-maintain-audio-quality
如果你真的需要opencv。那可能很难 你可以关注这个Audio output with video processing with opencv
你应该使用 moviepy。它同时处理视频和音频,并且有一个调整大小的功能。