Reducing audio , video size on upload Django - python

I got a test server where I have features like uploading audio and video on a post . The feature is working perfectly but using a lot of space as some videos has more than 80MB while my system has only 1Gb of storage. How can I reduce my audio video size on upload maintaining no upload restriction. I am using Django and drf on backend . I looked into ffmpeg & pydub but couldnt find anything to reduce the audio bitrate or video quality. Is there anything else I should look into ? or ffmpeg will work (if ffmpeg works, please give me some example links so that I can implement before integrating to my test server) Thanks
Any suggestion regarding this will be helpful

Related

Moviepy freezes after first couple of frames after concatenation

I have been trying to build a project in which a Flask Application can automatically concatenate a selected amount of video's to a 'core video'.
User's can upload a video, which is sent to amazon s3 for storage.
All video's are preprocessed by Moviepy to be an mp4 file, running on 24 fps without audio, with a resolution of 720p.
After this preprocessing, the video is uploaded to amazon s3.
Of all new uploads in s3, a queue is created which an administrator can approve or delete.
All approved video's end up in a list that is concatenated with a current 'core video'.
This is done by using Pythons Moviepy library.
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip
videos_to_concat(VideoFileClip(core_video.s3_link))
for video in approved_videos:
videos_to_concat.append(VideoFileClip(video.s3_link))
result = concatenate_videoclips(videos_to_concat, method=compose)
Later, some audio is added under the full duration of the video.
result_with_audio = result.set_audio(some_audio.mp3)
The problem however, is that without throwing any errors, some videos are frozen after the first couple of frames after concatenation has been successfull. A frame remains stationary for the duration of the original clip. The audio keeps playing though. When a next clip is loaded, that one either plays normally or has the same behaviour of freezing after a couple of frames. There seems to be no obvious patern.
Initially I thought the mistake might be that ffmpeg does not download video's from the normal s3 link properly, but that would not explain why the the biggest video in the beginning and some other video's get rendered correctly, and some others aren't.
Could it that this is caused by a potential difference in codecs? (libx264 vs. mpeg4)?
Or is this way of accessing the files by URL and then directly feeding that to moviepy a potential cause of troubles? (VideoFileClip(https://amazon.s3.link.to.file.here.mp4)
Should I try to download all files and then locally concatenating them, or am I right to assume that the current approach should work.
When inspecting the files, nothing obvious like filename, filetype, resolution seems to be the issue, the preprocessing seems to do what it should.
Would love to hear any idea's on how the corruption of the resulting concatenated video could be explained and hopefully resolved.
Okay, I did manage to figure it out in the end. The problem was solved by downloading all video's with the boto3 client that amazon s3 provides for Python. Once downloading all video's to local storage of the webserver, concatenation worked without any issues.
I'm guessing that this might have something to do with s3 not serving the entire video file instantly through the link. In the end it seems quite logical to just use the provided s3 client to download store video's before performing any edits with moviepy.

How to write avi file with OpenCV larger than 2 GB?

I'm using OpenCV with Python, but actually can switch to C++, so if it's matter please answer question considering it.
I'm writing .avi file(joining multiple avi files into one) using
cv2.VideoWriter([filename, fourcc, fps, frameSize[, isColor]])
but recently found out that I can't write .avi file larger than 2 GB with it. It even mentioned there: Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB.
But right now I've got no time to learn new library like ffmpeg, I need to do it very fast.
How can I write this file, using C++ or Python with knowledge of OpenCV, or at least with input part - using
cv::Mat
as frames
This limitation was removed in OpenCV 3.0, due to the introduction of new file formats such as .mkv, who do support video files larger than 2GB.
See Does OpenCV 3.0 Still Has Limits On VideoWriter Size?.
NOTE: The documentation and examples weren't updated yet, so maybe this should be considered experimental.
You have answered your own question but I'm afriad it isn't the answer you want.
From your link
As you can see things can get really complicated with videos. However, OpenCV is mainly a computer vision library, not a video stream, codec and write one. Therefore, the developers tried to keep this part as simple as possible. Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you can only create and expand a single video track inside the container. No audio or other track editing support here. Nevertheless, any video codec present on your system might work. If you encounter some of these limitations you will need to look into more specialized video writing libraries such as FFMpeg or codecs as HuffYUV, CorePNG and LCL.
What this paragraph says is that the developers of OpenCV made a design choice that says you cannot write video files larger than 2Gb using OpenCV for the specific reason that it is a computer vision library not a video tool.
Unfortunately if you want to write videos larger than 2Gb you are going to need to learn to use FFMPEG or something similar (It isn't that hard and has good bindings to OpenCV)

Python get Audio/Video frames separately from video file

What i'm trying to do:
Hi!
I'm trying to store the Video and Audio information from a video file. I would like to store video frames and audio frames separately in different variables.
My intention is to manage video/files and do some actions with the audio and video frame list, but to do what I'm plannign to do I need to store this audio/video frames separately. I've read a lot of questions in StackOverflow about python and audio/video managing.
Most people recommend to use OpenCV or ffmpeg to manage videos. I saw some scripts using these libraries to get video(only video) frames, but none of them are getting audio, most of them are just getting video frames and save them as RGB images. I also check some scripts where people get audio frames from a mp3 file, but I'm not sure if you can do that in a video file
Most important thing to me is to know the best way to manage video and audio separately. I'm not looking for people to do my code, just asking to point me in a good direction.
One of the things I'm trying to do is to send this information via socket, but as I said I need the audio and video frames to be in separated variables (yes, i'm wondering about an stream app, but that's not the only thing I'm trying to do)
I know I should give more information, and maybe show some code, but I don't have any concret code I tried some things, but I've never been capable to separate audio and video. I know that each format has his own encryption, and at the end I decided to use "mp4" as video format but I don't know neither if this is the best format for what I'm trying to do.
Resume:
Is openCV the best way to manage video and audio separately ?
Wich is the easiest way to separate video and audio frames ? Is it possible ?
Wich is the best documentation I should read to learn about video/audio management ?
I would like to do the things with my own code, and use in the less way possible openCV or other libraries.
My "basic" idea is to get a "list" of audio and video frames, and then I would like to do some operations, but right now I can't find the best way for me to manage a vide using python. I even wonder if could be possible to manage a video as raw data
I need to know wich is the best library to manage videos using python, for me the best library, will be the one that allows me to manage the videos more "freely"
I've already checked:
I've read too many questions on this theme, the most recent are :
How to extract audio from video file
Split audio video separately from given video using MLT
Embed audio video in python gui

How do I get all the urls of a video flv pieces only with Python?

The url for ordinary people to watch the video is: http://v.youku.com/v_show/id_XNjM5NDU1OTUy.html
This video is split into 14 flv pieces, 5 of which are advertising flvs.
If I open the Developer Tools of IE11 and keep capturing the network flow during the whole process of watching the video (It must be the whole process, or the server doesn't send all of the video flv urls to IE11), the flv urls will be captured by IE11 and then I can copy the data of the flv urls which the below picture displays in a red line box:
Then I can change the data into a list of url-strings and use Python to download them.
But this is really trouble.
I have tried to match the source code of http://v.youku.com/v_show/id_XNjM5NDU1OTUy.html with the flv urls, but no results. So I guess there must be a function or a javascript or something else in the code to tell the server to send all the flv urls. Am I right?
So,
1.How to get all the urls of a video flv pieces only with Python?
2.What should I learn to solve this kind of problem.
After all, using Developer Tools of IE11, waiting for the whole process of the video (nearly one hour), copying the related data to a txt file and finally using Python to parse the txt file are really something trouble.
Thanks in advance.
I think you could get some insights from Youtube-dl. It is a set of python scripts created to "download Youtube videos and a few more sites". Go to their Download section and get the full source tarball. I think that could be useful in some way, at least to give you some directions on how to deal with flv pieces.

mpeg-1 video writing with python

I am trying to use the Opencv VideoWriter object with the mpeg-1 encoding to create videos, I am aiming at writing only two images on that video, using mpeg-1 encoding, I would like to know how much the first image that I wrote first helps in compressing the second image. In other words find the file size before writing the 2nd image and after. My questions are:
Is there any way to perform this process using Opencv?
Is there a way to avoid writing on disks and just have the information of the size of the compreesed video( after adding the second image)?
Is there any other good alternatives reach my goals?
I suggest you learn GStreamer framework which has Python bindings available.
http://gstreamer.freedesktop.org/modules/gst-python.html
It works best on Linux platforms, some OSX support is available.
GStreamer provides "sane", but very powerful and very complex, APIs for procedural video and audio generation.
See also:
GStreamer: status of Python bindings and encoding video with mixed audio
Alternative you can write out frames to raw image images files and parse them to a video using ffmpeg command. Might work on Microsoft Windows platforms too.

Categories