-1

I want resize a video from 1280*720 to 854*480 without changing fps or anything and also want to keep the original audio clip. I am using Jupyter Notebook in Windows 8.1.

Input Video file:
    Name - test.mp4 
    Length - 7min, 41sec
    Size - 13.5MB
    FPS - 29.97

I used the following code:

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()

But, I get output as 29fps and also there is no audio in output. Also the size of output is 673MB. Please tell some efficient way to do this.

I have tried:

import skvideo.io
videodata = skvideo.io.vread("test.mp4")  
print(videodata.shape)

It gives error as:

---------------------------------------------------------------------------
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).

I have tried many ways to install ffmpeg but of no use, nothing works fine for me.

I then tried the following which gives invalid syntax error:

ffmpeg -i test.mp4 -vf scale=640:360 movie_360p.mp4

I also tried:

import subprocess
command = f"ffmpeg -i test.mp4 -vf scale=640:360 movie_360p.mp4"
subprocess.call(command, shell=True)

This gives the following output but does not generate any video

1
Rahul Vishwakarma
  • 1,446
  • 2
  • 7
  • 22

2 Answers2

1

opencv itself cant deal with auto. it is called OPEN Computer Vision not open audio.

If this is just for some actual conversion use, i would suggest

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

change the input.mpr to your video and output as any name

reference. https://superuser.com/questions/837032/ffmpeg-resize-video-but-maintain-audio-quality

If you really need opencv. then it might be hard you can follow this Audio output with video processing with opencv

Dr Yuan Shenghai
  • 1,849
  • 1
  • 6
  • 19
  • This is showing invalid syntax in my jupyter notebook. I guess ffmpeg command works on linux/unix, but I am using Windows. – Rahul Vishwakarma Jun 29 '20 at 11:39
  • virutal machine vm? then do this? windows also has ffmepg but i never use. For windows, the windows movie maker should be able to solve it also – Dr Yuan Shenghai Jun 29 '20 at 12:04
  • I want to do that using python, I also want to produce the same on Google Colaboratory for a project. – Rahul Vishwakarma Jun 29 '20 at 12:15
  • I dont think windows can give you easy life on this question. If you really want to use it for project, get FFMEPG installed and compiled then use the opencv with ffempg to store the audio and video concurrently. – Dr Yuan Shenghai Jun 29 '20 at 12:20
  • the error with skvideo.io is that you don't have ffmepg. which you need to installed to windows OS and has it path discovered by python – Dr Yuan Shenghai Jun 29 '20 at 12:25
  • Please provide any link helping me to installing ffmpeg. I have tried many ways from google, at last I have ffmpeg in C:\ffmpeg\bin and also assigned this path in environment variables, but of no use. – Rahul Vishwakarma Jun 29 '20 at 12:38
  • 1
    try this https://www.wikihow.com/Install-FFmpeg-on-Windows . if it doesn work, i guess high chance it is Jupyter issue. if that's the case, change to a raspberry pi. i used ffmepg on this b4. it was good – Dr Yuan Shenghai Jun 29 '20 at 12:58
  • w8 have you tried to compile ffmepg into a library. and command line to the path of the bin. then try ffmepg version command? – Dr Yuan Shenghai Jun 29 '20 at 13:01
  • I have followed wikihow tutorial, so its working in cmd but not in jupyter notebook. is there any modification while using in jupyter notebook – Rahul Vishwakarma Jun 29 '20 at 13:07
  • 1
    if you downloaded, then cd to the bin. to see if you can execute the ffmepg version there – Dr Yuan Shenghai Jun 29 '20 at 13:10
  • if can run. good run it. if can not run the file in bin. try compile a new one. the guide is here https://trac.ffmpeg.org/wiki/CompilationGuide/WinRT – Dr Yuan Shenghai Jun 29 '20 at 13:12
  • if can run that means its the windows symbol link issue. link it correctly then all python conversion one should be able to use. – Dr Yuan Shenghai Jun 29 '20 at 13:23
0

you should use moviepy. It handles both video and audio and there is a resize function.

Dawei Wang
  • 160
  • 12