mapping formats to file extensions and MIME types
I tried posting this to StackOverflow, but haven't gotten any responses yet: http://stackoverflow.com/questions/44035411/how-to-map-ffmpeg-formats-to-mim... I'll copy the question here for convenience: Anyone know of a reference for mapping ffmpeg format values to MIME types and recommended file extension? My google attempt failed to turn up anything. I did manually put together a small list with guess-work and clues from Wikipedia, IANA <https://www.iana.org/assignments/media-types/media-types.xhtml#video>, and the Mozilla Developer Network <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types> for the subset of formats that I encountered in my video input test collection: ffmpeg Format Extension MIME Type ─────────────────────── ───────── ────────────────────── asf asf application/vnd.ms-asf avi avi video/x-msvideo flv flv video/x-flv matroska,webm webm video/webm m4v m4v video/x-m4v mov,mp4,m4a,3gp,3g2,mj2 mp4 video/mp4 mpeg mpeg video/mpeg mpegts mpeg video/mpeg mpegvideo mpeg video/mpeg ogg ogv video/ogg matroska mkv video/x-matroska webm webm video/webm No idea if I've made the right calls, though. (The test files already have file extensions, but I'm operating on the assumption that the extension of a file a user uploads is irrelevant, and that the file should be renamed based on ffprobe and intelligent mapping...) -ofer
2017-05-20 1:51 GMT+02:00 Ofer Nave <odigity@gmail.com>:
Anyone know of a reference for mapping ffmpeg format values to MIME types
You can use the FFmpeg source code to produce such a mapping but I wonder what you need it for?
and recommended file extension?
What is "recommended"? The answer may be possible for avi (although .divx exists iirc) but for asf, at least three "recommended" extensions exist, for transport streams there may be even more. Otoh, .mpg is used for three or four different formats, can you "recommend" it for anything?
My google attempt failed to turn up anything.
I did manually put together a small list with guess-work and clues from Wikipedia, IANA <https://www.iana.org/assignments/media-types/media-types.xhtml#video>, and the Mozilla Developer Network <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types> for the subset of formats that I encountered in my video input test collection:
ffmpeg Format Extension MIME Type ─────────────────────── ───────── ────────────────────── asf asf application/vnd.ms-asf avi avi video/x-msvideo flv flv video/x-flv
matroska,webm webm video/webm
This is a demuxer that supports several formats with different mime-types.
m4v m4v video/x-m4v
This is a muxer.
mov,mp4,m4a,3gp,3g2,mj2 mp4 video/mp4
This is a demuxer, again with different mime-types.
mpeg mpeg video/mpeg mpegts mpeg video/mpeg mpegvideo mpeg video/mpeg
Both the extension list and the mime-types make little sense, the formats are not identical. Wikipedia claims the mime-type for mpegts is "video/MP2T" (and MP2P for mpegps and dvd for vob which I thought is the same).
ogg ogv video/ogg
matroska mkv video/x-matroska webm webm video/webm
These two are muxers.
No idea if I've made the right calls, though.
(The test files already have file extensions, but I'm operating on the assumption that the extension of a file a user uploads is irrelevant, and
This is generally correct although a few formats exist for which probing is impossible or at least so difficult that FFmpeg does not support it currently.
that the file should be renamed based on ffprobe and
intelligent mapping...)
If you don't trust the extension (which generally makes sense) why would you trust the mime type? And why would you trust it more than probing? Carl Eugen
Thanks for your response. Maybe I wasn't clear enough about my situation. My process is this: 1) User uploads file. 2) I ignore the file's current extension (if any) and use ffprobe to determine the format name. 3) I then consult my mapping table to decide: 3a) What extension to rename the file. 3b) What contentType to serve the file with. Is that wrong? -ofer On Sat, May 20, 2017 at 9:05 AM, Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:
2017-05-20 1:51 GMT+02:00 Ofer Nave <odigity@gmail.com>:
Anyone know of a reference for mapping ffmpeg format values to MIME types
You can use the FFmpeg source code to produce such a mapping but I wonder what you need it for?
and recommended file extension?
What is "recommended"? The answer may be possible for avi (although .divx exists iirc) but for asf, at least three "recommended" extensions exist, for transport streams there may be even more. Otoh, .mpg is used for three or four different formats, can you "recommend" it for anything?
My google attempt failed to turn up anything.
I did manually put together a small list with guess-work and clues from Wikipedia, IANA <https://www.iana.org/assignments/media-types/media-types.xhtml#video>, and the Mozilla Developer Network <https://developer.mozilla.org/en-US/docs/Web/HTTP/ Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types> for the subset of formats that I encountered in my video input test collection:
ffmpeg Format Extension MIME Type ─────────────────────── ───────── ────────────────────── asf asf application/vnd.ms-asf avi avi video/x-msvideo flv flv video/x-flv
matroska,webm webm video/webm
This is a demuxer that supports several formats with different mime-types.
m4v m4v video/x-m4v
This is a muxer.
mov,mp4,m4a,3gp,3g2,mj2 mp4 video/mp4
This is a demuxer, again with different mime-types.
mpeg mpeg video/mpeg mpegts mpeg video/mpeg mpegvideo mpeg video/mpeg
Both the extension list and the mime-types make little sense, the formats are not identical.
Wikipedia claims the mime-type for mpegts is "video/MP2T" (and MP2P for mpegps and dvd for vob which I thought is the same).
ogg ogv video/ogg
matroska mkv video/x-matroska webm webm video/webm
These two are muxers.
No idea if I've made the right calls, though.
(The test files already have file extensions, but I'm operating on the assumption that the extension of a file a user uploads is irrelevant, and
This is generally correct although a few formats exist for which probing is impossible or at least so difficult that FFmpeg does not support it currently.
that the file should be renamed based on ffprobe and
intelligent mapping...)
If you don't trust the extension (which generally makes sense) why would you trust the mime type? And why would you trust it more than probing?
Carl Eugen _______________________________________________ ffmpeg-user mailing list ffmpeg-user@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-user
To unsubscribe, visit link above, or email ffmpeg-user-request@ffmpeg.org with subject "unsubscribe".
2017-05-21 1:07 GMT+02:00 Ofer Nave <odigity@gmail.com>:
Maybe I wasn't clear enough about my situation.
No, you were not. Top-posting is not welcome here, your email is a good example why.
My process is this:
1) User uploads file. 2) I ignore the file's current extension (if any) and use
Just to make sure: There are file-types that are detected by the file extension (targa for example).
ffprobe to determine the format name.
So you only use the demuxer formats, never the muxer formats (there is no 1:1 relationship), the muxers in your list therefore make no sense. You can use "ffmpeg -demuxers" to see all existing demuxers.
3) I then consult my mapping table to decide:
3a) What extension to rename the file.
As said, this is generally difficult because for typical file types like asf and mkv, different extensions (depending on the file content) should be chosen.
3b) What contentType to serve the file with.
Demuxers like "mov...." and "matroska..." are used for files with different mime-types, so I am not sure what you want can be achieved with libavformat (that was not written with the intention of mapping its formats to mime-types). Of course, if you don't require to be more exact than libavformat, there is no issue. As said, the source code may help you: If you grep for ff_*_demuxer, you will find structs some of which contain a field ".mime_type" with fields of strings you can use. Carl Eugen
On Sun, May 21, 2017 at 5:03 AM, Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:
Top-posting is not welcome here, your email is a good example why.
If you're referring to the fact that gmail automatically quoted the previous message in my reply, I apologize. I can prevent that from happening now that I know it's an issue. Now, it seems like I'm tripping over my own ignorance of ffmpeg and video in general. I assumed I could probe for format_name and simply map that to a file extension and MIME type, but apparently it's more complicated than that, so let me take a step back: The Problem I am given a file that may or may not be a video file. I know the extension it was originally named with on the user's computer, but since anyone can name a file anything, I conclude I should ignore that. 1) Determine if ffprobe can understand it. (It exits with 1 if it doesn't, so that's easy.) 2) Compare it against some kind of whitelist of formats/codecs/etc that I've decided to allow. (Necessary because ffmpeg will, for example, "recognize" a text file, and even report a video stream in it.) 3) If whitelisted, do further processing. Additionally, I was going to attempt to determine what file extension and MIME type would be most appropriate to associate with the original file and store those in the DB for reference purposes, as well as to set the contentType on the original file, since it is technically serveable via our private web server. But I can do without either if it proves to be too difficult. I would also appreciate any advice on assembling such a whitelist. -ofer
2017-05-21 14:30 GMT+02:00 Ofer Nave <odigity@gmail.com>:
2) Compare it against some kind of whitelist of formats/codecs/etc that I've decided to allow.
(Necessary because ffmpeg will, for example, "recognize" a text file, and even report a video stream in it.)
You can disable the text demuxer at build time or you can use the global -format_whitelist option to only allow formats that you want (without needing any processing).
3) If whitelisted, do further processing.
Additionally, I was going to attempt to determine what file extension and MIME type would be most appropriate to associate with the original file and store those in the DB for reference purposes, as well as to set the contentType on the original file, since it is technically serveable via our private web server. But I can do without either if it proves to be too difficult.
You can just store the FFmpeg demuxer type in you DB, ignoring the mime-type.
I would also appreciate any advice on assembling such a whitelist.
Why would you disable any formats? Carl Eugen
On Sun, May 21, 2017 at 5:48 AM, Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:
You can disable the text demuxer at build time or you can use the global -format_whitelist option to only allow formats that you want (without needing any processing).
Good to know - but what other surprises lurk that I haven't discovered yet?
You can just store the FFmpeg demuxer type in you DB, ignoring the mime-type.
Can I ask for clarification about one point? I understand that ffprobe returns format (which includes format_name) and a list of streams (which include codec_name). When you say store the demuxer, are you referring to the format or codec? And is it always a demuxer (rather than a muxer) because I'm probing the file, rather than generating it?
I would also appreciate any advice on assembling such a whitelist.
Why would you disable any formats?
Because paranoia is a cheap substitute for perfect knowledge. As I said, I was surprised when ffprobe inspected a text file without complaint, and even reported a video stream. (I expected an error.) Clearly, a text file is not an acceptable source for a video hosting platform that needs to generate preview clips and screenshots from the original. Since I can't know what other surprises are in store, it seemed wise to start by drafting a list of commonly-encountered ffmpeg formats that are *known* to be actual videos until I get a better grasp on the domain. I generated the initial list by running a crawler against a collection of videos I had lying around. My intent was to log rejections, examine the rejected file manually, and if it's actually a video, add the format to the whitelist. -ofer
participants (2)
-
Carl Eugen Hoyos -
Ofer Nave