How to convert .ts file into a mainstream format losslessly?

127,422

Solution 1

Matroska (MKV)

This will stream copy (re-mux) all streams:

ffmpeg -i input -map 0 -c copy output.mkv

The -map 0 option is used to include all streams. Otherwise it will use the default stream selection behavior which would only result in one stream per stream type being selected. Since Matroska can handle most arbitrary streams I included -map 0.

MP4

This will stream copy (re-mux) all streams:

ffmpeg -i input -map 0 -c copy output.mp4
  • If your inputs formats are not compatible with MP4 you will get an error.
  • Your player/device may not support all arbitrary, less common, or legacy formats even if they are supported by MP4.
  • If in doubt re-encode to H.264 + AAC as shown below.

This will re-encode the video to H.264 and stream copy the audio:

ffmpeg -i input.ts -c:v libx264 -c:a copy output.mp4

The next example will re-encode both video and audio:

ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4

Lossless H.264 example:

ffmpeg -i input.ts -c:v libx264 -crf 0 -c:a copy output.mp4

Lossless files will be huge.

See FFmpeg Wiki: H.264 for more info.

Solution 2

VideoLAN (VLC - http://www.videolan.org/vlc/index.html) will easily convert just about anything into anything.

Give it a shot. It runs on Linux, Windows, and Mac OS X, and has a very user-friendly interface.

Solution 3

As a complement to the other answer by @llogan - as a stream copy is preferable anyway:

I have been using for a long time some commands to extract audio without changing the name of the files, which can be adapted to those presented here, in order to have them integrated into file managers' context menus.

So, for "demuxing and muxing" without changing the name of the file use:

ffmpeg -i "$0" -map 0 -c copy "${0%%.*}".mkv

I have added that to Thunar's custom actions and to FileManager Actions Configuration tool (Nautilus, Nemo, Caja, PCManFM), like so:

sh -c 'ffmpeg -i "$0" -map 0 -c copy "${0%%.*}".mkv' %f

while restricting it to "*.ts" in Thunar and video/mp2t in FileManager Actions.


As for GUI applications, there are some that can process without transcoding, by copying streams into a different container like mkv or mp4.

I use MKVToolNix (output only mkv) and dmMediaConverter (mkv, mp4 and any format supported by ffmpeg).

Share:
127,422

Related videos on Youtube

lacton
Author by

lacton

I dream of web and mobile applications that truly delight their users. I am convinced that "Team = Product", i.e., the qualities of the product reflect the collective qualities of those that create it. My core approach is to grow a shared vision, that will guide the enthusiasm and creativity of the teams. To put people in a situation where things get done, I use my simple and time-proven toolkit of lightweight methods (agile, lean, test-driven development, problem solving...). With 30 years of coding practice, I'm comfortable getting dirty with technical details and finding robust solutions. But the most rewarding experience for me is to develop other people, to make myself dispensable and thus enable the team and the product to prosper even without me. By the same token, I contribute to open source projects and share my latest insights as a public speaker.

Updated on September 18, 2022

Comments

  • lacton
    lacton over 1 year

    I have a file that ends in .ts (e.g., here are the first 10 MB). I would like to convert it to a more main stream format (e.g., mp4, MPEG2-PS...), in a lossless way if possible (i.e., remuxing).

    I have read the How do I convert .ts files into something useful? question. I tried avidemux with the settings "copy" for the video and audio streams, and the "PS" container format for MPEG. That failed with the error message "Incompatible audio / For DVD, audio must be 48 kHz MP2 (stereo), AC3, DTS or LPCM (stereo)".

    I also tried the suggested CLI command.

    avconv -i 10MB.ts -vcodec copy -acodec copy 10MB.mpg
    

    The output file has the right video, but no sound, at least when played with VLC. This is quite puzzling, because avconv seems to have correctly detected the audio stream.

    Input #0, mpegts, from '10MB.ts':
      Duration: 00:00:06.36, start: 51523.824800, bitrate: 12563 kb/s
      Program 37888 
        Stream #0.0[0x100]: Video: mpeg2video (Main), yuv420p, 1440x1080 [PAR 4:3 DAR 16:9], 20000 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
        Stream #0.1[0x110]: Audio: aac, 0 channels, fltp, 144 kb/s
        Stream #0.2[0x130]: Data: [6][0][0][0] / 0x0006
        Stream #0.3[0x138]: Data: [6][0][0][0] / 0x0006
        Stream #0.4[0x140]: Data: [13][0][0][0] / 0x000D
        Stream #0.5[0x160]: Data: [13][0][0][0] / 0x000D
        Stream #0.6[0x161]: Data: [13][0][0][0] / 0x000D
        Stream #0.7[0x162]: Data: [13][0][0][0] / 0x000D
        Stream #0.8[0x170]: Data: [13][0][0][0] / 0x000D
        Stream #0.9[0x171]: Data: [13][0][0][0] / 0x000D
        Stream #0.10[0x172]: Data: [13][0][0][0] / 0x000D
    Output #0, mpeg, to '10MB.mpg':
      Metadata:
        encoder         : Lavf54.20.4
        Stream #0.0: Video: mpeg2video, yuv420p, 1440x1080 [PAR 4:3 DAR 16:9], q=2-31, 20000 kb/s, 90k tbn, 90k tbc
    Stream mapping:
      Stream #0:0 -> #0:0 (copy)
    

    I also tried the CLI command suggested in the comments of another question.

    avconv -i 10MB.ts -c:v copy -c:a libfaac 10MB.mp4
    

    Again, no sound in the output file.

    EDIT: I tried VLC as suggested by @Daniel. It was almost perfect. It was fast and user friendly. I just had to click on "Convert / Save", add the input file, select the MP4 profile, configure Video codec and Audio codec to "Keep original video / audio track", choose a destination file, and click on "Start". The video looked perfect, but the audio was somehow slightly corrupted, but it might be caused by something quite exotic in the audio stream of my video.

    • Admin
      Admin over 8 years
      Have you tried VLC? videolan.org/vlc/index.html
    • Admin
      Admin over 8 years
      You mentioned you wanted to remux, not re-encode if possible. Are you sure VLC is only remuxing?
    • Admin
      Admin over 8 years
      @LordNeckbeard Good point! How can I be sure?
    • Admin
      Admin over 8 years
      I'm not familiar with VLC for converting, but I added an answer using ffmpeg.
    • Admin
      Admin over 8 years
      VLC will convert anything to anything with outstanding losslessness. Basically you're limited by the format you chose.
  • SuperSluether
    SuperSluether over 8 years
    If you wish to encode a lossless MP4 file, add either -qp 0 or -crf 0, as mentioned on the FFMpeg wiki page: trac.ffmpeg.org/wiki/Encode/H.264
  • llogan
    llogan over 8 years
    @SuperSluether Lossless makes huge files. By "lossless" I believe lacton meant "remuxing".
  • lacton
    lacton over 8 years
    The sample file I provided is indeed truncated. The actual file I want to remux is too big for easy sharing.
  • lacton
    lacton over 8 years
    Yes, by lossless, I wanted to indicate my preference for remuxing rather than reencoding. Adding this to the original question.
  • lacton
    lacton over 8 years
    What "invalid extradata" are you referring to?
  • llogan
    llogan over 8 years
    @lacton Try muxing into matroska: ffmpeg -i 10MB.ts -c copy output.mkv. You will get error: Error parsing AAC extradata, unable to determine samplerate. That is why my MKV example re-encoded the audio. Also see #4472: AAC copy from stream without encoding fails and lavf/mkv: Fix AAC remuxing. However, none of this may apply to your large, untruncated input file and it may work as expected.
  • lacton
    lacton over 8 years
    Now that I've used VLC for video conversion, I would recommend it. Thank you for this suggestion. For my .ts file, it almost worked. The video looked perfect, but the audio was somehow slightly corrupted. It might be caused by something quite exotic in the audio stream of my video. I edited the original question to include this.
  • lacton
    lacton over 8 years
    @LordNeckbeard I confirm I get the "Error parsing AAC extradata" message even with the full file. There is definitely something fishy going on with the audio stream. When playing the video with VLC, it detects 2 different audio streams, labelled Track 1 and Track 2. One is the normal audio stream, the other is a commentary audio stream. Somehow the broadcaster managed to squeeze two audio streams into one...?
  • lacton
    lacton over 8 years
    @LordNeckbeard ffmpeg -i input -c:v copy -c:a aac output.mkv worked perfectly. The video was remuxed, and the audio sounds as good as the original, without the slight corruption I got when converting with VLC.
  • Daniel
    Daniel over 8 years
    Yeah, it might work better if you use a different subcontainer for the audio inside the mp4. Perhaps RAW.
  • cipricus
    cipricus about 4 years
  • Ahmed
    Ahmed over 3 years
    Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
  • llogan
    llogan over 3 years
    @Ahmed Need to see your command and the complete log. You can use a pastebin site and provide a link in a comment.
  • Admin
    Admin almost 2 years
    On Ubuntu 20.04, I've tried the above "ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4 " & "ffmpeg -i input -c:v copy -c:a aac output.mkv". Both give "input.ts: could not find codec parameters". I've tried vlc but it creates a 161 byte output.mp4 and does nothing more. The input.ts file shows N/A for Video & Audio codecs. Help please!