如何将动画保存为文件(“算法第 4 版”,作者 Robert Sedgewick 和 Kevin Wayne。)
How to save animation as a file (“Algorithms 4th Edition” by Robert Sedgewick and Kevin Wayne.)
我正在阅读 Robert Sedgewick 和 Kevin Wayne 的 "Algorithms 4th Edition"。
使用StdDraw
,我们可以创建动画:
package general_test;
import edu.princeton.cs.algs4.StdDraw;
public class AnimationTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
StdDraw.setScale(-2, +2);
StdDraw.enableDoubleBuffering();
for (double t = 0.0; true; t += 0.02) {
double x = Math.sin(t);
double y = Math.cos(t);
StdDraw.clear();
StdDraw.filledCircle(x, y, 0.05);
StdDraw.filledCircle(-x, -y, 0.05);
StdDraw.show();
StdDraw.pause(20);
}
}
}
我想将这个动画保存为文件。
如何将此动画另存为文件?
有什么简单的方法吗?
很简单的方法
- 添加计数器。
int counter = 0;
- 将迭代步数限制为 1000。
for (double t = 0.0; t < 20; t += 0.02)
- 将每个迭代步骤保存为 PNG 文件。
StdDraw.save("image-"+String.format("%03d", counter++)+".png");
- 使用 ffmpeg 将这些文件转换为视频文件。
ffmpeg -i image-%03d.png video.webm
尽情享受吧!
参考文献
- https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence
- Add prefix to number e.g (1 = 001)
我正在阅读 Robert Sedgewick 和 Kevin Wayne 的 "Algorithms 4th Edition"。
使用StdDraw
,我们可以创建动画:
package general_test;
import edu.princeton.cs.algs4.StdDraw;
public class AnimationTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
StdDraw.setScale(-2, +2);
StdDraw.enableDoubleBuffering();
for (double t = 0.0; true; t += 0.02) {
double x = Math.sin(t);
double y = Math.cos(t);
StdDraw.clear();
StdDraw.filledCircle(x, y, 0.05);
StdDraw.filledCircle(-x, -y, 0.05);
StdDraw.show();
StdDraw.pause(20);
}
}
}
我想将这个动画保存为文件。
如何将此动画另存为文件?
有什么简单的方法吗?
很简单的方法
- 添加计数器。
int counter = 0;
- 将迭代步数限制为 1000。
for (double t = 0.0; t < 20; t += 0.02)
- 将每个迭代步骤保存为 PNG 文件。
StdDraw.save("image-"+String.format("%03d", counter++)+".png");
- 使用 ffmpeg 将这些文件转换为视频文件。
ffmpeg -i image-%03d.png video.webm
尽情享受吧!
参考文献
- https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence
- Add prefix to number e.g (1 = 001)