Log in

View Full Version : Muxing audio file and still image with FFMPEG for youtube upload?


egr
24th August 2020, 02:59
Hi, since youtube doesn't accept raw audio file and must be mp4 video file, so I googled around and found this handy piece of code to mux any audio file with any image file to make a video file:

ffmpeg -r 1 -loop 1 -i <image file> -i <audio file> -acodec copy 3.mp4


...except it doesn't seem to work with youtube as I get a processing error when I upload to it and it gets to that point. Plays back fine in all players(windows media player, pot player, vlc), except MPC HC for some reason....

Maybe there is extra bits needed to make it compatible with youtube upload?

butterw2
24th August 2020, 13:37
This works for me in mpc-hc (evr-cp), not tested with youtube:
ffmpeg -loop 1 -i "photo.jpg" -i "audio.m4a" -c:a copy -c:v libx264 -tune stillimage -r 25 -shortest -movflags faststart "out.mp4"

Progressive download video with 25fps, keyframes and the same length as the audio.

sneaker_ger
24th August 2020, 17:07
"raw audio file" can mean many things. You have not provided much info (actual audio format, MediaInfo, ffmpeg logs, error messages). Maybe the audio format is not compatible with Youtube or at least not in the way ffmpeg muxes it into mp4. You could try to use mkv container instead of mp4 and/or convert audio to flac. ( -c:a flac )


(Default crf value for x264 is 23 which is IMHO quite low. Since Youtube re-encodes everything you upload I would suggest a lower crf, e.g. -crf 16 )

egr
24th August 2020, 17:28
This works for me in mpc-hc (evr-cp), not tested with youtube:
ffmpeg -loop 1 -i "photo.jpg" -i "audio.m4a" -c:a copy -c:v libx264 -tune stillimage -r 25 -shortest -movflags faststart "out.mp4"

Progressive download video with 25fps, keyframes and the same length as the audio.

Aaaaaaaaaayyyyyyyyyy that worked! :thanks: Even works uploading to youtube as well! Nice.

....so what I was missing was setting the frame rate to 25 and keyframes at the same length of the audio?

egr
24th August 2020, 17:36
"raw audio file" can mean many things. You have not provided much info (actual audio format, MediaInfo, ffmpeg logs, error messages). Maybe the audio format is not compatible with Youtube or at least not in the way ffmpeg muxes it into mp4. You could try to use mkv container instead of mp4 and/or convert audio to flac. ( -c:a flac )


(Default crf value for x264 is 23 which is IMHO quite low. Since Youtube re-encodes everything you upload I would suggest a lower crf, e.g. -crf 16 )

raw audio file as in any audio file, like .wav, .m4a, .aac, .ac3, .mp3, .flac, .ogg.....you know the usual audio file extensions....and codecs...

Is there a way to *not* have youtube re encode everything? I've noticed redownloading the same file I uploaded is different to the file I uploaded.....hhhhhhhhhmmmmmmmmmmm.....how to download original file uploaded?

otherwise I guess I'll keep that in mind and tack that somewhere in there...maybe: ffmpeg -loop 1 -i "Untitled.png" -i "File.m4a" -c:a copy -c:v libx264 -crf 16 -tune stillimage -r 25 -shortest -movflags faststart "VideoFile.mp4"

butterw2
24th August 2020, 18:16
Aaaaaaaaaayyyyyyyyyy that worked! :thanks: Even works uploading to youtube as well! Nice.

....so what I was missing was setting the frame rate to 25 and keyframes at the same length of the audio?

You weren't missing anything really.
- using Shortest is recommended because the audio duration could be incorrect.
- Frame rate defaults to 30. But outside youtube, it may make sense to use significantly less, (ex: -r 2)
- With -loop 0, you get smaller filesize, but you don't get keyframes.
You don't get faststart by default with ffmpeg, but it is recommended by youtube.

I think you can download the clip as uploaded in the youtube video editor/dashboard. Youtube always re-encodes video + audio.

egr
25th August 2020, 05:53
You weren't missing anything really.
- using Shortest is recommended because the audio duration could be incorrect.
- Frame rate defaults to 30. But outside youtube, it may make sense to use significantly less, (ex: -r 2)
- With -loop 0, you get smaller filesize, but you don't get keyframes.
You don't get faststart by default with ffmpeg, but it is recommended by youtube.

I think you can download the clip as uploaded in the youtube video editor/dashboard. Youtube always re-encodes video + audio.

How can the audio duration be wrong?!

I didnt think framerate would matter if it's just a still image that doesnt change at all or have any movement or motion....wouldnt 1fps be good enough? To keep the file size small?

Are keyframes that important?

butterw2
25th August 2020, 08:29
How can the audio duration be wrong?!

I didnt think framerate would matter if it's just a still image that doesnt change at all or have any movement or motion....wouldnt 1fps be good enough? To keep the file size small?

Are keyframes that important?

The reported audio duration is sometimes wrong/unavailable with m4a/aac (some tools cause this). Remuxing with ffmpeg fixes the issue.

more info on simple lossless ffmpeg commands here:
https://forum.videohelp.com/threads/398507-Top10-Commands-for-Lossless-Video-manipulation-using-ffmpeg-(Guide)


Youtube might not be happy with a 1fps video with single/few keyframes, this might also be the case with hw players ?
It probably works in software players like mpc-hc, one issue could be seeking if the audio is long.

-loop 0 actually gives you a single frame of video, and you need to specify the duration in seconds instead of shortest, ex: -t 60:
ffmpeg -loop 0 -i "photo.jpg" -i "audio.m4a" -c:a copy -c:v libx264 -crf 16 -tune stillimage -t 60 -movflags faststart "out.mp4"

Overall it looks like an interesting method: it muxes fast, overhead vs audio-only is low and you can display subtitles (which you cannot do with audio+cover art).

egr
26th August 2020, 15:31
The reported audio duration is sometimes wrong/unavailable with m4a/aac (some tools cause this). Remuxing with ffmpeg fixes the issue.

more info on simple lossless ffmpeg commands here:
https://forum.videohelp.com/threads/398507-Top10-Commands-for-Lossless-Video-manipulation-using-ffmpeg-(Guide)


Youtube might not be happy with a 1fps video with single/few keyframes, this might also be the case with hw players ?
It probably works in software players like mpc-hc, one issue could be seeking if the audio is long.




-loop 0 actually gives you a single frame of video, and you need to specify the duration in seconds instead of shortest, ex: -t 60:
ffmpeg -loop 0 -i "photo.jpg" -i "audio.m4a" -c:a copy -c:v libx264 -crf 16 -tune stillimage -t 60 -movflags faststart "out.mp4"

Overall it looks like an interesting method: it muxes fast, overhead vs audio-only is low and you can display subtitles (which you cannot do with audio+cover art).
I see, interesting...

For the -loop 0 command, wouldn't you also need milliseconds too since no one audio is exactly in seconds?

There are various GUI on FFMPEG, but they're mostly unmaintained or up to date..... I can think of one that is still maintained: Axiom - used it a bit for some simple stuff when I'm too lazy to google up the commands for it....heh..