convert animated gif to video on linux server while preserving frame rate
Yet Another Avconv Bug (YAAB)
ffmpeg
has better GIF demuxing support (and improved GIF encoding). I recommend ditching avconv
and getting ffmpeg
(the real one from FFmpeg; not the old charlatan from Libav). A static build is easy, or you can of course compile.
Example
ffmpeg -i in.gif -c:v libx264 -pix_fmt yuv420p -movflags +faststart out.mp4
See the FFmpeg Wiki: H.264 Encoding Guide for more examples.
100% of gif does not convert to mp4 with moviepy
I changed my .gif conversion to
os.system('echo "y"| ffmpeg -i temp.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" temp.mp4')
and then clip the duration to a max of 15s by using
if (dur > 15):
print("changing duration")
clip.set_duration(15).write_videofile("buffertemp.mp4")
clip = mp.VideoFileClip("buffertemp.mp4")
clip.write_videofile("temp.mp4")
Breaking a video into predefined chunks
Xuggler is Java library on top of the ffmpeg libraries that makes a task like this relatively easy from Java. Look at the tutorials on the site for a quick start.
Map every frame of a video file to a .png that points to the original frame
Is there some special file (maybe some magic with a pipe) that could reference specific frame in a video file that is seen as a .png to outside utils, but when edited updates the original frame in the video?
This isn't a thing that you could do usefully with most video formats. Frames are not stored independently in the file; video compression algorithms work by storing the differences between adjacent frames, so it is very difficult to update a single frame without altering all of the other frames which are related to it.
In any case, no; there is no way in a standard UNIX system to create a file that is linked in this sort of way to another file. What you could do, however, is create a FUSE file system that exposes the frames of a video as files in a virtual filesystem. This isn't a trivial task, though; for information on getting started, you may want to work through the FUSE tutorial:
http://www.cs.nmsu.edu/~pfeiffer/fuse-tutorial/
Rendering video to file that may have an inconsistent frame rate
It turns out WriteVideoFrame
is overloaded and one variant of the function is WriteVideoFrame(Bitmap frame, TimeSpan timestamp)
. As you can guess, the time stamp is used to make a frame appear at a certain time in the video.
Thus, by keeping track of the real time, we can set each frame to use the time that it should be in the video. Of course, the video quality will be worse if you can't render quickly enough, but this resolves the issue at hand.
Here's the code that I used for the Draw
function:
// We can provide a frame offset so that each frame has a time that it's supposed to be
// seen at. This ensures that the video looks correct if the render rate is lower than
// the frame rate, since these times will be used (it'll be choppy, but at least it'll
// be paced correctly -- necessary so that sound won't go out of sync).
long currentTick = DateTime.Now.Ticks;
StartTick = StartTick ?? currentTick;
var frameOffset = new TimeSpan(currentTick - StartTick.Value);
// Figure out if we need to render this frame to the file (ie, has enough time passed
// that this frame will be in the file?). This prevents us from going over the
// desired frame rate and improves performance (assuming that we can go over the frame
// rate).
double elapsedTimeInSeconds = stopwatch.ElapsedTicks / (double) Stopwatch.Frequency;
double timeBetweenFramesInSeconds = 1.0 / FrameRate;
if (elapsedTimeInSeconds >= timeBetweenFramesInSeconds)
{
stopwatch.Restart();
Writer.WriteVideoFrame(frame.ToBitmap(), frameOffset);
}
Where StartTick
is a long?
member of the object.
Convert from .mov to .mp4 (or h264) using avconv
In a very basic form, it would look a bit like this:
avconv -i inputfile.mov -c:v libx264 outputfile.mp4
This will only work if you compiled avconv with libx264 support - you can see here on how to do that.
If you're not that concerned about codecs and just need an ".mp4" file, you can also run this:
avconv -i inputfile.mov -c copy outputfile.mp4
This will copy all codec information from the .mov container and place it into the .mp4 container file.
Just as a note, avconv is a fork of ffmpeg, many of the switches for ffmpeg will work for avconv (if that helps your search for answers)
Reduce Video Frame Rate While Keeping Same Length/Duration Using AVAssetWriter
For each dropped frame, you need to compensate by doubling the duration of the sample you're going to write.
while(videoInput.isReadyForMoreMediaData){
if let sample = assetReaderVideoOutput.copyNextSampleBuffer() {
if counter % 2 == 0 {
let timingInfo = UnsafeMutablePointer<CMSampleTimingInfo>.allocate(capacity: 1)
let newSample = UnsafeMutablePointer<CMSampleBuffer?>.allocate(capacity: 1)
// Should check call succeeded
CMSampleBufferGetSampleTimingInfo(sample, 0, timingInfo)
timingInfo.pointee.duration = CMTimeMultiply(timingInfo.pointee.duration, 2)
// Again, should check call succeeded
CMSampleBufferCreateCopyWithNewTiming(nil, sample, 1, timingInfo, newSample)
videoInput.append(newSample.pointee!)
}
counter = counter + 1
}
}
FFmpeg: high quality animated GIF?
I've written a tool specifically for maximum quality:
https://gif.ski
ffmpeg -i video.mp4 frame%04d.png
gifski -o clip.gif frame*.png
It generates good per-frame palettes, but also combines palettes across frames, achieving even thousands of colors per frame.
If you want to reduce the video dimensions, add a scaling filter:
ffmpeg -i video.mp4 -vf scale=400:240 frame%04d.png
If you want to reduce the frame rate, add the fps
filter:
ffmpeg -i video.mp4 -vf fps=12 frame%04d.png
You can combine the filters with -vf scale=400:240,fps=12
Related Topics
I Can't Execute Command Modprobe Vboxdrv
Shell Script to Copy and Prepend Folder Name to Files from Multiple Subdirectories
What Does "$1/*" Mean in "For File in $1/*"
How to Update Cudnn to a Newer Version
Cannot Create Backup File(Add ! to Overwrite)
When Should the Option Remainafterexit Needs to Be Set True When Creating New Systemd Services
Execution Time of an SQLite Query: Units
Installing Qt on Linux, Cannot Find -Lgl
How to Check If Find Command Didn't Find Anything
How to Recursively Search for Files with Certain Extensions
Calculate Total Used Disk Space by Files Older Than 180 Days Using Find
Change Filenames to Lowercase in Ubuntu in All Subdirectories
Pause Programmatically Video Player Mpv