Issue with overwriting file while using ffmpeg for

2019-04-05 15:42发布

问题:

I'm using ffpmeg to convert all my videos to mp4:

 ffmpeg -i InpuFile -vcodec h264 -acodec aac -strict -2 OutputFile.mp4

The problem is, if I'm overwriting the input file, i.e the output and input files are the same:

 ffmpeg -i InpuFile -vcodec h264 -acodec aac -strict -2 InpuFile.mp4 -y

or

 ffmpeg -i InpuFile -y -vcodec h264 -acodec aac -strict -2 InpuFile.mp4

the new file is not good. He lasts one second and his size is extremely small.

Any ideas?

I want to use this as a script in my server so the overwriting is the most convinient way for me, I prefer that way instead of creating temporary files then replacting the temporary with original.

回答1:

I had this same (frustrating) problem, you may have noticed that this happens because ffmpeg is writing over the file that it's reading, you are corrupting the source before the process finish... ffmpeg doesn't put the file in some buffer, so you can't do this way, you will have to use a temporary file.

just in case



回答2:

You cannot overwrite the input file while you are encoding. You must encode to an different output file.

Afterwards, you can replace the original file with the new encoded file.



回答3:

Neither is too annoying tmp file use. In one line:

input="InpuFile"; ffmpeg -i "$input" -y -vcodec h264 -acodec aac -strict -2 "/tmp/$input";rm "$input"; mv "/tmp/$input" .;

There is any bad stuff by this?



标签: ffmpeg