my HD Homerun for PLEX saves .TS files (transport stream).

in order to shrink this to 720 i use the following script:

for INF in *.ts 
    do ffmpeg -i "$INF" -vf scale=-1:720 -c:v libx264 -crf 23 -preset ultrafast -c:a copy "${INF%.*}.mp4" 

this works well and converts every .TS in folder .sh script is stored in to an mp4 container. I then run the following script:

for INF in *.mp4 
    echo "Extracting subtitles"
    ccextractor "$INF" -o "/tmp/$(basename "$INF" .ts).srt"

    echo "Moving subtitles" 
    mv -v /tmp/*.srt .

My goal is combine both these bash loops into one script. Ideally i would like, one script that will: a) scan current folder and all sub-folders looking for .ts files. b) remux .ts to .mp4 c) pull closed captions out of .ts and store as .srt d) remove all .ts files

I am not sure I added a / after 'for INF in *.ts' would include sub-directories. and not sure how to join these two scripts into one file or where to put the remove file(s) code

any ideas would be greatly appreciated. -shaun

  • It looks to me that you're pulling the subtitles from the mp4 file, at least that is what your code snippet above does.
    – wurtel
    Commented Feb 7, 2018 at 12:28
  • yes the .ts transport stream is a container with an mpeg2 file as well as closed captioning. VLC picks up CC info from the stream; however, PLEX does not do so. So i have to extract it to .srt manually. So i convert .ts to smaller size mp4, extract closed captions. i want to create one script that will do all this, then remove the .ts files too.
    – NixonShaun
    Commented Feb 7, 2018 at 13:01
  • You're not reading my comment correctly. You write that you want to extract the subtitles from the .ts file, but the code you show is extracting it from the .mp4 files. So what is it?
    – wurtel
    Commented Feb 7, 2018 at 13:49
  • my apologies. I will try change it to .ts and see if it still pulls the closed captions out of the stream or if it has to pull it from the .mp4. will update you one i get home from work and am able to test it on .ts file. At present it is extracting the closed caption subtitles from the mp4 file.
    – NixonShaun
    Commented Feb 7, 2018 at 16:56
  • thank you i tested it and ccextract will work on both .ts and .mp4. I have changed the Bash Script to For INf in *.ts Do... this will take the closed caption subtitle from the original file not the remuxed one.
    – NixonShaun
    Commented Feb 7, 2018 at 20:09

1 Answer 1


How about this:

for x in *.ts
  y=$(basename "$x" .ts)
  ffmpeg -i "$x" "$y".mp4
  ffmpeg -i "$x" "$y".srt
  • using what you wrote i made a change or two to include the ffmpeg options i use and to use ccextractor for subs as they are closed captions buried in a transport stream. for x in *.ts do y=$(basename "$x" .ts) ffmpeg -i "$x" -vf scale=-1:720 -c:v libx264 -crf 23 -preset ultrafast -c:a copy "$y".mp4 ccextractor "$x" -o "$y".srt done
    – NixonShaun
    Commented Feb 7, 2018 at 11:58
  • could not get it to post in code block, tried 4 spaces and highliting text and ctr k, but did not seem to work
    – NixonShaun
    Commented Feb 7, 2018 at 11:59
  • @StevenPenny: To be fair — the OP doesn’t have enough rep to upvote, and maybe they want to delay the “accept” until they get the answer completely working. Commented Feb 8, 2018 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .