How to add MOV file header to raw data written file with ffmpeg? - swift

I am going to record H264 encoded video stream data in iOS using swift.
I am not familiar with video codec formats so don't know how to do this. But I've tried to write the H264 raw video data to the file sequently and see its file Info. I am surprised that it has almost video file info (compared with standard mp4, MOV file). The only missing info is video duration, file size, overall bit rate, encoded data, etc. So I am just wondering if video can play if I add the MOV file header to this file manually. Spent few hours to googling how to add MOV file header with ffmpeg but stacked. Any help would be appreciated. Thanks

You can nominally use ffmpeg to do this:
ffmpeg -i in.h264 -c copy out.mov
However, due to a bug in ffmpeg relating to generation of PTS for video streams with multiple B-frames, the output video may not play smoothly. Test and check.
If it doesn't there's a workaround which involves using mp4box from GPAC.
mp4box -add in.h264 -new out.mp4
and then
ffmpeg -i out.mp4 -c copy out.mov

Related

Encoding video for streaming with multiple subtitle streams in VLC

I am implementing an offline video streaming service, so our users can have the encrypted videos on their computers and open them only by using our streaming app.
So what I need is to stream a freshly decrypted video file directly to a local VLC client so I don't have to save it on disk. A minimum working example would be:
cat my_video.mp4 | vlc -
But this only shows the video and audio tracks, no subtitles.
I want to know if there is a way to encode a video so that this would work with 3 audio tracks and 3 subtitle tracks.
I would also settle for an answer explaining that it is not possible and why not.
More details below:
I have 3 audio tracks and 3 subtitle tracks and I am using FFMPEG.
(The original format of the videos is mp4 with an h264 encoding)
Currently, I am able to make streamable .mp4 files, thanks to this answer, and also to make .ts files that will show the video and the 3 audio options but not the subtitles. The command I am using is this:
video_name=demo_0101.mp4
sub_name=demo_0101.srt
output=0101.mp4 # Or `.ts`
ffmpeg\
-i en_raw/$video_name -i pt_raw/$video_name -i es_raw/$video_name\
-i en_subs/$sub_name\
-i pt_subs/$sub_name\
-i es_subs/$sub_name\
-map 2:v\
-map 0:a:0 -map 1:a:0 -map 2:a:0\
-map 3:s -map 4:s -map 5:s\
-c:v libx264 -crf 22\
-movflags faststart\ # (This line is only necessary for .mp4)
-c:a:0 aac -c:a:1 aac -c:a:2 aac\
-c:s:0 mov_text -c:s:1 mov_text -c:s:2 mov_text\
$output
The problem is: The subtitles are not recognized (don't show up at all) using the .ts format and when using .mp4 VLC reports an error:
Unidentified codec:
VLC could not identify the audio or video codec
Please note when not streaming the .mp4 version works with all audios and the subtitles, i.e.:
vlc my_video.mp4
In case someone is wondering I plan on selecting the which subtitle and audio to play from command line with the VLC options: --audio-track 1 --sub-track 0
I hope someone can help me. Thanks in advance.
I was unable to find a way to encode the subtitles directly on the video which would be the preferable solution. However, I found a VLC command that will display the subtitle file together with the streaming video:
cat 0101.mp4 | vlc --sub-file en_subs/demo_0101.srt
This is not ideal for 2 reasons:
It does not work with the .ts format only .mp4.
This requires me to save the original subtitle files on the user disk.
I will not mark this solution as the accepted answer because if it is possible to stream the video directly with the subtitles I want to know, and I also believe that such answer would be helpful for others that happen to read this page.

Video file formats for H.264

I need to develop a small module, that can read and write h.264 packets to a container file. So far I have understood, typically h.264 encoded packets are stored in a mp4 file.
My question is, can h.264 encoded packets be stored in other file containers like in ogg file or in mkv file or in webm file.
If yes, how do I convert a mp4 file to an ogg file or mkv file or webm file, keeping same h.264 encoded packets in file.
H.264 can go into MP4, MKV, webm (pretty much same as MKV), FLV, F4V and others.
Please check https://en.wikipedia.org/wiki/Comparison_of_video_container_formats
For conversion I'd use ffmpeg which will do most combinations.
ffmpeg -i inputfile.xyz -vcodec copy ... (specify output file and audio stream)

Google Speech API doesn't give correct result when audio is sent in file

I chanced upon the article at Google Speech API which suggested a mechanism for extracting text from audio file through Perl. Now I have recorded a audio file, which you will find at http://vocaroo.com/i/s0lPN5d3YQJj. It is a simple piece of audio, reading I love you. When I go to the Google speech API in Chrome, and speak those words, I get the right result. When I try the code at the above mentioned link with the audio file I pointed out, it returns strange results, like logan. How can I make it more accurate? This is just a sample audio, what I am generally doing is extracting the audio from a video file through FFMpeg using something like ffmpeg -i input.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 output.mp3, followed by ffmpeg -i input.mp3 output.flac.
Have you tried playing the audio files you are creating?
You are setting an audio bitrate of 192 bits/second which is ridiculously low.
For 192Kbps you need -ab 196608.

http live streaming for mp3 files

I need help in converting mp3 files to Apples Http Live Streaming protocol files. I am working on a music application and wants to use Live streaming in this app.
I got this link http://www.ioncannon.net/programming/452/iphone-http-streaming-with-ffmpeg-and-an-open-source-segmenter/ from google but it contains how to live stream video files.
Can anybody help me with mp3 files.
Thanks
With ffmpeg you're also able to convert mp3-files only (no video).
Just use:
ffmpeg -i yourmp3.mp3 ...your arguments... output.mp3
To add to Tim's answer, HLS supports mp3 codec. So if your audio is already in mp3, all you would need is the segmenter

Streaming "proxy" converting video formats

This is related to my another question
Here I'd like to ask if it is in theory (according to video file formats and codecs, etc) possible to have such scenario:
1) Client on iPhone has a reference to video in flv format. It sends http request to converting "proxy" like http://convproxy.com?source=url_of_original_video.flv by just clicking such link in Safari
2) Converting proxy starts downloading that flv file and converting it to mp4 (which iphone understands) on the fly, returning converted portion as http response, so iPhone can immediately start playing it, before entire flv is downloaded and converted.
I was playing with ffmpeg trying to do such thing, and it indeed converts flv and produces mp4 file, however that mp4 file can not be played until convertion is finished or ffmpeg is stopped. If I just kill ffmpeg process the mp4 file can not be played. If I let it finish or press ctrl-c to stop it, the part that was downloaded and converted can be played. Seems like ffmpeg does some job after it receives stop signal. Is that a necessary part of mp4 format or it can be done differently? I see that iPhone can stream video, by starting playing before the entire file is downloaded to it, so in general it seems like possible scenario for me.
I short words, I can convert flv file to mp4 file, and the question is if I can convert flv stream to mp4 stream.
According to wikipedia, the MP4 container format requires a separate "hint track" to enable streaming. I assume ffmpeg writes this at the end of the conversion. If the iPhone OS requires this track to stream, I don't see a way to stream live video outside of using a different format and having a custom decoder on the iPhone side similar to how the Orb client for iPhone does it.